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-nodejs): support build args #9035

Merged
merged 4 commits into from
Jul 14, 2020
Merged
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
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',
}),
}));
});