Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(lambda-python): add support for buildArgs #15306

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions packages/@aws-cdk/aws-lambda-python/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down
10 changes: 9 additions & 1 deletion packages/@aws-cdk/aws-lambda-python/lib/bundling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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,
Expand Down
8 changes: 8 additions & 0 deletions packages/@aws-cdk/aws-lambda-python/lib/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
}

/**
Expand Down Expand Up @@ -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}`,
});
Expand Down
8 changes: 8 additions & 0 deletions packages/@aws-cdk/aws-lambda-python/lib/layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
}

/**
Expand Down Expand Up @@ -50,6 +57,7 @@ export class PythonLayerVersion extends lambda.LayerVersion {
entry,
runtime,
outputPathSuffix: 'python',
buildArgs: props.buildArgs,
}),
});
}
Expand Down
31 changes: 29 additions & 2 deletions packages/@aws-cdk/aws-lambda-python/test/bundling.test.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -19,8 +18,17 @@ jest.mock('child_process', () => ({
}),
}));

// Mock DockerImage.fromAsset() to avoid building the image
let fromBuildMock: jest.SpyInstance<DockerImage>;
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', () => {
Expand Down Expand Up @@ -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',
}),
}));
});