Invoking AWS Lambda Functions with S3 Events
Welcome to this Learning Activity for creating a Lambda function with an S3 Event trigger! This Learning Activity provides hands-on experience with creating and customizing Lambda functions from within the console, as well as configuring Lambda triggers for automating future invocations of your Lambda functions. The primary focus will be on the following features within AWS Lambda: 1. Lambda Console 2. Function Code 3. Lambda Triggers 4. CloudWatch Lambda Invocation Logs AWS Lambda allows you to create functions and lets you only worry about managing your code! AWS will handle managing all of the underlying compute. The function you create in today's live environment will provide you a hands-on experience for creating Lambda functions and configuring event-based triggers for your functions. Enjoy learning!
Invoking AWS Lambda Functions with S3 Events
Welcome to this hands-on lab, where we'll be creating a Lambda function with an S3 Event trigger! AWS Lambda allows us to create functions and lets us just worry about managing our code! AWS will handle managing all of the underlying computing.
Creating the function will provide some experience with setting up Lambda functions and configuring event-based triggers for them.
The primary focus will be on the following features within AWS Lambda:
- Lambda Console
- Function Code
- Lambda Triggers
- CloudWatch Lambda Invocation Logs
Use the link and credentials provided in the hands-on lab overview page, to log into the AWS console. There should be a username of cloud_user and a randomly generated password.
Once we're logged in, make sure we are in the us-east-1 region before beginning.
NOTE: All code used in the lesson is available to download here:
Create a Lambda Function Within the Lambda Console
To create a Lambda function in the AWS Console:
- Navigate to the Lambda portion of the AWS Console.
- Select Create a function, then on the next page choose Author from scratch.
- Give it a name (something simple like S3Function should be fine)
- Select Python3.6 as your runtime.
- In the next dropdown (Role) select Choose an existing role.
- From the Existing role list, choose the one that was provisioned as a part of the lab.
- Click the Create function button.
- Scroll down on the next screen to the Function code section:
- Copy the code from the example provided at the GitHub link provided earlier
- Scroll a little farther down and find Basic settings:
- We can give it a Description of something like S3 Function
- Then drag the memory slider to 256MB
- Set the Timeout to 3 seconds.
- Click Save
Create a New S3 Test Event and Test the Function by Invoking It with the Created Function
We need to create a Lambda test event in the AWS Console that will be used to manually invoke and test the Lambda function: (making sure to mimic an S3 event):
- Select the Configure test events from the drop-down next to Test at the top of the Lambda console.
- Make sure that Create new test event is selected, and that Hello World is the Event template that we're going to use.
- Give it an Event name of S3Test.
- Replace the code in the lower half of the window with the JSON that's provided in the GitHub repository, then click Create.
- Click on the Test button and verify your function succeeds.
- Once we get the succeeded message, we're good to move forward.
Create, Enable, and Then Test an S3 Event Trigger for the Lambda Function
To configure an S3 event trigger for the function that is mapped to the bucket created in S3:
- Scroll down to the Designer section, then scroll down through the Add Triggers section within, and select S3.
- Configure the event to target your S3 bucket:
- Scroll down a bit farther to the Configure triggers section.
- Our S3 bucket should already be in the Bucket dropdown.
- From the Event type dropdown, choose Object Created (All).
- Leave the Prefix and Filter sections blank.
- Verify your trigger is enabled (make sure it's checked), and then click Add.
- Save changes to our function again by scrolling back up and clicking Save.
Now test by uploading an object to S3:
- Navigate to the main S3 console, and click on our bucket.
- Click the Upload button, and browse for a local file (any file will work). Just keep clicking Next, accepting the defaults, until it's uploaded.
- Now let's navigate back to Lambda, and click on our S3Function.
- Click on the Monitoring tab, and we shouild see a graph showing that our function was invoked.
- To see it in actual logs, click Jump to Logs in the Invocations graph section:
- The START RequestId:... lines are events that invoked the function. One was our initial test, and the other was the file upload.
Well, we've done it. We managed to set up a Lambda function that triggers off of an S3 event. Congratulations!