Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save JStans12/840090c8100e379012dbe6e3979e8430 to your computer and use it in GitHub Desktop.
Save JStans12/840090c8100e379012dbe6e3979e8430 to your computer and use it in GitHub Desktop.

Create s3 bucket

  • bucket needs to be in the same region as lambda function

Create a deployment package

This is the code that makes up your lambda function. We zip it up and can easily deploy it using the aws lambda cli.

  • write lambda code and zip it
  • don't zip the container folder, but do zip all the require folders and files inside of it

Create an execution role

Role is require to give lambda the permissions it needs.

  • sign into IAM console
  • Roles < Create Role (most likely will want AWS Lambda < AWSLambdaExecute)
  • write down the role arn

Upload the deployment package

$ aws lambda create-function \
--region REGION \
--function-name FUNCTIONNAME \
--zip-file fileb://file-path/FUNCTIONNAME.zip \
--role ROLE-ARN \
--handler FUNCTIONNAME.handler \
--runtime nodejs6.10 \
--profile adminuser \
--timeout 10 \
--memory-size 1024

Write down the function arn.

Note that you will need to configure adminuser in `.aws/credentials

[adminuser]
aws_access_key_id = *********
aws_secret_access_key = *********
region = REGION

You should now see the function in AWS Lambda < Functions

Test the function

  • navigate to the function in AWS Lambda
  • click 'test and set 'Sample Event Template' to S3 Put
  • success message!

Add Permissions to the Lambda Function's Access Permissions Policy

Give s3 permissions to invoke the lambda:InvokeFunction function.

$ aws lambda add-permission \
--function-name FUNCTIONNAME \
--region REGION \
--statement-id UNIQUEID \
--action "lambda:InvokeFunction" \
--principal s3.amazonaws.com \
--source-arn arn:aws:s3:::BUCKETNAME \
--source-account ACCOUNTID \
--profile adminuser

Test this with:

$ aws lambda get-policy \
--function-name FUNCTIONNAME \
--profile adminuser

Add trigger to the Lambda

On the lambda page:

  • Triggers < New Trigger < s3
  • Event Type = Object Created (all)

Test it

If the function is just configured to return a message, you can compare the invocation count before and after you upload a file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment