Skip to content

Commit

Permalink
feat(lambda-nodejs): allow passing env vars to container (#8169)
Browse files Browse the repository at this point in the history
Add a `containerEnvironment` prop to pass environment variables to the
container running Parcel.

Closes #8031


----

*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 Jun 1, 2020
1 parent c704d16 commit 1755cf2
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 0 deletions.
11 changes: 11 additions & 0 deletions packages/@aws-cdk/aws-lambda-nodejs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,17 @@ new lambda.NodejsFunction(this, 'MyFunction', {

All other properties of `lambda.Function` are supported, see also the [AWS Lambda construct library](https://github.com/aws/aws-cdk/tree/master/packages/%40aws-cdk/aws-lambda).

Use the `containerEnvironment` prop to pass environments variables to the Docker container
running Parcel:

```ts
new lambda.NodejsFunction(this, 'my-handler', {
containerEnvironment: {
NODE_ENV: 'production',
},
});
```

### Configuring Parcel
The `NodejsFunction` construct exposes some [Parcel](https://parceljs.org/) options via properties: `minify`, `sourceMaps`,
`buildDir` and `cacheDir`.
Expand Down
12 changes: 12 additions & 0 deletions packages/@aws-cdk/aws-lambda-nodejs/lib/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ export interface BuilderOptions {
* mounted in the Docker container.
*/
readonly projectRoot: string;

/**
* The environment variables to pass to the container running Parcel.
*
* @default - no environment variables are passed to the container
*/
readonly environment?: { [key: string]: string; };
}

/**
Expand Down Expand Up @@ -111,6 +118,7 @@ export class Builder {
'-v', `${this.options.projectRoot}:${containerProjectRoot}`,
'-v', `${path.resolve(this.options.outDir)}:${containerOutDir}`,
...(this.options.cacheDir ? ['-v', `${path.resolve(this.options.cacheDir)}:${containerCacheDir}`] : []),
...flatten(Object.entries(this.options.environment || {}).map(([k, v]) => ['--env', `${k}=${v}`])),
'-w', path.dirname(containerEntryPath).replace(/\\/g, '/'), // Always use POSIX paths in the container
'parcel-bundler',
];
Expand Down Expand Up @@ -164,3 +172,7 @@ export class Builder {
fs.writeFileSync(this.pkgPath, this.originalPkg);
}
}

function flatten(x: string[][]) {
return Array.prototype.concat([], ...x);
}
8 changes: 8 additions & 0 deletions packages/@aws-cdk/aws-lambda-nodejs/lib/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ export interface NodejsFunctionProps extends lambda.FunctionOptions {
* @default - the closest path containing a .git folder
*/
readonly projectRoot?: string;

/**
* The environment variables to pass to the container running Parcel.
*
* @default - no environment variables are passed to the container
*/
readonly containerEnvironment?: { [key: string]: string; };
}

/**
Expand Down Expand Up @@ -119,6 +126,7 @@ export class NodejsFunction extends lambda.Function {
nodeVersion: extractVersion(runtime),
nodeDockerTag: props.nodeDockerTag || `${process.versions.node}-alpine`,
projectRoot: path.resolve(projectRoot),
environment: props.containerEnvironment,
});
builder.build();

Expand Down
24 changes: 24 additions & 0 deletions packages/@aws-cdk/aws-lambda-nodejs/test/builder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,30 @@ test('with Windows paths', () => {
]));
});

test('with env vars', () => {
const builder = new Builder({
entry: '/project/folder/entry.ts',
global: 'handler',
outDir: '/out-dir',
cacheDir: '/cache-dir',
nodeDockerTag: 'lts-alpine',
nodeVersion: '12',
projectRoot: '/project',
environment: {
KEY1: 'VALUE1',
KEY2: 'VALUE2',
},
});
builder.build();

// docker run
expect(spawnSync).toHaveBeenCalledWith('docker', expect.arrayContaining([
'run',
'--env', 'KEY1=VALUE1',
'--env', 'KEY2=VALUE2',
]));
});

test('throws in case of error', () => {
const builder = new Builder({
entry: '/project/folder/error',
Expand Down
15 changes: 15 additions & 0 deletions packages/@aws-cdk/aws-lambda-nodejs/test/function.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,21 @@ test('NodejsFunction with .js handler', () => {
}));
});

test('NodejsFunction with container env vars', () => {
// WHEN
new NodejsFunction(stack, 'handler1', {
containerEnvironment: {
KEY: 'VALUE',
},
});

expect(Builder).toHaveBeenCalledWith(expect.objectContaining({
environment: {
KEY: 'VALUE',
},
}));
});

test('throws when entry is not js/ts', () => {
expect(() => new NodejsFunction(stack, 'Fn', {
entry: 'handler.py',
Expand Down

0 comments on commit 1755cf2

Please sign in to comment.