aws_chalice_helpers#

This module implements the automation for AWS Chalice framework.

aws_ops_alpha.aws_helpers.aws_chalice_helpers.build_lambda_source_chalice_vendor(pyproject_ops: pyops.PyProjectOps)[source]#

Copy the Lambda source code Python library from ${dir_project_root}/${package_name} to ${dir_project_root}/lambda_app/vendor/${package_name}.

It also removes the __pycache__ directory and the .pyc, .pyo files during the copy.

Parameters:

pyproject_opsPyProjectOps object.

aws_ops_alpha.aws_helpers.aws_chalice_helpers.get_source_sha256(pyproject_ops: pyops.PyProjectOps) str[source]#

The chalice deploy command is an expensive operation, even when there is no change in the source code.

During the initial chalice deploy, we calculate the SHA256 hash of the related source code and store it in the metadata of the deployed JSON file in S3.

Subsequent chalice deploy operations involve comparing the SHA256 hash of the source code with the one stored in the S3 metadata. If the two hashes are the same, we skip the chalice deploy operation.

The SHA256 hash is calculated from the following files (order does matter):

  • lambda_app/.chalice/config.json

  • lambda_app/app.py

  • lambda_app/vendor/${package_name}

Parameters:

pyproject_opsPyProjectOps object.

Returns:

a sha256 hash value represent the local lambda source code

aws_ops_alpha.aws_helpers.aws_chalice_helpers.is_current_lambda_code_the_same_as_deployed_one(bsm_devops: BotoSesManager, s3path_deployed_json: S3Path, source_sha256: str) bool[source]#

Compare the local chalice app source code hash with the deployed one.

Parameters:
  • env_name – the environment name

  • bsm_devops – the devops AWS Account BotoSesManager object.

  • s3path_deployed_json – the S3 path to the deployed ${env_name}.json file.

  • source_sha256 – a sha256 hash value represent the local lambda source code

Returns:

a boolean flag to indicate that if the local lambda source code is the same as the deployed one.

aws_ops_alpha.aws_helpers.aws_chalice_helpers.get_concurrency_lock(vault: Vault, owner: str, bsm_devops: BotoSesManager) Optional[Lock][source]#

Get the concurrency lock.

Returns:

True if got the lock, False if not

aws_ops_alpha.aws_helpers.aws_chalice_helpers.download_deployed_json(env_name: str, bsm_devops: BotoSesManager, pyproject_ops: pyops.PyProjectOps, s3path_deployed_json: S3Path) bool[source]#

AWS Chalice utilizes a deployed/${env_name}.json JSON file to store the deployed resource information.

Since this file is generated on the fly based on the project config file, it cannot be stored in the Git repository. A better approach is to use S3 as the centralized storage for this file. Whenever we perform a new chalice deploy operation, we attempt to download the latest deployed JSON file from S3, carry out the deployment, and then upload the updated JSON file back to S3.

Naturally, we employ a concurrency lock mechanism to prevent competition.

Parameters:
  • env_name – the environment name

  • bsm_devops – the devops AWS Account BotoSesManager object.

  • pyproject_opsPyProjectOps object.

  • s3path_deployed_json – the S3 path to the deployed ${env_name}.json file.

Returns:

a boolean flag to indicate that if the deployed JSON exists on S3

aws_ops_alpha.aws_helpers.aws_chalice_helpers.upload_deployed_json(env_name: str, bsm_devops: BotoSesManager, pyproject_ops: pyops.PyProjectOps, s3path_deployed_json: S3Path, source_sha256: Optional[str] = None, tags: Optional[Dict[str, str]] = None) bool[source]#

After chalice deploy succeeded, upload the .chalice/deployed/${env_name}.json file from local to s3. It will generate two files:

  1. ${s3dir_artifacts}/lambda/deployed/${env_name}.json, this file will

    be overwritten over the time.

  2. ${s3dir_artifacts}/lambda/deployed/${env_name}-${datetime}.json, this

    file will stay forever as a backup

Parameters:
  • env_name – env name, will be used for conditional step test.

  • bsm_devops – the devops AWS Account BotoSesManager object.

  • pyproject_opsPyProjectOps object.

  • s3path_deployed_json – the S3 path to the deployed ${env_name}.json file.

  • source_sha256 – a sha256 hash value represent the lambda source code digest. if not provided, it will be calculated from the source code.

  • tags – optional AWS resource tags.

Returns:

a tuple of the s3 path of the deployed json file and a boolean flag to indicate that if the uploaded happen

aws_ops_alpha.aws_helpers.aws_chalice_helpers.run_chalice_command(env_name: str, command: str, bsm_devops: BotoSesManager, bsm_workload: BotoSesManager, pyproject_ops: pyops.PyProjectOps, path_bsm_devops_snapshot: Path = PosixPath('/home/docs/.bsm-snapshot.json')) CompletedProcess[source]#

Run chalice deploy or chalice delete command to deploy / delete the lambda function and API Gateway.

aws_ops_alpha.aws_helpers.aws_chalice_helpers.run_update_chalice_config_script(pyproject_ops: pyops.PyProjectOps)[source]#

Run the following command to generate .chalice/config.json file.

./.venv/bin/python lambda_app/update_chalice_config.py