Skip to content

Commit

Permalink
fix(lambda-python): runtime is now required (#18143)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Runtime is now required for `LambdaPython`

fixes #10248

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
peterwoodworth authored Dec 23, 2021
1 parent aa7c160 commit 98f1bb1
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
3 changes: 2 additions & 1 deletion packages/@aws-cdk/aws-lambda-python/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ Define a `PythonFunction`:
```ts
new lambda.PythonFunction(this, 'MyFunction', {
entry: '/path/to/my/function', // required
runtime: Runtime.PYTHON_3_6, // required
index: 'my_index.py', // optional, defaults to 'index.py'
handler: 'my_exported_func', // optional, defaults to 'handler'
runtime: Runtime.PYTHON_3_6, // optional, defaults to lambda.Runtime.PYTHON_3_7
});
```

Expand Down Expand Up @@ -86,6 +86,7 @@ layer.
```ts
new lambda.PythonFunction(this, 'MyFunction', {
entry: '/path/to/my/function',
runtime: Runtime.PYTHON_3_6,
layers: [
new lambda.PythonLayerVersion(this, 'MyLayer', {
entry: '/path/to/my/layer', // point this to your library's directory
Expand Down
4 changes: 1 addition & 3 deletions packages/@aws-cdk/aws-lambda-python/lib/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,8 @@ export interface PythonFunctionProps extends lambda.FunctionOptions {
/**
* The runtime environment. Only runtimes of the Python family are
* supported.
*
* @default lambda.Runtime.PYTHON_3_7
*/
readonly runtime?: lambda.Runtime;
readonly runtime: lambda.Runtime;

/**
* Determines how asset hash is calculated. Assets will get rebuild and
Expand Down
8 changes: 8 additions & 0 deletions packages/@aws-cdk/aws-lambda-python/test/function.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ beforeEach(() => {
test('PythonFunction with defaults', () => {
new PythonFunction(stack, 'handler', {
entry: 'test/lambda-handler',
runtime: Runtime.PYTHON_3_8,
});

expect(bundle).toHaveBeenCalledWith(expect.objectContaining({
Expand All @@ -62,6 +63,7 @@ test('PythonFunction with index in a subdirectory', () => {
entry: 'test/lambda-handler-sub',
index: 'inner/custom_index.py',
handler: 'custom_handler',
runtime: Runtime.PYTHON_3_8,
});

expect(bundle).toHaveBeenCalledWith(expect.objectContaining({
Expand All @@ -78,12 +80,14 @@ test('throws when index is not py', () => {
expect(() => new PythonFunction(stack, 'Fn', {
entry: 'test/lambda-handler',
index: 'index.js',
runtime: Runtime.PYTHON_3_8,
})).toThrow(/Only Python \(\.py\) index files are supported/);
});

test('throws when entry does not exist', () => {
expect(() => new PythonFunction(stack, 'Fn', {
entry: 'notfound',
runtime: Runtime.PYTHON_3_8,
})).toThrow(/Cannot find index file at/);
});

Expand All @@ -99,27 +103,31 @@ test('allows specifying hash type', () => {
entry: 'test/lambda-handler-nodeps',
index: 'index.py',
handler: 'handler',
runtime: Runtime.PYTHON_3_8,
});

new PythonFunction(stack, 'source2', {
entry: 'test/lambda-handler-nodeps',
index: 'index.py',
handler: 'handler',
assetHashType: AssetHashType.SOURCE,
runtime: Runtime.PYTHON_3_8,
});

new PythonFunction(stack, 'output', {
entry: 'test/lambda-handler-nodeps',
index: 'index.py',
handler: 'handler',
assetHashType: AssetHashType.OUTPUT,
runtime: Runtime.PYTHON_3_8,
});

new PythonFunction(stack, 'custom', {
entry: 'test/lambda-handler-nodeps',
index: 'index.py',
handler: 'handler',
assetHash: 'MY_CUSTOM_HASH',
runtime: Runtime.PYTHON_3_8,
});

Template.fromStack(stack).hasResourceProperties('AWS::Lambda::Function', {
Expand Down

0 comments on commit 98f1bb1

Please sign in to comment.