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): connection reuse with aws-sdk #9083

Merged
merged 6 commits into from
Jul 19, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 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,9 @@ 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).

The `NodejsFunction` construct automatically [reuses existing connections](https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/node-reusing-connections.html)
when working with the AWS SDK for JavaScript. Set the `awsSdkConnectionReuse` prop to `false` to disable it.

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

Expand Down
22 changes: 20 additions & 2 deletions packages/@aws-cdk/aws-lambda-nodejs/lib/function.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 { Bundling, ParcelBaseOptions } from './bundling';
import { PackageJsonManager } from './package-json-manager';
import { nodeMajorVersion, parseStackTrace } from './util';
Expand Down Expand Up @@ -35,6 +35,19 @@ export interface NodejsFunctionProps extends lambda.FunctionOptions, ParcelBaseO
* `NODEJS_10_X` otherwise.
*/
readonly runtime?: lambda.Runtime;

/**
* Whether to automatically reuse TCP connections when working with the AWS
* SDK for JavaScript.
*
* This sets the `AWS_NODEJS_CONNECTION_REUSE_ENABLED` environment variable
* to `1`.
*
* @see https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/node-reusing-connections.html
*
* @default true
*/
readonly awsSdkConnectionReuse?: boolean;
}

/**
Expand Down Expand Up @@ -69,6 +82,11 @@ export class NodejsFunction extends lambda.Function {
}),
handler: `index.${handler}`,
});

// Enable connection reuse for aws-sdk
if (props.awsSdkConnectionReuse ?? true) {
this.addEnvironment('AWS_NODEJS_CONNECTION_REUSE_ENABLED', '1');
}
} finally {
// We can only restore after the code has been bound to the function
packageJsonManager.restore();
Expand Down
29 changes: 27 additions & 2 deletions packages/@aws-cdk/aws-lambda-nodejs/test/function.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import '@aws-cdk/assert/jest';
import { Runtime } from '@aws-cdk/aws-lambda';
import { Stack } from '@aws-cdk/core';
import * as fs from 'fs';
import * as path from 'path';
import { ABSENT } from '@aws-cdk/assert';
import { Runtime } from '@aws-cdk/aws-lambda';
import { Stack } from '@aws-cdk/core';
import { NodejsFunction } from '../lib';
import { Bundling } from '../lib/bundling';

Expand Down Expand Up @@ -107,3 +108,27 @@ test('resolves entry to an absolute path', () => {
entry: expect.stringMatching(/@aws-cdk\/aws-lambda-nodejs\/lib\/index.ts$/),
}));
});

test('configures connection reuse for aws sdk', () => {
// WHEN
new NodejsFunction(stack, 'handler1');

expect(stack).toHaveResource('AWS::Lambda::Function', {
Environment: {
Variables: {
AWS_NODEJS_CONNECTION_REUSE_ENABLED: '1',
},
},
});
});

test('can opt-out of connection reuse for aws sdk', () => {
// WHEN
new NodejsFunction(stack, 'handler1', {
awsSdkConnectionReuse: false,
});

expect(stack).toHaveResource('AWS::Lambda::Function', {
Environment: ABSENT,
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,12 @@
"Arn"
]
},
"Runtime": "nodejs12.x"
"Runtime": "nodejs12.x",
"Environment": {
"Variables": {
"AWS_NODEJS_CONNECTION_REUSE_ENABLED": "1"
}
}
},
"DependsOn": [
"externalServiceRole85A00A90"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,12 @@
"Arn"
]
},
"Runtime": "nodejs12.x"
"Runtime": "nodejs12.x",
"Environment": {
"Variables": {
"AWS_NODEJS_CONNECTION_REUSE_ENABLED": "1"
}
}
},
"DependsOn": [
"tshandlerServiceRole8876B8E7"
Expand Down Expand Up @@ -164,7 +169,12 @@
"Arn"
]
},
"Runtime": "nodejs12.x"
"Runtime": "nodejs12.x",
"Environment": {
"Variables": {
"AWS_NODEJS_CONNECTION_REUSE_ENABLED": "1"
}
}
},
"DependsOn": [
"jshandlerServiceRole781AF366"
Expand Down