Lambda event triggers are extremely useful for automating serverless workflow, as they help trigger Lambda code/logic and have use cases from monitoring to processing online purchase orders and emailing receipts. In this lab, we’ll create a Lambda function from scratch and create an S3 event trigger to execute our Lambda logic.
Learning Objectives
Successfully complete this lab by achieving the following learning objectives:
- Create IAM Role for Lambda
Create an IAM role for Lambda using the AWS IAM CLI command:
aws iam create-role --role-name LambdaIAMRole --description "Lambda Role" --assume-role-policy-document file://lambda_assume_role_policy.json --region us-east-1
- Create a Policy for the Lambda Function and Attach It to Role
Add a policy:
aws iam create-policy --policy-name LambdaRolePolicy --policy-document file://lambda_execution_policy.json --region us-east-1
Attach the policy to the role, replacing
<POLICY_ARN>
with the policy ARN included in the output of the previous command:aws iam attach-role-policy --role-name "LambdaIAMRole" --policy-arn <POLICY_ARN> --region us-east-1
- Create SNS Topic and Subscribe Your Email Address to It
Create topic:
aws sns create-topic --name LambdaTopic --region us-east-1
Subscribe an endpoint — for example, your email address — to your topic so that when you publish to the topic, notifications will be sent to your email address. Replace
<TOPIC_ARN>
with the topic ARN included in the output of the previous command:aws sns subscribe --protocol "email" --topic-arn <TOPIC_ARN> --notification-endpoint <EMAIL_ADDRESS> --region us-east-1
Confirm the subscription by clicking on the link in the email you receive after executing the previous command.
- Modify Lambda Function with SNS Topic ARN and Zip it into Lambda Deployment Package
Open the file:
vim lambda_function.py
Zip the file:
zip lambda_function.zip lambda_function.py
- Create Lambda Function
- Create a Lambda function, replacing
<ROLE_ARN>
with yours:
aws lambda create-function --memory-size 128 --function-name my-lambda --runtime python3.7 --handler lambda_function.lambda_handler --zip-file fileb://lambda_function.zip --role <ROLE_ARN>
- Create a Lambda function, replacing
- Add Lambda Permission for S3 Service to Invoke Function
Add Lambda permission, replacing
<ARN_S3_BUCKET>
with the ARN of the S3 bucket provided on the lab page:aws lambda add-permission --action lambda:InvokeFunction --principal s3.amazonaws.com --statement-id LabS3Trigger --function-name my-lambda --source-arn "<ARN_S3_BUCKET>"
- Enable and Add Notification Configuration to S3 Bucket
Open the
bucket-trigger-notification.json
file:vim bucket-trigger-notification.json
Add your Lambda function ARN, which was included in the output when you created your Lambda function.
Enable the notification configuration on the S3 website bucket, replacing
<S3_BUCKET_NAME>
with the bucket name provided on the lab page:aws s3api put-bucket-notification-configuration --bucket <S3_BUCKET_NAME> --notification-configuration file://bucket-trigger-notification.json
- Verify Configuration by Uploading a File to Provided S3 Bucket
Upload a file to the bucket, replacing
S3_BUCKET_NAME
with the bucket name provided on the lab page:aws s3 cp lambda_policy.json s3://<S3_BUCKET_NAME>
Once it’s successfully uploaded, check your email. If everything was set up properly and you subscribed to the SNS topic via email, you should receive a notification email with details of the file uploaded to the S3 bucket.