runway.cfngin.hooks.awslambda.python_requirements package

Handle python requirements.

class runway.cfngin.hooks.awslambda.python_requirements.PythonDeploymentPackage[source]

Bases: runway.cfngin.hooks.awslambda.deployment_package.DeploymentPackage[PythonProject]

AWS Lambda Python Deployment Package.

property gitignore_filter: Optional[igittigitt.igittigitt.IgnoreParser]

Filter to use when zipping dependencies.

This should be overridden by subclasses if a filter should be used.

static insert_layer_dir(file_path: pathlib.Path, relative_to: pathlib.Path) pathlib.Path[source]

Insert python directory into local file path for layer archive.

Parameters
  • file_path – Path to local file.

  • relative_to – Path to a directory that the file_path will be relative to in the deployment package.

__init__(project: runway.cfngin.hooks.awslambda.deployment_package._ProjectTypeVar, usage_type: typing_extensions.Literal[function, layer] = 'function') None

Instantiate class.

The provided .init() class method should be used in place of direct instantiation.

Parameters
  • project – Project that is being built into a deployment package.

  • usage_type – How the deployment package can be used by AWS Lambda.

__new__(**kwargs)
property archive_file: Path

Path to archive file.

Because the archive file path contains runtime, it’s use can cause a race condition or recursion error if used in some locations. If we removed runtime from the path we would not have a way to track changes to runtime which is more important than needing to be mindful of where this is used.

property bucket: runway.core.providers.aws.s3._bucket.Bucket

AWS S3 bucket where deployment package will be uploaded.

build() Path

Build the deployment package.

build_tag_set(*, url_encoded: bool = True) Union[Dict[str, str], str]

Build tag set to be applied to the S3 object.

Parameters
  • layer – Tag the deployment package as a Lambda Layer or not.

  • url_encoded – Whether to return a dict or URL encoded query string.

property code_sha256: str

SHA256 of the archive file.

Returns

Value to pass to CloudFormation AWS::Lambda::Version.CodeSha256.

Raises

FileNotFoundError – Property accessed before archive file has been built.

property compatible_architectures: Optional[List[str]]

List of compatible instruction set architectures.

property compatible_runtimes: Optional[List[str]]

List of compatible runtimes.

delete() None

Delete deployment package.

property exists: bool

Whether the deployment package exists.

classmethod init(project: runway.cfngin.hooks.awslambda.deployment_package._ProjectTypeVar, usage_type: typing_extensions.Literal[function, layer] = 'function') runway.cfngin.hooks.awslambda.deployment_package.DeploymentPackage[runway.cfngin.hooks.awslambda.deployment_package._ProjectTypeVar]

Initialize deployment package.

This should be used in place of creating an instance of this class directly as it will automatically account for the S3 object already existing.

Parameters
  • project – Project that is being built into a deployment package.

  • usage_type – How the deployment package can be used by AWS Lambda.

Returns

Instance of generic S3 object class if S3 object exists else an instance of this class.

iterate_dependency_directory() Iterator[Path]

Iterate over the contents of the dependency directory.

If gitignore_filter is set, it will be used to exclude files.

property license: Optional[str]

Software license for the project.

property md5_checksum: str

MD5 of the archive file.

Returns

Value to pass as ContentMD5 when uploading to AWS S3.

Raises

FileNotFoundError – Property accessed before archive file has been built.

property object_key: str

Key to use when upload object to AWS S3.

property object_version_id: Optional[str]

S3 object version ID.

Returns

The ID of the current object version. This will only have a value if versioning is enabled on the bucket.

property runtime: str

Runtime of the deployment package.

upload(*, build: bool = True) None

Upload deployment package.

Parameters

build – If true, the deployment package will be built before before trying to upload it. If false, it must have already been built.

usage_type: Literal['function', 'layer']

How the deployment package can be used by AWS Lambda.

class runway.cfngin.hooks.awslambda.python_requirements.PythonDockerDependencyInstaller[source]

Bases: runway.cfngin.hooks.awslambda.docker.DockerDependencyInstaller

Docker dependency installer for Python.

__init__(project: PythonProject, *, client: Optional[DockerClient] = None, context: Optional[Union[CfnginContext, RunwayContext]] = None) None[source]

Instantiate class.

Parameters
  • project – awslambda project.

  • client – Pre-configured docker.client.DockerClient.

  • context – CFNgin or Runway context object.

property bind_mounts: List[docker.types.services.Mount]

Bind mounts that will be used by the container.

property environment_variables: Dict[str, str]

Environment variables to pass to the docker container.

This is a subset of the environment variables stored in the context object as some will cause issues if they are passed.

property install_commands: List[str]

Commands to run to install dependencies.

property python_version: Optional[runway.utils._version.Version]

Version of Python installed in the docker container.

property runtime: Optional[str]

AWS Lambda runtime determined from the docker container’s Python version.

__new__(**kwargs)
build_image(docker_file: Path, *, name: Optional[str] = None, tag: Optional[str] = None) Image

Build Docker image from Dockerfile.

This method is exposed as a low-level interface. image should be used in place for this for most cases.

Parameters
  • docker_file – Path to the Dockerfile to build. This path should be absolute, must exist, and must be a file.

  • name – Name of the Docker image. The name should not contain a tag. If not provided, a default value is use.

  • tag – Tag to apply to the image after it is built. If not provided, a default value of latest is used.

Returns

Object representing the image that was built.

classmethod from_project(project: Project[AwsLambdaHookArgs]) Optional[_T]

Instantiate class from a project.

High-level method that wraps instantiation in error handling.

Parameters

project – Project being processed.

Returns

Object to handle dependency installation with Docker if Docker is available and not disabled.

Raises

DockerConnectionRefused – Docker is not install or is unreachable.

property image: Union[docker.models.images.Image, str]

Docker image that will be used.

Raises

ValueError – Insufficient data to determine the desired Docker image.

install() None

Install dependencies using Docker.

Commands are run as they are defined in the following cached properties:

log_docker_msg_bytes(stream: Iterator[bytes], *, level: int = 20) List[str]

Log Docker output message from blocking generator that return bytes.

Parameters
  • stream – Blocking generator that returns log messages as bytes.

  • level – Log level to use when logging messages.

Returns

List of log messages.

log_docker_msg_dict(stream: Iterator[Dict[str, Any]], *, level: int = 20) List[str]

Log Docker output message from blocking generator that return dict.

Parameters
  • stream – Blocking generator that returns log messages as a dict.

  • level – Log level to use when logging messages.

Returns

list of log messages.

property post_install_commands: List[str]

Commands to run after dependencies have been installed.

property pre_install_commands: List[str]

Commands to run before dependencies have been installed.

pull_image(name: str, *, force: bool = True) docker.models.images.Image

Pull a Docker image from a repository if it does not exist locally.

This method is exposed as a low-level interface. image should be used in place for this for most cases.

Parameters
  • name – Name of the Docker image including tag.

  • force – Always pull the image even if it exists locally. This will ensure that the latest version is always used.

Returns

Object representing the image found locally or pulled from a repository.

run_command(command: str, *, level: int = 20) List[str]

Execute equivalent of docker container run.

Parameters
  • command – Command to be run.

  • level – Log level to use when logging messages.

Raises

DockerExecFailedError – Docker container returned a non-zero exit code.

Returns

List of log messages.

client: DockerClient

Docker client.

ctx: Union[CfnginContext, RunwayContext]

Context object.

options: DockerOptions

Hook arguments specific to Docker.

class runway.cfngin.hooks.awslambda.python_requirements.PythonProject[source]

Bases: runway.cfngin.hooks.awslambda.base_classes.Project[runway.cfngin.hooks.awslambda.models.args.PythonHookArgs]

Python project.

DEFAULT_CACHE_DIR_NAME: ClassVar[str] = 'pip_cache'

Name of the default cache directory.

property docker: Optional[runway.cfngin.hooks.awslambda.python_requirements._docker.PythonDockerDependencyInstaller]

Docker interface that can be used to build the project.

property metadata_files: Tuple[Path, ...]

Project metadata files.

Files are only included in return value if they exist.

property runtime: str

Runtime of the build system.

Value should be a valid Lambda Function runtime (https://docs.aws.amazon.com/lambda/latest/dg/lambda-runtimes.html).

property pip: runway.dependency_managers._pip.Pip

Pip dependency manager.

property pipenv: Optional[runway.dependency_managers._pipenv.Pipenv]

Pipenv dependency manager.

Returns

If the project uses pipenv and pipenv is not explicitly disabled, an object for interfacing with pipenv will be returned.

Raises

PipenvNotFoundError – pipenv is not installed or not found in PATH.

property poetry: Optional[runway.dependency_managers._poetry.Poetry]

Poetry dependency manager.

Returns

If the project uses poetry and poetry is not explicitly disabled, an object for interfacing with poetry will be returned.

Raises

PoetryNotFound – poetry is not installed or not found in PATH.

property project_type: typing_extensions.Literal[pip, pipenv, poetry]

Type of python project.

property requirements_txt: Optional[Path]

Dependency file for the project.

property supported_metadata_files: Set[str]

Names of all supported metadata files.

Returns

Set of file names - not paths.

property tmp_requirements_txt: Path

Temporary requirements.txt file.

This path is only used when exporting from another format.

cleanup() None[source]

Cleanup temporary files after the build process has run.

__init__(args: runway.cfngin.hooks.awslambda.base_classes._AwsLambdaHookArgsTypeVar_co, context: runway.context.CfnginContext) None

Instantiate class.

Parameters
  • args – Parsed hook arguments.

  • context – Context object.

__new__(**kwargs)
property build_directory: pathlib.Path

Directory being used to build deployment package.

property cache_dir: Optional[pathlib.Path]

Directory where a dependency manager’s cache data will be stored.

Returns

Explicit cache directory if provided or default cache directory if it is not provided. If configured to not use cache, will always be None.

cleanup_on_error() None

Cleanup project files when an error occurs.

This will be run before self.cleanup() if an error has occurred.

Hooks should call this method in an except block and reraise the error afterward.

property compatible_architectures: Optional[List[str]]

List of compatible instruction set architectures.

property compatible_runtimes: Optional[List[str]]

List of compatible runtimes.

Value should be valid Lambda Function runtimes (https://docs.aws.amazon.com/lambda/latest/dg/lambda-runtimes.html).

Raises

ValueError – Defined or detected runtime is not in the list of compatible runtimes.

property dependency_directory: pathlib.Path

Directory to use as the target of pip install --target.

install_dependencies() None[source]

Install project dependencies.

property license: Optional[str]

Software license for the project.

Can be any of the following:

  • A SPDX license identifier (e.g. MIT).

  • The URL of a license hosted on the internet (e.g. https://opensource.org/licenses/MIT).

  • The full text of the license.

property project_root: pathlib.Path

Root directory of the project.

The top-level directory containing the source code and all configuration/metadata files (e.g. pyproject.toml, package.json).

The project root can be different from the source code directory but, if they are different, the project root should contain the source code directory. If it does not, the source code directory will be always be used.

The primary use case for this property is to allow configuration files to exist outside of the source code directory. The project_type can and should rely on the value of this property when determining the type.

property source_code: runway.cfngin.hooks.awslambda.source_code.SourceCode

Project source code.

Lazy load source code object. Extends gitignore as needed.

args: _AwsLambdaHookArgsTypeVar_co

Parsed hook arguments.

ctx: CfnginContext

CFNgin context object.