diff --git a/packages/@aws-cdk/aws-lambda-python/README.md b/packages/@aws-cdk/aws-lambda-python/README.md index ffd19568aa5dc..d2428571c284d 100644 --- a/packages/@aws-cdk/aws-lambda-python/README.md +++ b/packages/@aws-cdk/aws-lambda-python/README.md @@ -36,6 +36,16 @@ new PythonFunction(this, 'MyFunction', { }); ``` +Additional `buildArgs` to use when bundling the Docker building image can be provided as: + +```ts +new PythonFunction(this, 'my-handler', { + buildArgs: { + HTTPS_PROXY: 'https://127.0.0.1:3001', + }, +}); +``` + All other properties of `lambda.Function` are supported, see also the [AWS Lambda construct library](https://github.com/aws/aws-cdk/tree/master/packages/%40aws-cdk/aws-lambda). ## Module Dependencies @@ -44,9 +54,9 @@ If `requirements.txt` or `Pipfile` exists at the entry path, the construct will all required modules in a [Lambda compatible Docker container](https://gallery.ecr.aws/sam/build-python3.7) according to the `runtime`. -Python bundles are only recreated and published when a file in a source directory has changed. +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. +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 has lockfile support. diff --git a/packages/@aws-cdk/aws-lambda-python/lib/bundling.ts b/packages/@aws-cdk/aws-lambda-python/lib/bundling.ts index 5a3228a78fef7..277fd6c0d5cd0 100644 --- a/packages/@aws-cdk/aws-lambda-python/lib/bundling.ts +++ b/packages/@aws-cdk/aws-lambda-python/lib/bundling.ts @@ -71,13 +71,20 @@ export interface BundlingOptions { * @default - based on `assetHashType` */ readonly assetHash?: string; + + /** + * Build arguments to pass when building the bundling image. + * + * @default - no build arguments are passed + */ + readonly buildArgs?: { [key: string]: string }; } /** * Produce bundled Lambda asset code */ export function bundle(options: BundlingOptions): lambda.Code { - const { entry, runtime, outputPathSuffix } = options; + const { entry, runtime, outputPathSuffix, buildArgs } = options; const stagedir = cdk.FileSystem.mkdtemp('python-bundling-'); const hasDeps = stageDependencies(entry, stagedir); @@ -100,6 +107,7 @@ export function bundle(options: BundlingOptions): lambda.Code { const image = cdk.DockerImage.fromBuild(stagedir, { buildArgs: { + ...buildArgs, IMAGE: runtime.bundlingDockerImage.image, }, file: dockerfile, diff --git a/packages/@aws-cdk/aws-lambda-python/lib/function.ts b/packages/@aws-cdk/aws-lambda-python/lib/function.ts index 733c115c0383d..7e14484c0bdd3 100644 --- a/packages/@aws-cdk/aws-lambda-python/lib/function.ts +++ b/packages/@aws-cdk/aws-lambda-python/lib/function.ts @@ -77,6 +77,13 @@ export interface PythonFunctionProps extends lambda.FunctionOptions { * @default - based on `assetHashType` */ readonly assetHash?: string; + + /** + * Build arguments to pass when building the bundling image. + * + * @default - no build arguments are passed + */ + readonly buildArgs?: { [key: string]: string }; } /** @@ -112,6 +119,7 @@ export class PythonFunction extends lambda.Function { outputPathSuffix: '.', assetHashType: props.assetHashType, assetHash: props.assetHash, + buildArgs: props.buildArgs, }), handler: `${index.slice(0, -3)}.${handler}`, }); diff --git a/packages/@aws-cdk/aws-lambda-python/lib/layer.ts b/packages/@aws-cdk/aws-lambda-python/lib/layer.ts index 1a9684e224580..d34ac13b723af 100644 --- a/packages/@aws-cdk/aws-lambda-python/lib/layer.ts +++ b/packages/@aws-cdk/aws-lambda-python/lib/layer.ts @@ -21,6 +21,13 @@ export interface PythonLayerVersionProps extends lambda.LayerVersionOptions { * @default - All runtimes are supported. */ readonly compatibleRuntimes?: lambda.Runtime[]; + + /** + * Build arguments to pass when building the bundling image. + * + * @default - no build arguments are passed + */ + readonly buildArgs?: { [key: string]: string }; } /** @@ -50,6 +57,7 @@ export class PythonLayerVersion extends lambda.LayerVersion { entry, runtime, outputPathSuffix: 'python', + buildArgs: props.buildArgs, }), }); } diff --git a/packages/@aws-cdk/aws-lambda-python/test/bundling.test.ts b/packages/@aws-cdk/aws-lambda-python/test/bundling.test.ts index d6de4ee23aedd..f00510e2844bd 100644 --- a/packages/@aws-cdk/aws-lambda-python/test/bundling.test.ts +++ b/packages/@aws-cdk/aws-lambda-python/test/bundling.test.ts @@ -1,11 +1,10 @@ import * as fs from 'fs'; import * as path from 'path'; import { Code, Runtime } from '@aws-cdk/aws-lambda'; -import { FileSystem } from '@aws-cdk/core'; +import { DockerImage, FileSystem } from '@aws-cdk/core'; import { stageDependencies, bundle } from '../lib/bundling'; jest.mock('@aws-cdk/aws-lambda'); - jest.mock('child_process', () => ({ spawnSync: jest.fn(() => { return { @@ -19,8 +18,17 @@ jest.mock('child_process', () => ({ }), })); +// Mock DockerImage.fromAsset() to avoid building the image +let fromBuildMock: jest.SpyInstance; beforeEach(() => { jest.clearAllMocks(); + + fromBuildMock = jest.spyOn(DockerImage, 'fromBuild').mockReturnValue({ + image: 'built-image', + cp: () => 'dest-path', + run: () => {}, + toJSON: () => 'built-image', + }); }); test('Bundling a function without dependencies', () => { @@ -139,3 +147,22 @@ describe('Dependency detection', () => { expect(stageDependencies(sourcedir, '/dummy')).toEqual(false); }); }); + +test('Bundling Docker with build args', () => { + const entry = path.join(__dirname, 'lambda-handler-nodeps'); + bundle({ + entry, + runtime: Runtime.PYTHON_3_7, + outputPathSuffix: '.', + buildArgs: { + HELLO: 'WORLD', + }, + }); + + expect(fromBuildMock).toHaveBeenCalledWith(expect.stringContaining('/tmp/python-bundling'), + expect.objectContaining({ + buildArgs: expect.objectContaining({ + HELLO: 'WORLD', + }), + })); +});