diff --git a/packages/@aws-cdk/aws-lambda-nodejs/README.md b/packages/@aws-cdk/aws-lambda-nodejs/README.md index 20b79f7878cdc..d65f6ae08996b 100644 --- a/packages/@aws-cdk/aws-lambda-nodejs/README.md +++ b/packages/@aws-cdk/aws-lambda-nodejs/README.md @@ -52,6 +52,15 @@ new lambda.NodejsFunction(this, 'my-handler', { }); ``` +Use the `buildArgs` prop to pass build arguments when building the bundling image: +```ts +new lambda.NodejsFunction(this, 'my-handler', { + buildArgs: { + HTTPS_PROXY: 'https://127.0.0.1:3001', + }, +}); +``` + ### Configuring Parcel The `NodejsFunction` construct exposes some [Parcel](https://parceljs.org/) options via properties: `minify`, `sourceMaps` and `cacheDir`. diff --git a/packages/@aws-cdk/aws-lambda-nodejs/lib/bundling.ts b/packages/@aws-cdk/aws-lambda-nodejs/lib/bundling.ts index 1a068f93d957e..fb57878099afc 100644 --- a/packages/@aws-cdk/aws-lambda-nodejs/lib/bundling.ts +++ b/packages/@aws-cdk/aws-lambda-nodejs/lib/bundling.ts @@ -1,7 +1,7 @@ -import * as lambda from '@aws-cdk/aws-lambda'; -import * as cdk from '@aws-cdk/core'; import * as fs from 'fs'; import * as path from 'path'; +import * as lambda from '@aws-cdk/aws-lambda'; +import * as cdk from '@aws-cdk/core'; import { PackageJsonManager } from './package-json-manager'; import { findUp } from './util'; @@ -71,6 +71,13 @@ export interface ParcelBaseOptions { * @default - 2.0.0-beta.1 */ readonly parcelVersion?: string; + + /** + * Build arguments to pass when building the bundling image. + * + * @default - no build arguments are passed + */ + readonly buildArgs?: { [key:string] : string }; } /** @@ -105,6 +112,7 @@ export class Bundling { // Bundling image derived from runtime bundling image (lambci) const image = cdk.BundlingDockerImage.fromAsset(path.join(__dirname, '../parcel'), { buildArgs: { + ...options.buildArgs ?? {}, IMAGE: options.runtime.bundlingDockerImage.image, PARCEL_VERSION: options.parcelVersion ?? '2.0.0-beta.1', }, diff --git a/packages/@aws-cdk/aws-lambda-nodejs/test/bundling.test.ts b/packages/@aws-cdk/aws-lambda-nodejs/test/bundling.test.ts index 03b71a893f1f2..4ded56d78c049 100644 --- a/packages/@aws-cdk/aws-lambda-nodejs/test/bundling.test.ts +++ b/packages/@aws-cdk/aws-lambda-nodejs/test/bundling.test.ts @@ -1,9 +1,9 @@ -import { Code, Runtime } from '@aws-cdk/aws-lambda'; -import { AssetHashType } from '@aws-cdk/core'; -import { version as delayVersion } from 'delay/package.json'; import * as fs from 'fs'; import * as path from 'path'; +import { Code, Runtime } from '@aws-cdk/aws-lambda'; +import { AssetHashType, BundlingDockerImage } from '@aws-cdk/core'; +import { version as delayVersion } from 'delay/package.json'; import { Bundling } from '../lib/bundling'; import * as util from '../lib/util'; @@ -18,6 +18,7 @@ const findUpMock = jest.spyOn(util, 'findUp').mockImplementation((name: string, } return originalFindUp(name, directory); }); +const fromAssetMock = jest.spyOn(BundlingDockerImage, 'fromAsset'); beforeEach(() => { jest.clearAllMocks(); @@ -157,3 +158,20 @@ test('Detects yarn.lock', () => { }), }); }); + +test('with build args', () => { + Bundling.parcel({ + entry: '/project/folder/entry.ts', + runtime: Runtime.NODEJS_12_X, + projectRoot: '/project', + buildArgs: { + HELLO: 'WORLD', + }, + }); + + expect(fromAssetMock).toHaveBeenCalledWith(expect.stringMatching(/parcel$/), expect.objectContaining({ + buildArgs: expect.objectContaining({ + HELLO: 'WORLD', + }), + })); +});