Skip to main content

AWS Lambda Versioning and Aliases from the CLI

Hands-On Lab

 

Photo of Craig Arcuri

Craig Arcuri

AWS Training Architect II in Content

Length

01:00:00

Difficulty

Advanced

In this AWS hands-on lab, we will learn to work with Lambda functions and create function versions and aliases from the command line interface (CLI). We will first use the CLI to create a Lambda function. After the function has been created, we can use the CLI to create versions and aliases for the function. The key thing to remember about versions is that any new version we create is immutable, but the $LATEST version can be changed and used to create new versions. Aliases can be pointed to the same function or to different functions, or be moved around as needed. In this lab, we will create multiple versions and aliases and point those aliases to the necessary version. We will also use the AWS Management Console to view the results of what we do from 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.

AWS Lambda Versioning and Aliases from the CLI

Introduction

In this AWS hands-on lab, we will learn to work with Lambda functions and create function versions and aliases from the command line interface (CLI). We will first use the CLI to create a Lambda function. After the function has been created, we can use the CLI to create versions and aliases for the function. The key thing to remember about versions is that any new version we create is immutable, but the $LATEST version can be changed and used to create new versions. Aliases can be pointed to the same function or to different functions, or be moved around as needed. In this lab, we will create multiple versions and aliases and point those aliases to the necessary version. We will also use the AWS Management Console to view the results of what we do from the CLI.

Create a Lambda Function

  1. In the AWS Management Console, navigate to the IAM service.
  2. Click Roles in the left sidebar.
  3. Enter "Lambda" in the search bar at the bottom of the page.
  4. Under Role name, select the Lambda execution role.
  5. On the Summary page, click the clipboard icon next to the Role ARN to copy it to your clipboard.
  6. Save the Role ARN value (e.g., in a text document) for reference later.
  7. In a new browser tab, navigate to the EC2 service in the AWS Management Console.
  8. Under Resources, click 1 Running Instances.
  9. At the bottom of the screen, click the clipboard icon next to IPv4 Public IP to copy it to your clipboard.
  10. In the CLI, connect to the instance via SSH.
    ssh cloud_user@PUBLIC_IP_ADDRESS
  11. Enter yes at the prompt.
  12. Enter your cloud_user password at the prompt.
  13. Run the following command:
    sudo yum update
  14. Enter your cloud_user password at the prompt.
  15. Run the following command:
    aws configure
  16. Press Enter at the two access key prompts to leave them blank.
  17. At the Default region name prompt, enter us-east-1.
  18. Open the amilookup.js file.
    vim amilookup.js
  19. Press i to enter Insert mode.
  20. Copy the contents of the amilookup.js file, and paste them into the file in the CLI.
  21. Press Esc, then type :wq! to save and quit the file.
  22. Compress the file.
    zip amilookup.zip amilookup.js
  23. Create the function.
    aws lambda create-function --function-name clidemo --runtime nodejs8.10 
    --zip-file fileb://amilookup.zip --handler amilookup.handler 
    --role arn:aws:iam::ACCOUNT_ID:role/lambda_exec_role_LA 

Create and Publish Lambda Function Versions and Aliases

  1. Go back to the AWS Management Console.
  2. Navigate to the Lambda service.
  3. Click Functions in the left sidebar.
  4. In the CLI, create an alias for the function.
    aws lambda create-alias --function-name clidemo --name DEV 
    --description "sample alias" --function-version "$LATEST"
  5. Publish the latest version of the function.
    aws lambda publish-version --function-name clidemo
  6. In the AWS Management Console, click the name of theclidemo function to open it.
  7. At the top of the page, click the Qualifiers dropdown.
  8. View the Versions and Aliases.
  9. Note the DEV version in the Versions list.
  10. In the CLI, create a BETA version of the function.
    aws lambda create-alias --function-name clidemo 
    --description "sample alias" --function-version 1 --name BETA
  11. In the AWS Management Console, click the Qualifiers dropdown.
  12. Note the BETA version in the Versions list.
  13. In the CLI, create another alias.
    aws lambda create-alias --function-name clidemo 
    --description "sample alias" --function-version 1 --name PROD
  14. Refresh the AWS Management Console page, and click the Qualifiers dropdown.
  15. Note the PROD version in the Versions list.

Move and Delete Aliases and Versions

  1. In the CLI, look up the versions of the function.
    aws list-versions-by-function --function-name clidemo
  2. Look up the aliases of the function.
    aws lambda list-aliases --function-name clidemo
  3. Look up the BETA alias.
    aws lambda get-alias --function-name clidemo --name BETA
  4. Open the amilookup.js file.
  5. Press i to enter Insert mode.
  6. Change the "REQUEST RECEIVED:"n parameter to "New REQUEST RECEIVED:n".
  7. Press Esc and :wq! to save and exit the file.
  8. Compress the file.
    zip amilookup.zip amilookup.js
  9. Update the function.
    aws lambda update-function-code --function-name clidemo --zip-file fileb://amilookup.zip
  10. Publish the function.
    aws lambda publish-version --function-name clidemo
  11. Refresh the page of the AWS Management Console.
  12. Point the PROD alias to Version 2 of the function.
    aws lambda update-alias --function-name clidemo --name PROD --function-version 2
  13. Refresh the page of the AWS Management Console, and click the Qualifiers dropdown.
  14. Note the PROD alias now points to Version 2 of the function.
  15. List the versions of the function from the CLI.
    aws lambda list-versions-by-function --function-name clidemo
  16. List the aliases of the function from the CLI.
    aws lambda list-aliases --function-name clidemo
  17. Delete the DEV alias.
    aws lambda delete-alias --function-name clidemo --name DEV
  18. Refresh the page in the AWS Management Console, and click the Qualifiers dropdown.
  19. Note that the DEV alias is no longer listed.

Conclusion

Congratulations, you've successfully completed this hands-on lab!