Skip to main content

Deploying Lambda Functions Using CloudFormation (Contains Environment Variables)

Hands-On Lab

 

Photo of

Training Architect

Length

01:00:00

Difficulty

Advanced

Welcome to this Learning Activity where we will create and deploy a Lambda function using AWS CloudFormation! You will gain hands-on experience with creating Lambda functions using CloudFormation from within the AWS Console. The primary focus will be on these AWS features: 1. Lambda Console 2. Function Code 3. IAM Roles 4. CloudFormation Just worry about perfecting your code, let AWS handle all the infrastructure woes. Embrace the serverless life!

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Deploying Lambda Functions Using CloudFormation (Contains Environment Variables)

Introduction

Welcome to this hands-on lab where we will create and deploy a Lambda function using AWS CloudFormation!

You will gain hands-on experience with creating Lambda functions using CloudFormation from within the AWS Console.

The primary focus will be on these AWS features:

  1. Lambda Console
  2. Function Code
  3. IAM Roles
  4. CloudFormation

Just worry about perfecting the code, and let AWS handle all the infrastructure woes. Embrace the serverless life!

The Scenario

As the admins, we've got to create a new stack within CloudFormation. The template we use for this will create a new Lambda function, and create an S3 bucket that will trigger an event every time a jpg image gets uploaded to it. This trigger will point at the Lambda function, which is going to do a couple of things. First, it will send a thumbnail version of the image to a another S3 bucket. Second, it will record the event by sending a log entry to CloudWatch.

Logging In

Please go to the AWS Console using the link provided after lab creation is complete.

Log in using the credentials provided to you. You should have been given a username of cloud_user and a randomly generated password.

Please make sure you are in the us-east-1 region before beginning.

Note: All code used in the lesson is available to download, so you do not need to rewrite it. It is available here:

https://github.com/linuxacademy/content-lambda-deep-dive/tree/master/section_5/live_activity_7

Upload the Template and Create the CloudFormation Stack

Upload the Template

We need to create our stack using the supplied template:

  1. Upload the lambda_function.zip file to bucketa:
    • Navigate to S3 in the AWS console. There should be two buckets in there (maybe three, but ignore the elasticbeanstalk one).
    • Pick the first one (it will have bucketa somewhere in the name), then click Upload. Navigate to wherever we've stored the zip file, and select it.

Create the Function

  1. Navigate to CloudFormation.
  2. Click Create stack.
  3. In the Prerequisite - Prepare template section, select Create template in Designer.
  4. Click Create template in designer.
  5. Select YAML in the Choose template language area, then click the Template tab at the bottom.
  6. Copy everything from the student-cf.yml file and paste it into the Template window.
  7. Click the validate button, a box with a check in it up at the upper-left. We should get a Template is valid message more toward the top-center of the designer window. Now we can hit the refresh button and get a diagram, a graphical representation of what we're building.
  8. Click the upload button (a cloud with an arrow in it).
  9. On the next screen (we landed back in the Create stack screen), click Next
  10. Click Next.
  11. On the stack details page, set the following values:
    • Stack name: Linux-Academy
    • CodeBucket: (grab the whole name of bucketa from the S3 console)
    • DestinationBucket: (paste the whole name for bucketb in here)
    • LambdaFileName: This is already populated for us.
  12. Click Next.
  13. Leave the defaults on the stack options page, and click Next.
  14. Click Create stack.
  15. In the Tags section on the next screen, set a Key of ENV and a Value of PROD.
  16. Click Next.
  17. The next screen is just a review, so we can check the acknowledgement box at the bottom and click Create stack. This process may take a few minutes. When we see a CREATE_COMPLETE message in the Stacks window for this Linux-Academy one, we're good to go. Just keep hitting the refresh button to see how it's progressing.

Verify That the Resources Exist

Let's verify that all of the resources exist:

  • Make sure there is another bucket (in S3) created that contains the word source.
  • Within that source bucket properties, make sure there is an event configured that invokes our function.
  • Verify that the Lambda function exists:
    • Navigate to Lambda in the AWS console, and check to see if the function was created.

Test the Function

Let's test everything out now:

  1. Navigate to the source bucket.
  2. Upload a .jpg image to the source bucket.
  3. Wait a bit and then verify that the function was invoked.
  4. Check for any errors within the metrics page.
  5. After it's done, verify that there is a resized image sitting in the destination bucket.

Conclusion

Wow, that was quite a job. It's a little more complicated than this, but what we've essentially done is set up a process that resizes images. If we were running a blog, and wanted thumbnail-sized images on it (with the end user able to click on it if they want to view a the original bigger image) in order to save bandwidth, this situation is ideal. Congratulations!