Skip to content

Commit

Permalink
feat(lambda-python): add local bundling option
Browse files Browse the repository at this point in the history
  • Loading branch information
polothy committed Jun 20, 2023
1 parent a8768f4 commit 6b87157
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 2 deletions.
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,
}),
}));
});

0 comments on commit 6b87157

Please sign in to comment.