Create Lambda Function with S3
SWEN 514/614: Engineering Cloud Software Systems
Department of Software Engineering
Rochester Institute of Technology
Activity Overview 2
u In this activity, you build a lambda function, which will be triggered when you upload
a file to your S3 ”source” bucket and copy your file to another S3 “destination” bucket
u To verify that this has worked correctly, you will verify through CloudWatch, which is a
monitoring tool that allows you to set alarms, visualize logs, metrics and many other
related activities
u Note that there will be 2 deliverables for this activity and is worth 2 points
Create 2 S3 Buckets 3
u You need to create 2 buckets, one for
the source and the other for the
destination
u Go to the console and select S3 to
create a new bucket
u Click “Create bucket”
u For your source bucket, pick a name
for your bucket under “Bucket Name”
u At the bottom of the screen, click
create “Create Bucket”
u Repeat the same steps for a
destination bucket
Create Lambda Function and add S3 Trigger 4
u Now we will create a lambda function and add an S3 trigger
u Go to the AWS console and select “Lambda”
Create Lambda Function and add S3 Trigger 5
u Click “Create a function” to create a new Lambda function
Create Lambda Function and add S3 Trigger 6
u To create a function, make sure you have “Author from scratch” selected
u Enter a name under “Function name”
u For “Runtime”, select Python 3.9
u Keep all other default settings and
click “Create function”
Create Lambda Function and add S3 Trigger 7
u Under the Configurations tab, click the “Edit” button under “General
configuration”
Create Lambda Function and add S3 Trigger 8
u We need to set up an IAM Policy for this Lambda function so it can
access your S3 buckets
u Click the link below
Click here
Create Lambda Function and add S3 Trigger 9
u Click “Add permissions > Attach policies”
Create Lambda Function and add S3 Trigger 10
u Type s3 to filter the policies and select “AmazonS3FullAccess”
u This will allow the Lambda function to have full access your S3 buckets
u Policies will be covered in a future class
u Click “Add permissions”
Create Lambda Function and add S3 Trigger 11
u Verify the policy has been added similar to what’s shown below
Verify this policy has
been added
Create Lambda Function and add S3 Trigger 12
u Go back to your Lambda function
u Click the ”Add trigger” button to add a new trigger for the Lambda
function
Create Lambda Function and add S3 Trigger 13
u Select S3 as the trigger
u Under the “Bucket”, select your source
bucket name
u For the ”Event Type”, select PUT as we
will only trigger on file uploads to the
bucket
u Click the “Recursive invocation”
checkbox to ensure you are using a
different bucket for the output
u Otherwise, this could cause your function to
be called recursively (not good)
u Click the “Add” button
Create Lambda Function and add S3 Trigger 14
u Once your trigger has been added, you see a message indicating
your S3 bucket is receiving events
u Now we need to write the Lambda function to process the event
Create Lambda Function and add S3 Trigger 15
u Click the ”Code” tab and paste the Python code (s3-lambda.py), which can be found on
myCourses in Activity #13 – Create Lambda Function with S3
u On line 10, you need to add your destination bucket name
u On line 29, add your RIT name and put quotes around it
u Click the “Deploy” button to deploy your Lambda function. It is now live!
Test the Lambda Function 16
u Open S3 from the console and open the source bucket you have previously created
u Click the “Upload” button to upload a text file of your choice from your PC
u Source bucket u Destination bucket
u Verify your uploaded file appears in your destination bucket
u If it doesn’t, go back and verify the Lambda function is correct and deployed
u If unsure, continue as CloudWatch might provide an error message
Check CloudWatch 17
u Go to the AWS console and select “CloudWatch”
u Select “Log groups” under “Logs”
u Under “Log groups”, select the log group associated with your S3 bucket
u Under “Log streams”, select the stream
Check CloudWatch – Deliverable #1 18
u Under the “Message”, find the log message from your Lambda function. You may
need to expand this out
u If everything worked properly, you should see the messages printed from the Lambda
function
u If this didn’t work, there should be an error message indicating the problem. If there are no
error messages, then it’s likely the Lambda is not deployed.
u Take a screenshot and upload to Assignments > Activity #13 - Create Lambda
Function with S3
Deliverable #2 19
u Now that you have successfully created a Lambda function triggered by an S3 bucket, your next
assignment is to create a simple ”hello world” Lambda function that’s integrated with the API
Gateway
u When accessed in a browser (using the URL format below), it should display a message from your function
u You will need to do the following:
u Create a simple Lambda function (choose any programming language) that prints a simple ”hello world”
message with your RIT username
u No trigger is needed
u Create a publicly accessible API in the API Gateway that calls this Lambda function
u Your API should follow the same conventions used in the previous activity (see below)
Note the version
Your Lambda should return your RIT
username and a simple “hello world”
message
u Submit the URL (paste in a doc) to Assignments > Activity #13 - Create Lambda Function with S3
Cleanup (only for Deliverable #1) 20
u Follow these steps to delete everything you have created for this activity
u When you are done, go back to the AWS console and select “Lambda”
u Select your lambda function and select “Actions” > “Delete” to delete the function
u When you are done, go back to the AWS console and select “S3”
u You will need to empty your buckets before you can delete them
u For Deliverable #2, do not delete your Lambda function or API until it is
graded
u Once graded, follow the above instructions to delete your Lambda
function
u Select your API and click “Actions > Delete”