diff --git a/packages/@aws-cdk/aws-lambda-python/README.md b/packages/@aws-cdk/aws-lambda-python/README.md index fc68cbce9d837..370bc6c13cf83 100644 --- a/packages/@aws-cdk/aws-lambda-python/README.md +++ b/packages/@aws-cdk/aws-lambda-python/README.md @@ -145,6 +145,18 @@ new python.PythonFunction(this, 'function', { }); ``` +You can also pass additional options to configure Docker for situations where the docker daemon is not running in the same system as you are bundling from. + + ```ts +const entry = '/path/to/function'; + +new python.PythonFunction(this, 'function', { + entry, + runtime: Runtime.PYTHON_3_8, + bundling: { volumesFrom: process.env.HOSTNAME }, +}); +``` + ## Custom Bundling with Code Artifact To use a Code Artifact PyPI repo, the `PIP_INDEX_URL` for bundling the function can be customized (requires AWS CLI in the build environment): diff --git a/packages/@aws-cdk/aws-lambda-python/lib/bundling.ts b/packages/@aws-cdk/aws-lambda-python/lib/bundling.ts index 726b72c5bc2b8..14050cb49d0ad 100644 --- a/packages/@aws-cdk/aws-lambda-python/lib/bundling.ts +++ b/packages/@aws-cdk/aws-lambda-python/lib/bundling.ts @@ -35,6 +35,13 @@ export interface BundlingProps extends BundlingOptions { */ readonly architecture?: Architecture; + /** + * Where to mount the specified volumes from + * Docker [volumes-from option](https://docs.docker.com/engine/reference/commandline/run/#mount-volumes-from-container---volumes-from) + * @default - no volumes-from options + */ + readonly volumesFrom?: string; + /** * Whether or not the bundling process should be skipped * @@ -59,6 +66,7 @@ export class Bundling implements CdkBundlingOptions { public readonly image: DockerImage; public readonly command: string[]; public readonly environment?: { [key: string]: string }; + public readonly volumesFrom?: string | undefined; constructor(props: BundlingProps) { const { @@ -88,6 +96,7 @@ export class Bundling implements CdkBundlingOptions { }); this.command = ['bash', '-c', chain(bundlingCommands)]; this.environment = props.environment; + this.volumesFrom = props.volumesFrom; } private createBundlingCommand(options: BundlingCommandOptions): string[] { diff --git a/packages/@aws-cdk/aws-lambda-python/lib/types.ts b/packages/@aws-cdk/aws-lambda-python/lib/types.ts index 3689c43335959..c3311be3f70fd 100644 --- a/packages/@aws-cdk/aws-lambda-python/lib/types.ts +++ b/packages/@aws-cdk/aws-lambda-python/lib/types.ts @@ -86,4 +86,11 @@ export interface BundlingOptions { * @default - Based on `assetHashType` */ readonly assetHash?: string; + + /** + * Where to mount the specified volumes from + * Docker [volumes-from option](https://docs.docker.com/engine/reference/commandline/run/#mount-volumes-from-container---volumes-from) + * @default - no volumes-from options + */ + readonly volumesFrom?: string; } diff --git a/packages/@aws-cdk/aws-lambda-python/test/bundling.test.ts b/packages/@aws-cdk/aws-lambda-python/test/bundling.test.ts index 5626a3f02d2a4..6276a31213792 100644 --- a/packages/@aws-cdk/aws-lambda-python/test/bundling.test.ts +++ b/packages/@aws-cdk/aws-lambda-python/test/bundling.test.ts @@ -278,6 +278,21 @@ test('Bundling with custom environment vars`', () => { })); }); +test('Bundling with custom volumes', () => { + const entry = path.join(__dirname, 'lambda-handler'); + Bundling.bundle({ + entry: entry, + runtime: Runtime.PYTHON_3_7, + volumesFrom: process.env.HOSTNAME, + }); + + expect(Code.fromAsset).toHaveBeenCalledWith(entry, expect.objectContaining({ + bundling: expect.objectContaining({ + volumesFrom: process.env.HOSTNAME, + }), + })); +}); + test('Do not build docker image when skipping bundling', () => { const entry = path.join(__dirname, 'lambda-handler'); Bundling.bundle({ diff --git a/packages/@aws-cdk/core/lib/asset-staging.ts b/packages/@aws-cdk/core/lib/asset-staging.ts index 96b16554a67f1..6831790d50938 100644 --- a/packages/@aws-cdk/core/lib/asset-staging.ts +++ b/packages/@aws-cdk/core/lib/asset-staging.ts @@ -469,6 +469,7 @@ export class AssetStaging extends Construct { entrypoint: options.entrypoint, workingDirectory: options.workingDirectory ?? AssetStaging.BUNDLING_INPUT_DIR, securityOpt: options.securityOpt ?? '', + volumesFrom: options.volumesFrom, }); } } catch (err) { diff --git a/packages/@aws-cdk/core/lib/bundling.ts b/packages/@aws-cdk/core/lib/bundling.ts index c496bf31d8672..c33db4f3c5c10 100644 --- a/packages/@aws-cdk/core/lib/bundling.ts +++ b/packages/@aws-cdk/core/lib/bundling.ts @@ -43,6 +43,13 @@ export interface BundlingOptions { */ readonly volumes?: DockerVolume[]; + /** + * Where to mount the specified volumes from + * Docker [volumes-from option](https://docs.docker.com/engine/reference/commandline/run/#mount-volumes-from-container---volumes-from) + * @default - no volumes-from options + */ + readonly volumesFrom?: string; + /** * The environment variables to pass to the Docker container. * @@ -210,6 +217,9 @@ export class BundlingDockerImage { ...options.user ? ['-u', options.user] : [], + ...options.volumesFrom + ? ['--volumes-from', options.volumesFrom] + : [], ...flatten(volumes.map(v => ['-v', `${v.hostPath}:${v.containerPath}:${isSeLinux() ? 'z,' : ''}${v.consistency ?? DockerVolumeConsistency.DELEGATED}`])), ...flatten(Object.entries(environment).map(([k, v]) => ['--env', `${k}=${v}`])), ...options.workingDirectory @@ -441,6 +451,13 @@ export interface DockerRunOptions { */ readonly volumes?: DockerVolume[]; + /** + * Where to mount the specified volumes from + * Docker [volumes-from option](https://docs.docker.com/engine/reference/commandline/run/#mount-volumes-from-container---volumes-from) + * @default - no volumes-from options + */ + readonly volumesFrom?: string; + /** * The environment variables to pass to the container. *