2022-09-13

Versioning and Alias in AWS Lambda

Versioning in AWS Lambda

As your AWS Lambda functions evolve over time, managing different versions of your code becomes essential. AWS Lambda provides a versioning feature that allows you to manage and track the different iterations of your Lambda function code. Each version is a snapshot of your function code and its configuration, including environment variables and resource allocation.

When you create a Lambda function or update its code, you are working on the $LATEST version. You can publish a new version to capture the current state of the function. Published versions are immutable, which means that you cannot modify their code or settings.

Creating and Managing Versions

To create a new version of your Lambda function, follow these steps:

  1. Navigate to the AWS Lambda console.
  2. Choose the function you want to version.
  3. In the Actions dropdown, select Publish new version.
  4. Enter an optional description and click Publish.

You can now view and manage the published version of your function in the Versions tab of the Lambda console.

When you publish a new version of a Lambda function, AWS automatically assigns a unique version number to it. To view the version history of your Lambda function:

  1. Go to the AWS Lambda console.
  2. Select the function you want to view the version history for.
  3. Click on the Versions tab to see the list of published versions and their details.

To delete a specific version of a Lambda function:

  1. In the Lambda console, go to the Versions tab.
  2. Locate the version you want to delete.
  3. Select the checkbox next to the version number.
  4. Click Delete from the Actions dropdown.

Remember that you cannot delete the $LATEST version of a Lambda function.

Using AWS CLI for Version Management

In addition to the AWS Lambda console, you can manage Lambda function versions using the AWS Command Line Interface (CLI). This provides a programmatic way to create, update, and delete versions, allowing for easier automation and integration with your existing workflows.

To install and configure the AWS CLI, follow the instructions in the official AWS CLI documentation:

https://aws.amazon.com/cli/

To publish a new version of a Lambda function using the AWS CLI, run the following command:

bash
$ aws lambda publish-version --function-name YourFunctionName --description "Your version description"

This command creates a new version of the specified Lambda function and returns a JSON object containing the new version's details.

To list all versions of a Lambda function, use the list-versions-by-function command:

bash
$ aws lambda list-versions-by-function --function-name YourFunctionName

This command returns a JSON object containing details of all published versions and the $LATEST version.

To delete a specific version of a Lambda function using the AWS CLI, run:

bash
$ aws lambda delete-function --function-name YourFunctionName:YourVersionNumber

Aliases in AWS Lambda

Aliases in AWS Lambda are pointers to specific function versions. They enable you to refer to a specific version using a human-readable identifier, such as "prod" or "dev," instead of version numbers. This simplifies function invocation and allows for seamless switching between versions during deployment.

Using aliases, you can easily update the function version your alias points to without changing any client code that invokes the function. This is particularly useful when you need to deploy a new version of your function to production without any downtime.

Creating and Managing Aliases

To create a new alias for your Lambda function, follow these steps:

  1. Navigate to the AWS Lambda console.
  2. Choose the function you want to create an alias for.
  3. In the Actions dropdown, select Create alias.
  4. Enter the alias name, description, and the version it should point to, then click Create.

You can now view and manage aliases in the Aliases tab of the Lambda console.

To update an existing alias to point to a new version, follow these steps:

  1. In the Lambda console, go to the Aliases tab.
  2. Locate the alias you want to update.
  3. Click on the alias name to open the alias configuration page.
  4. Under the Alias configuration section, click Edit.
  5. Update the version the alias points to and click Save.

To delete an alias:

  1. In the Lambda console, go to the Aliases tab.
  2. Locate the alias you want to delete.
  3. Select the checkbox next to the alias name.
  4. Click Delete from the Actions dropdown.

Using AWS CLI for Alias Management

In addition to using the AWS Lambda console, you can manage Lambda function aliases using the AWS CLI. This provides a programmatic way to create, update, and delete aliases, allowing for easier automation and integration with your existing workflows.

To create a new alias for your Lambda function using the AWS CLI, run the following command:

bash
$ aws lambda create-alias \
  --function-name YourFunctionName \
  --name YourAliasName \
  --function-version YourVersionNumber \
  --description "Your alias description"

This command creates a new alias for the specified Lambda function and returns a JSON object containing the new alias's details.

To update an existing alias to point to a new version, use the update-alias command:

bash
aws lambda update-alias --function-name YourFunctionName --name YourAliasName --function-version NewVersionNumber

This command updates the specified alias to point to the new version.

To list all aliases for a Lambda function, use the list-aliases command:

bash
$ aws lambda list-aliases --function-name YourFunctionName

This command returns a JSON object containing details of all aliases associated with the specified Lambda function.

To delete an alias using the AWS CLI, run:

bash
$ aws lambda delete-alias --function-name YourFunctionName --name YourAliasName

Use Cases for Lambda Aliases

Lambda aliases are useful in a variety of scenarios, including:

  • Simplifying deployments
    When deploying a new version of your Lambda function, you can update the alias to point to the new version without having to update client code that invokes the function.

  • Environment separation
    You can create separate aliases for different environments, such as development, staging, and production. This allows you to test new versions of your function in isolation before promoting them to the next environment.

  • A/B testing
    By using aliases, you can route a percentage of traffic to a new version of your function to test its performance or functionality before rolling it out to all users.

  • Rollbacks
    If an issue arises after deploying a new version, you can quickly revert the alias to point to the previous version, minimizing downtime and impact on users.

References

https://docs.aws.amazon.com/lambda/latest/dg/configuration-aliases.html
https://acloudguru.com/blog/engineering/aws-lambda-versioning-and-aliases
https://medium.com/@dalibor.plavcic/aws-lambda-versions-and-aliases-5f7a63a75afa

Ryusei Kakujo

researchgatelinkedingithub

Focusing on data science for mobility

Bench Press 100kg!