AWS Lambda hook.
copydir(source, destination, includes, excludes=None, follow_symlinks=False)¶
Extend the functionality of shutil.
Correctly copies files and directories in a source directory.
Identify Python requirement files.
Assess if Docker should be used based on the value of args.
handle_requirements(package_root, dest_path, requirements, pipenv_timeout=300, python_path=None, use_pipenv=False)¶
Use the correct requirements file.
package_root (str) – Base directory containing a requirements file.
dest_path (str) – Where to output the requirements file if one needs to be created.
pipenv_timeout (int) – Seconds to wait for a subprocess to complete.
python_path (Optional[str]) – Explicit python interpreter to be used. Requirement file generators must be installed and executable using
use_pipenv (bool) – Explicitly use pipenv to export a Pipfile as requirements.txt.
Path to the final requirements.txt
- Return type
NotImplementedError – When a requirements file is not found. This should never be encountered but is included just in case.
dockerized_pip(work_dir, client=None, runtime=None, docker_file=None, docker_image=None, **_kwargs)¶
Run pip with docker.
work_dir (str) – Work directory for docker.
client (Optional[docker.DockerClient]) – Custom docker client.
runtime (Optional[str]) – Lambda runtime. Must provide one of
docker_file (Optional[str]) – Path to a Dockerfile to build an image. Must provide one of
docker_image (Optional[str]) – Local or remote docker image to use. Must provide one of
kwargs (Any) – Advanced options for docker. See source code to determine what is supported.
Content of the ZIP file as a byte string and calculated hash of all the files
- Return type
select_bucket_region(custom_bucket, hook_region, cfngin_bucket_region, provider_region)¶
Return the appropriate region to use when uploading functions.
Select the appropriate region for the bucket where lambdas are uploaded in.
custom_bucket (Optional[str]) – The custom bucket name provided by the bucket kwarg of the aws_lambda hook, if provided.
hook_region (str) – The contents of the bucket_region argument to the hook.
cfngin_bucket_region (str) – The contents of the
provider_region (str) – The region being used by the provider.
The appropriate region string.
- Return type
upload_lambda_functions(context, provider, **kwargs)¶
Build Lambda payloads from user configuration and upload them to S3.
Constructs ZIP archives containing files matching specified patterns for each function, uploads the result to Amazon S3, then stores objects (of type
troposphere.awslambda.Code) in the context’s hook data, ready to be referenced in blueprints.
Configuration consists of some global options, and a dictionary of function specifications. In the specifications, each key indicating the name of the function (used for generating names for artifacts), and the value determines what files to include in the ZIP (see more details below).
Payloads are uploaded to either a custom bucket or the CFNgin default bucket, with the key containing it’s checksum, to allow repeated uploads to be skipped in subsequent runs.
The configuration settings are documented as keyword arguments below.
- Keyword Arguments
bucket (Optional[str]) – Custom bucket to upload functions to. Omitting it will cause the default CFNgin bucket to be used.
bucket_region (Optional[str]) – The region in which the bucket should exist. If not given, the region will be either be that of the global
cfngin_bucket_regionsetting, or else the region in use by the provider.
prefix (Optional[str]) – S3 key prefix to prepend to the uploaded zip name.
follow_symlinks (Optional[bool]) – Will determine if symlinks should be followed and included with the zip artifact. (default:
payload_acl (Optional[str]) – The canned S3 object ACL to be applied to the uploaded payload. (default: private)
functions (Dict[str, Any]) –
Configurations of desired payloads to build. Keys correspond to function names, used to derive key names for the payload. Each value should itself be a dictionary, with the following data:
- docker_file (Optional[str])
Path to a local DockerFile that will be built and used for
dockerize_pip. Must provide exactly one of
- docker_image (Optional[str])
Custom Docker image to use with
dockerize_pip. Must provide exactly one of
- dockerize_pip (Optional[Union[str, bool]])
Whether to use Docker when preparing package dependencies with pip. Can be set to True/False or the special string ‘non-linux’ which will only run on non Linux systems. To use this option Docker must be installed.
- exclude (Optional[Union[str, List[str]]])
Pattern or list of patterns of files to exclude from the payload. If provided, any files that match will be ignored, regardless of whether they match an inclusion pattern.
Commonly ignored files are already excluded by default, such as
- include (Optional[Union[str, List[str]]])
Pattern or list of patterns of files to include in the payload. If provided, only files that match these patterns will be included in the payload.
Omitting it is equivalent to accepting all files that are not otherwise excluded.
- path (str)
Base directory of the Lambda function payload content. If it not an absolute path, it will be considered relative to the directory containing the CFNgin configuration file in use.
Files in this directory will be added to the payload ZIP, according to the include and exclude patterns. If no patterns are provided, all files in this directory (respecting default exclusions) will be used.
Files are stored in the archive with path names relative to this directory. So, for example, all the files contained directly under this directory will be added to the root of the ZIP file.
- pipenv_lock_timeout (Optional[int])
Time in seconds to wait while creating lock file with pipenv.
- pipenv_timeout (Optional[int])
Time in seconds to wait while running pipenv.
- python_path (Optional[str])
Absolute path to a python interpreter to use for
pipenvactions. If not provided, the current python interpreter will be used for
pipenvwill be used from the current
- runtime (Optional[str])
Runtime of the AWS Lambda Function being uploaded. Used with
dockerize_pipto automatically select the appropriate Docker image to use. Must provide exactly one of
- use_pipenv (Optional[bool])
Explicitly use Pipfile/Pipfile.lock to prepare package dependencies even if a requirements.txt file is found.
pre_build: upload_functions: path: runway.cfngin.hooks.aws_lambda.upload_lambda_functions required: true enabled: true data_key: lambda args: bucket: custom-bucket follow_symlinks: true prefix: cloudformation-custom-resources/ payload_acl: authenticated-read functions: MyFunction: path: ./lambda_functions dockerize_pip: non-linux use_pipenv: true runtime: python3.8 include: - '*.py' - '*.txt' exclude: - '*.pyc' - test/
from troposphere.awslambda import Function from runway.cfngin.blueprints.base import Blueprint class LambdaBlueprint(Blueprint): def create_template(self): code = self.context.hook_data['lambda']['MyFunction'] self.template.add_resource( Function( 'MyFunction', Code=code, Handler='my_function.handler', Role='...', Runtime='python2.7' ) )