Skip to content

Commit

Permalink
do not build docker image if stack does not require bundling
Browse files Browse the repository at this point in the history
  • Loading branch information
jihndai committed Jan 28, 2022
1 parent 6416349 commit 9879338
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 1 deletion.
9 changes: 8 additions & 1 deletion packages/@aws-cdk/aws-lambda-python/lib/bundling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ export interface BundlingProps extends BundlingOptions {
* @default Architecture.X86_64
*/
readonly architecture?: Architecture;

/**
* Whether or not the bundling process should be skipped
*
* @default - Does not skip bundling
*/
readonly skip?: boolean;
}

/**
Expand All @@ -45,7 +52,7 @@ export class Bundling implements CdkBundlingOptions {
assetHash: options.assetHash,
assetHashType: options.assetHashType,
exclude: DEPENDENCY_EXCLUDES,
bundling: new Bundling(options),
bundling: options.skip ? undefined : new Bundling(options),
});
}

Expand Down
2 changes: 2 additions & 0 deletions packages/@aws-cdk/aws-lambda-python/lib/function.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as fs from 'fs';
import * as path from 'path';
import { Function, FunctionOptions, Runtime, RuntimeFamily } from '@aws-cdk/aws-lambda';
import { Stack } from '@aws-cdk/core';
import { Bundling } from './bundling';
import { BundlingOptions } from './types';

Expand Down Expand Up @@ -79,6 +80,7 @@ export class PythonFunction extends Function {
code: Bundling.bundle({
entry,
runtime,
skip: !Stack.of(scope).bundlingRequired,
...props.bundling,
}),
handler: resolvedHandler,
Expand Down
2 changes: 2 additions & 0 deletions packages/@aws-cdk/aws-lambda-python/lib/layer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as path from 'path';
import * as lambda from '@aws-cdk/aws-lambda';
import { Stack } from '@aws-cdk/core';
import { Bundling } from './bundling';
import { BundlingOptions } from './types';

Expand Down Expand Up @@ -67,6 +68,7 @@ export class PythonLayerVersion extends lambda.LayerVersion {
runtime,
architecture,
outputPathSuffix: 'python',
skip: !Stack.of(scope).bundlingRequired,
...props.bundling,
}),
});
Expand Down
22 changes: 22 additions & 0 deletions packages/@aws-cdk/aws-lambda-python/test/bundling.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,3 +229,25 @@ test('Bundling with custom build args', () => {
}),
}));
});

test('Do not build docker image when skipping bundling', () => {
const entry = path.join(__dirname, 'lambda-handler');
Bundling.bundle({
entry: entry,
runtime: Runtime.PYTHON_3_7,
skip: true,
});

expect(DockerImage.fromBuild).not.toHaveBeenCalled();
});

test('Build docker image when bundling is not skipped', () => {
const entry = path.join(__dirname, 'lambda-handler');
Bundling.bundle({
entry: entry,
runtime: Runtime.PYTHON_3_7,
skip: false,
});

expect(DockerImage.fromBuild).toHaveBeenCalled();
});
32 changes: 32 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 @@ -167,3 +167,35 @@ test('Allows use of custom bundling image', () => {
image,
}));
});

test('Skip bundling when stack does not require it', () => {
const spy = jest.spyOn(stack, 'bundlingRequired', 'get').mockReturnValue(false);
const entry = path.join(__dirname, 'lambda-handler');

new PythonFunction(stack, 'function', {
entry,
runtime: Runtime.PYTHON_3_8,
});

expect(Bundling.bundle).toHaveBeenCalledWith(expect.objectContaining({
skip: true,
}));

spy.mockRestore();
});

test('Do not skip bundling when stack requires it', () => {
const spy = jest.spyOn(stack, 'bundlingRequired', 'get').mockReturnValue(true);
const entry = path.join(__dirname, 'lambda-handler');

new PythonFunction(stack, 'function', {
entry,
runtime: Runtime.PYTHON_3_8,
});

expect(Bundling.bundle).toHaveBeenCalledWith(expect.objectContaining({
skip: false,
}));

spy.mockRestore();
});
30 changes: 30 additions & 0 deletions packages/@aws-cdk/aws-lambda-python/test/layer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,33 @@ test('Allows use of custom bundling image', () => {
image,
}));
});

test('Skip bundling when stack does not require it', () => {
const spy = jest.spyOn(stack, 'bundlingRequired', 'get').mockReturnValue(false);
const entry = path.join(__dirname, 'lambda-handler-project');

new PythonLayerVersion(stack, 'layer', {
entry,
});

expect(Bundling.bundle).toHaveBeenCalledWith(expect.objectContaining({
skip: true,
}));

spy.mockRestore();
});

test('Do not skip bundling when stack requires it', () => {
const spy = jest.spyOn(stack, 'bundlingRequired', 'get').mockReturnValue(true);
const entry = path.join(__dirname, 'lambda-handler-project');

new PythonLayerVersion(stack, 'layer', {
entry,
});

expect(Bundling.bundle).toHaveBeenCalledWith(expect.objectContaining({
skip: false,
}));

spy.mockRestore();
});

0 comments on commit 9879338

Please sign in to comment.