package
1.204.0-devpreview
Repository: https://github.com/aws/aws-cdk-go.git
Documentation: pkg.go.dev

# README

Amazon Lambda Python Library

This library provides constructs for Python Lambda functions.

To use this module, you will need to have Docker installed.

Python Function

Define a PythonFunction:

lambda.NewPythonFunction(this, jsii.String("MyFunction"), &PythonFunctionProps{
	Entry: jsii.String("/path/to/my/function"),
	 // required
	Runtime: awscdk.Runtime_PYTHON_3_8(),
	 // required
	Index: jsii.String("my_index.py"),
	 // optional, defaults to 'index.py'
	Handler: jsii.String("my_exported_func"),
})

All other properties of lambda.Function are supported, see also the AWS Lambda construct library.

Python Layer

You may create a python-based lambda layer with PythonLayerVersion. If PythonLayerVersion detects a requirements.txt or Pipfile or poetry.lock with the associated pyproject.toml at the entry path, then PythonLayerVersion will include the dependencies inline with your code in the layer.

Define a PythonLayerVersion:

lambda.NewPythonLayerVersion(this, jsii.String("MyLayer"), &PythonLayerVersionProps{
	Entry: jsii.String("/path/to/my/layer"),
})

A layer can also be used as a part of a PythonFunction:

lambda.NewPythonFunction(this, jsii.String("MyFunction"), &PythonFunctionProps{
	Entry: jsii.String("/path/to/my/function"),
	Runtime: awscdk.Runtime_PYTHON_3_8(),
	Layers: []iLayerVersion{
		lambda.NewPythonLayerVersion(this, jsii.String("MyLayer"), &PythonLayerVersionProps{
			Entry: jsii.String("/path/to/my/layer"),
		}),
	},
})

Packaging

If requirements.txt, Pipfile or poetry.lock exists at the entry path, the construct will handle installing all required modules in a Lambda compatible Docker container according to the runtime and with the Docker platform based on the target architecture of the Lambda function.

Python bundles are only recreated and published when a file in a source directory has changed. Therefore (and as a general best-practice), it is highly recommended to commit a lockfile with a list of all transitive dependencies and their exact versions. This will ensure that when any dependency version is updated, the bundle asset is recreated and uploaded.

To that end, we recommend using [pipenv] or [poetry] which have lockfile support.

Packaging is executed using the Packaging class, which:

  1. Infers the packaging type based on the files present.
  2. If it sees a Pipfile or a poetry.lock file, it exports it to a compatible requirements.txt file with credentials (if they're available in the source files or in the bundling container).
  3. Installs dependencies using pip.
  4. Copies the dependencies into an asset that is bundled for the Lambda package.

Lambda with a requirements.txt

.
├── lambda_function.py # exports a function named 'handler'
├── requirements.txt # has to be present at the entry path

Lambda with a Pipfile

.
├── lambda_function.py # exports a function named 'handler'
├── Pipfile # has to be present at the entry path
├── Pipfile.lock # your lock file

Lambda with a poetry.lock

.
├── lambda_function.py # exports a function named 'handler'
├── pyproject.toml # your poetry project definition
├── poetry.lock # your poetry lock file has to be present at the entry path

Custom Bundling

Custom bundling can be performed by passing in additional build arguments that point to index URLs to private repos, or by using an entirely custom Docker images for bundling dependencies. The build args currently supported are:

  • PIP_INDEX_URL
  • PIP_EXTRA_INDEX_URL
  • HTTPS_PROXY

Additional build args for bundling that refer to PyPI indexes can be specified as:

entry := "/path/to/function"
image := awscdk.DockerImage_FromBuild(entry)

lambda.NewPythonFunction(this, jsii.String("function"), &PythonFunctionProps{
	Entry: jsii.String(Entry),
	Runtime: awscdk.Runtime_PYTHON_3_8(),
	Bundling: &BundlingOptions{
		BuildArgs: map[string]*string{
			"PIP_INDEX_URL": jsii.String("https://your.index.url/simple/"),
			"PIP_EXTRA_INDEX_URL": jsii.String("https://your.extra-index.url/simple/"),
		},
	},
})

If using a custom Docker image for bundling, the dependencies are installed with pip, pipenv or poetry by using the Packaging class. A different bundling Docker image that is in the same directory as the function can be specified as:

entry := "/path/to/function"
image := awscdk.DockerImage_FromBuild(entry)

lambda.NewPythonFunction(this, jsii.String("function"), &PythonFunctionProps{
	Entry: jsii.String(Entry),
	Runtime: awscdk.Runtime_PYTHON_3_8(),
	Bundling: &BundlingOptions{
		Image: *Image,
	},
})

Custom Bundling with Code Artifact

To use a Code Artifact PyPI repo, the PIP_INDEX_URL for bundling the function can be customized (requires AWS CLI in the build environment):

import "github.com/aws-samples/dummy/child_process"


entry := "/path/to/function"
image := awscdk.DockerImage_FromBuild(entry)

domain := "my-domain"
domainOwner := "111122223333"
repoName := "my_repo"
region := "us-east-1"
codeArtifactAuthToken := child_process.ExecSync(fmt.Sprintf("aws codeartifact get-authorization-token --domain %v --domain-owner %v --query authorizationToken --output text", domain, domainOwner)).toString().trim()

indexUrl := fmt.Sprintf("https://aws:%v@%v-%v.d.codeartifact.%v.amazonaws.com/pypi/%v/simple/", codeArtifactAuthToken, domain, domainOwner, region, repoName)

lambda.NewPythonFunction(this, jsii.String("function"), &PythonFunctionProps{
	Entry: jsii.String(Entry),
	Runtime: awscdk.Runtime_PYTHON_3_8(),
	Bundling: &BundlingOptions{
		Environment: map[string]*string{
			"PIP_INDEX_URL": indexUrl,
		},
	},
})

The index URL or the token are only used during bundling and thus not included in the final asset. Setting only environment variable for PIP_INDEX_URL or PIP_EXTRA_INDEX_URL should work for accesing private Python repositories with pip, pipenv and poetry based dependencies.

If you also want to use the Code Artifact repo for building the base Docker image for bundling, use buildArgs. However, note that setting custom build args for bundling will force the base bundling image to be rebuilt every time (i.e. skip the Docker cache). Build args can be customized as:

import "github.com/aws-samples/dummy/child_process"


entry := "/path/to/function"
image := awscdk.DockerImage_FromBuild(entry)

domain := "my-domain"
domainOwner := "111122223333"
repoName := "my_repo"
region := "us-east-1"
codeArtifactAuthToken := child_process.ExecSync(fmt.Sprintf("aws codeartifact get-authorization-token --domain %v --domain-owner %v --query authorizationToken --output text", domain, domainOwner)).toString().trim()

indexUrl := fmt.Sprintf("https://aws:%v@%v-%v.d.codeartifact.%v.amazonaws.com/pypi/%v/simple/", codeArtifactAuthToken, domain, domainOwner, region, repoName)

lambda.NewPythonFunction(this, jsii.String("function"), &PythonFunctionProps{
	Entry: jsii.String(Entry),
	Runtime: awscdk.Runtime_PYTHON_3_8(),
	Bundling: &BundlingOptions{
		BuildArgs: map[string]*string{
			"PIP_INDEX_URL": indexUrl,
		},
	},
})

# Functions

Experimental.
Experimental.
Experimental.
Experimental.
Record whether specific properties in the `AWS::Lambda::Function` resource should also be associated to the Version resource.
Import a lambda function into the CDK using its ARN.
Creates a Lambda function object which represents a function not defined within this stack.
Import a lambda function into the CDK using its name.
Return whether the given object is a Construct.
Check whether the given construct is a Resource.
Return the given named metric for this Lambda.
Metric for the number of concurrent executions across all Lambdas.
Metric for the Duration executing all Lambdas.
Metric for the number of Errors executing all Lambdas.
Metric for the number of invocations of all Lambdas.
Metric for the number of throttled invocations of all Lambdas.
Metric for the number of unreserved concurrent executions across all Lambdas.
Imports a layer version by ARN.
Imports a Layer that has been defined externally.
Return whether the given object is a Construct.
Check whether the given construct is a Resource.

# Structs

Options for bundling.
Properties for a PythonFunction.
Properties for PythonLayerVersion.

# Interfaces

A Python Lambda function.
A lambda layer version.