aws_lambda_version_and_alias#

AWS Lambda version and alias management helper functions.

Requirements:

boto3

Optional Requirements:

boto3-stubs[lambda]

Usage example:

from aws_lambda_version_and_alias import (
    LATEST,
    list_versions_by_function,
    version_dct_to_version_int,
    get_last_published_version,
    publish_version,
    keep_n_most_recent_versions,
    deploy_alias,
    delete_alias,
)

This module is originally from https://github.com/MacHu-GWU/fixa-project/blob/main/fixa/aws/aws_lambda_version_and_alias.py

aws_ops_alpha.vendor.aws_lambda_version_and_alias.list_versions_by_function(lbd_client: LambdaClient, func_name: str, max_items: int = 9999) List[dict][source]#

List all lambda function versions. Return a list of detail dict.

aws_ops_alpha.vendor.aws_lambda_version_and_alias.version_dct_to_version_int(versions: List[dict]) List[int][source]#

Convert a list of lambda function version detail dict to a list of version number. The $LATEST version is not included.

aws_ops_alpha.vendor.aws_lambda_version_and_alias.get_last_published_version(lbd_client: LambdaClient, func_name: str, max_items: int = 9999) Optional[int][source]#

Get the last published version number. If there’s no published version, return None.

aws_ops_alpha.vendor.aws_lambda_version_and_alias.publish_version(lbd_client: LambdaClient, func_name: str) Tuple[bool, int][source]#

Publish a new version. The AWS official doc says that: Lambda doesn’t publish a version if the function’s configuration and code haven’t changed since the last version.

Reference:

Returns:

a tuple of two items, first item is a boolean flag to indicate that if a new version is created. the second item is the version id. if there’s a new version is created, return the new version, otherwise, return the latest version number.

aws_ops_alpha.vendor.aws_lambda_version_and_alias.keep_n_most_recent_versions(lbd_client: LambdaClient, func_name: str, n: int, max_items: int = 9999) List[int][source]#

Only keep the most recent n versions, delete the rest of published versions. If a version is associated with an alias, it will not be deleted.

aws_ops_alpha.vendor.aws_lambda_version_and_alias.deploy_alias(lbd_client: LambdaClient, func_name: str, alias: str, description: Optional[str] = None, version1: Optional[Union[str, int]] = None, version2: Optional[Union[str, int]] = None, version2_percentage: Optional[float] = None) Tuple[bool, Optional[str]][source]#

Point the alias to the given version or split traffic between two versions.

Parameters:
  • bsm – boto session manager object

  • func_name – lambda function name

  • alias – alias name

  • description – description of the alias

  • version1 – the main version of the alias; if not specified, use $LATEST

  • version2 – the secondary version of the alias; if not specified, then the version1 will have 100% traffic; if specified, then version2_percentage also has to be specified.

  • version2_percentage – if version2 is specified, then it has to be a value between 0.01 and 0.99.

Returns:

a tuple of two items; first item is a boolean flag to indicate whether a creation or update is performed; second item is the alias revision id, if creation or update is not performed, then return None.

Reference:

aws_ops_alpha.vendor.aws_lambda_version_and_alias.delete_alias(lbd_client: LambdaClient, func_name: str, alias: str) dict[source]#

The original API is already idempotent, so no need to check if the alias exists.