- bucket needs to be in the same region as lambda function
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
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
$ 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
- navigate to the function in AWS Lambda
- click 'test and set 'Sample Event Template' to S3 Put
- success message!
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
On the lambda page:
- Triggers < New Trigger < s3
- Event Type = Object Created (all)
If the function is just configured to return a message, you can compare the invocation count before and after you upload a file.