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): support build args (#12949) #13938

Closed
wants to merge 4 commits into from
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
15 changes: 15 additions & 0 deletions packages/@aws-cdk/aws-lambda-python/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ new PythonFunction(this, 'MyFunction', {
index: 'my_index.py', // optional, defaults to 'index.py'
handler: 'my_exported_func', // optional, defaults to 'handler'
runtime: lambda.Runtime.PYTHON_3_6, // optional, defaults to lambda.Runtime.PYTHON_3_7
build_args: {
HTTPS_PROXY: 'https://127.0.0.1:3001',
}, // optional, defaults to {}
});
```

Expand Down Expand Up @@ -86,3 +89,15 @@ new lambda.PythonFunction(this, 'MyFunction', {
],
});
```

**Lambda with custom Docker Build Time Variables**

Use the `buildArgs` parameter to pass build arguments when building the bundling image:

```ts
new lambda.PythonFunction(this, 'MyFunction', {
buildArgs: {
HTTPS_PROXY: 'https://127.0.0.1:3001',
},
});
```
8 changes: 8 additions & 0 deletions packages/@aws-cdk/aws-lambda-python/lib/bundling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ 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 };
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stylistic:

Suggested change
readonly buildArgs?: { [key:string] : string };
readonly buildArgs?: { [key:string]: string };

}

/**
Expand Down Expand Up @@ -100,6 +107,7 @@ export function bundle(options: BundlingOptions): lambda.Code {

const image = cdk.DockerImage.fromBuild(stagedir, {
buildArgs: {
...options.buildArgs ?? {},
IMAGE: runtime.bundlingDockerImage.image,
},
file: dockerfile,
Expand Down
9 changes: 9 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,14 @@ 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 +120,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
9 changes: 9 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,14 @@ 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 @@ -51,6 +59,7 @@ export class PythonLayerVersion extends lambda.LayerVersion {
entry,
runtime,
outputPathSuffix: 'python',
buildArgs: props.buildArgs,
}),
});
}
Expand Down
22 changes: 21 additions & 1 deletion packages/@aws-cdk/aws-lambda-python/test/bundling.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
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 { FileSystem, DockerImage } from '@aws-cdk/core';
import { stageDependencies, bundle } from '../lib/bundling';

jest.mock('@aws-cdk/aws-lambda');
Expand Down Expand Up @@ -139,3 +139,23 @@ describe('Dependency detection', () => {
expect(stageDependencies(sourcedir, '/dummy')).toEqual(false);
});
});

let fromAssetMock = jest.spyOn(DockerImage, 'fromBuild');
test('with Docker build args', () => {
const entry = path.join(__dirname, 'lambda-handler');

bundle({
entry,
runtime: Runtime.PYTHON_3_7,
buildArgs: {
HELLO: 'WORLD',
},
outputPathSuffix: 'python',
});

expect(fromAssetMock).toHaveBeenCalledWith(expect.stringMatching(/python-bundling-/), expect.objectContaining({
buildArgs: expect.objectContaining({
HELLO: 'WORLD',
}),
}));
});