Creating AWS Lambda Versions and Aliases via the AWS CLI

Hands-On Lab

 

Photo of Andru Estes

Andru Estes

Training Architect

Length

00:30:00

Difficulty

Advanced

Welcome to this Learning Activity, where we'll create Lambda function versions and aliases via the AWS CLI. After completing this learning activity, you will have some valuable hands-on experience with Lambda function best practices via the AWS CLI. The primary focus will be on these AWS Lambda features: 1. AWS Lambda 2. Versions and Aliases 3. AWS CLI So let's ditch the console, and get our hands dirty in the CLI!

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.

Creating AWS Lambda Versions and Aliases via the AWS CLI

Introduction

In this hands-on lab, we'll create Lambda function versions and aliases via the AWS CLI.

Solution

Open a terminal session, and log in to the server via SSH using the credentials provided on the lab page:

ssh cloud_user@<PUBLIC_IP_ADDRESS>

In the browser, log in to the live AWS environment using the credentials provided, and make sure you're in the N. Virginia (us-east-1) region.

All of the code used in the lesson is available for download here.

Lab Prep

  1. In AWS, navigate to IAM > Roles.
  2. Click the lambda_exec_role_LA role listed.
  3. Copy the ARN and paste it into a text file. We'll need it later.
  4. In the terminal, make sure AWS CLI is installed by running:

    aws help

    You should see it's installed.

Create Your Lambda Function

  1. In the terminal, run:

    vim lambda_function.py
  2. Paste in the code from GitHub.

  3. Save and exit the file.

  4. Zip the file:

    zip lambda_function.zip lambda_function.py
  5. Create the function (replacing <lambda_exec_role_LA_ARN> with the ARN you copied earlier):

    aws lambda create-function
    --function-name SnapShot 
    --description "Taking EBS Snaps" 
    --memory-size 1024 
    --timeout 10 
    --handler lambda_function.handler 
    --runtime "python3.6" 
    --role "<lambda_exec_role_LA_ARN>" 
    --zip-file fileb:///home/cloud_user/lambda_function.zip 
    --region us-east-1

Publish Your First Version

  1. Publish a new version:

    aws lambda publish-version 
    --function-name SnapShot 
    --region us-east-1
  2. Check the versions:

    aws lambda list-versions-by-function 
    --function-name SnapShot
    --region us-east-1

Create Your Alias

  1. Create the alias:

    aws lambda create-alias 
    --function-name SnapShot 
    --description "Production Alias" 
    --function-version 1 
    --name PROD 
    --region us-east-1
  2. Check the aliases:

    aws lambda list-aliases 
    --function-name SnapShot 
    --region us-east-1

Create Your CloudWatch Event Rule/Trigger

  1. Add a permission for events to invoke the function alias:

    aws lambda add-permission 
    --function-name SnapShot 
    --qualifier PROD 
    --statement-id 001 
    --principal events.amazonaws.com 
    --action lambda:InvokeFunction 
    --region us-east-1
  2. Copy the ARN listed in the output and paste it into a text file, as we'll need it later.

  3. Create the CloudWatch rule:

    aws events put-rule 
    --name "EC2-SNAP" 
    --schedule-expression "rate(2 minutes)" 
    --state "ENABLED" 
    --description "Takes EBS Snapshot of volumes on a scheduled basis." 
    --region us-east-1
  4. Add a target to the rule (replacing <PROD_ARN> with the one you copied a minute ago):

    aws events put-targets 
    --rule "EC2-SNAP" 
    --targets "Id"="1","Arn"="<PROD_ARN>" 
    --region us-east-1
  5. Pause the rule:

    aws events disable-rule 
    --name EC2-SNAP 
    --region us-east-1
  6. Verify your output.

Publish a New Version and Remap Your Alias

  1. Update the code:

    vim lambda_function.py
  2. Paste in the updated code from GitHub.

  3. Save and exit the file.

  4. Zip the file:

    !zip
    zip lambda_function.zip lambda_function.py
  5. Upload your updated function code to Lambda:

    aws lambda update-function-code 
    --function-name SnapShot 
    --zip-file fileb:///home/cloud_user/lambda_function.zip 
    --region us-east-1
  6. Publish a new version from that updated code base:

    aws lambda publish-version 
    --function-name SnapShot 
    --region us-east-1
  7. Once created, remap your alias to Version 2:

    aws lambda update-alias 
    --function-name SnapShot 
    --name PROD 
    --function-version 2 
  8. Check the alias:

    aws lambda list-aliases 
    --function-name SnapShot 
    --region us-east-1
  9. Re-enable the rule:

    aws events enable-rule 
    --name EC2-SNAP 
    --region us-east-1

Conclusion

Congratulations on completing this hands-on lab!