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 local bundling option #26062

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
30 changes: 30 additions & 0 deletions packages/@aws-cdk/aws-lambda-python-alpha/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,36 @@ new python.PythonFunction(this, 'function', {
});
```

## Local bundling

Use `local` to specify a local bundling provider. The provider implements a
method `tryBundle()` which should return `true` if local bundling was performed.
If `false` is returned, Docker bundling will be done:

```ts
import * as cdk from 'aws-cdk-lib';

class MyBundle implements cdk.ILocalBundling {
public tryBundle(outputDir: string, options: cdk.BundlingOptions) {
const canRunLocally = true // replace with actual logic
if (canRunLocally) {
// perform local bundling here
return true;
}
return false;
}
}

const entry = '/path/to/function';
new python.PythonFunction(this, 'function', {
entry,
runtime: Runtime.PYTHON_3_8,
bundling: {
local: new MyBundle()
},
});
```

## Command hooks

It is possible to run additional commands by specifying the `commandHooks` prop:
Expand Down
4 changes: 3 additions & 1 deletion packages/@aws-cdk/aws-lambda-python-alpha/lib/bundling.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as path from 'path';
import { Architecture, AssetCode, Code, Runtime } from 'aws-cdk-lib/aws-lambda';
import { AssetStaging, BundlingFileAccess, BundlingOptions as CdkBundlingOptions, DockerImage, DockerVolume } from 'aws-cdk-lib/core';
import { AssetStaging, BundlingFileAccess, BundlingOptions as CdkBundlingOptions, DockerImage, DockerVolume, ILocalBundling } from 'aws-cdk-lib/core';
import { Packaging, DependenciesFile } from './packaging';
import { BundlingOptions, ICommandHooks } from './types';

Expand Down Expand Up @@ -73,6 +73,7 @@ export class Bundling implements CdkBundlingOptions {
public readonly securityOpt?: string;
public readonly network?: string;
public readonly bundlingFileAccess?: BundlingFileAccess;
public readonly local?: ILocalBundling;

constructor(props: BundlingProps) {
const {
Expand Down Expand Up @@ -114,6 +115,7 @@ export class Bundling implements CdkBundlingOptions {
this.securityOpt = props.securityOpt;
this.network = props.network;
this.bundlingFileAccess = props.bundlingFileAccess;
this.local = props.local;
}

private createBundlingCommand(options: BundlingCommandOptions): string[] {
Expand Down
13 changes: 12 additions & 1 deletion packages/@aws-cdk/aws-lambda-python-alpha/lib/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AssetHashType, BundlingFileAccess, DockerImage, DockerRunOptions } from 'aws-cdk-lib/core';
import { AssetHashType, BundlingFileAccess, DockerImage, DockerRunOptions, ILocalBundling } from 'aws-cdk-lib/core';

/**
* Options for bundling
Expand Down Expand Up @@ -98,6 +98,17 @@ export interface BundlingOptions extends DockerRunOptions {
* @default - BundlingFileAccess.BIND_MOUNT
*/
readonly bundlingFileAccess?: BundlingFileAccess;

/**
* Local bundling provider.
*
* The provider implements a method `tryBundle()` which should return `true`
* if local bundling was performed. If `false` is returned, docker bundling
* will be done.
*
* @default - bundling will only be performed in a Docker container
*/
readonly local?: ILocalBundling;
}

/**
Expand Down
22 changes: 22 additions & 0 deletions packages/@aws-cdk/aws-lambda-python-alpha/test/bundling.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -554,3 +554,25 @@ test('with command hooks', () => {
}),
}));
});

test('Local bundling is used', () => {
const entry = path.join(__dirname, 'lambda-handler');

const local = {
tryBundle() {
return true;
},
};

Bundling.bundle({
entry: entry,
runtime: Runtime.PYTHON_3_7,
local,
});

expect(Code.fromAsset).toHaveBeenCalledWith(entry, expect.objectContaining({
bundling: expect.objectContaining({
local,
}),
}));
});
Loading