Skip to content

Instantly share code, notes, and snippets.

@crazyguitar
Last active March 17, 2018 01:32
Show Gist options
  • Save crazyguitar/f81fef74c9485651ba9c3dd831840c84 to your computer and use it in GitHub Desktop.
Save crazyguitar/f81fef74c9485651ba9c3dd831840c84 to your computer and use it in GitHub Desktop.
aws lambda note

Lambda note

Quick Start with S3

Lambda function can detect events of S3 operations including put, delete, move. Therefore, we can use lambda function to monitor S3 buckets which contain the data we are interesting.

Process the S3 event

First, we git clone the serverless-application-model from GitHub

# clone serverless-application-model from awslabs
$ git clone https://github.com/awslabs/serverless-application-model

# change dir
$ cd serverless-application-model/examples/apps/s3-get-object-python3

# generage a s3 event through sam local generate-event
$ sam local generate-event s3 > s3-event.json

# modify the key and the bucket in s3-event.json
$ vim s3-event.json

# using sam local test lambda function
$ sam local invoke -e s3-event.json s3getobjectpython3

# package the lambda function
$ sam package --template-file template.yaml --s3-bucket $S3_BUCKET --output-template-file packaged-template.yaml

# deploy the lambda function on AWS
$ sam deploy --template-file packaged-template.yaml --stack-name s3-test-stack --capabilities CAPABILITY_IAM

# invoke lambda on AWS
$ aws lambda invoke \
      --function-name $FUNCTION_NAME \
      --region $REGION \
      --log-type Tail \
      --payload file://s3-event.json \
      output.txt

Further reading:

S3 support event type

Amazon S3 can publish events of the following types:

  • s3:ObjectCreated:*
  • s3:ObjectCreated:Put
  • s3:ObjectCreated:Post
  • s3:ObjectCreated:Copy
  • s3:ObjectCreated:CompleteMultipartUpload
  • s3:ObjectRemoved:*
  • s3:ObjectRemoved:Delete
  • s3:ObjectRemoved:DeleteMarkerCreated
  • s3:ReducedRedundancyLostObject

Sample Code

template.yaml

AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Description: >-
  An Amazon S3 trigger that retrieves metadata for the object that has been
  updated.
Resources:
  s3getobjectpython3:
    Type: 'AWS::Serverless::Function'
    Properties:
      Handler: lambda_function.lambda_handler
      Runtime: python3.6
      CodeUri: .
      Description: >-
        An Amazon S3 trigger that retrieves metadata for the object that has
        been updated.
      MemorySize: 128
      Timeout: 3
      Policies:
        - Version: '2012-10-17'
          Statement:
            - Effect: Allow
              Action:
                - 's3:GetObject'
              Resource: 'arn:aws:s3:::*'
      Events:
        BucketEvent1:
          Type: S3
          Properties:
            Bucket:
              Ref: Bucket1
            Events:
              - 's3:ObjectCreated:*'
  Bucket1:
    Type: 'AWS::S3::Bucket'

lambda_function.py

import json
import urllib.parse
import boto3

print('Loading function')
s3 = boto3.client('s3')


def lambda_handler(event, context):
    # print("Received event: " + json.dumps(event, indent=2))
    # Get the object from the event and show its content type
    bucket = event['Records'][0]['s3']['bucket']['name']
    key = urllib.parse.unquote_plus(
        event['Records'][0]['s3']['object']['key'], encoding='utf-8'
    )
    try:
        response = s3.get_object(Bucket=bucket, Key=key)
        print("CONTENT TYPE: " + response['ContentType'])
        return response['ContentType']

    except Exception as e:
        print(e)
        print(f'Error getting object {key} from bucket {bucket}.')
        raise e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment