Skip to content

Commit

Permalink
feat(lambda-nodejs): support build args (aws#9035)
Browse files Browse the repository at this point in the history
Add support for passing build arguments when building the bundling
image.

Closes aws#8117


----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
jogold authored and Curtis Eppel committed Aug 11, 2020
1 parent 133a33e commit 2d6f5fa
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 5 deletions.
9 changes: 9 additions & 0 deletions packages/@aws-cdk/aws-lambda-nodejs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.

Expand Down
12 changes: 10 additions & 2 deletions packages/@aws-cdk/aws-lambda-nodejs/lib/bundling.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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 };
}

/**
Expand Down Expand Up @@ -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',
},
Expand Down
24 changes: 21 additions & 3 deletions packages/@aws-cdk/aws-lambda-nodejs/test/bundling.test.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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();
Expand Down Expand Up @@ -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',
}),
}));
});

0 comments on commit 2d6f5fa

Please sign in to comment.