Skip to content

Commit

Permalink
feat(lambda-nodejs): connection reuse with aws-sdk
Browse files Browse the repository at this point in the history
By default, the default Node.js HTTP/HTTPS agent creates a new TCP
connection for every new request. To avoid the cost of establishing a
new connection, configure the SDK for JavaScript to reuse TCP
connections.

For short-lived operations, such as DynamoDB queries, the latency overhead of
setting up a TCP connection might be greater than the operation itself.

See https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/node-reusing-connections.html
  • Loading branch information
jogold committed Jul 15, 2020
1 parent b0f8729 commit 229eff7
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
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.

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

Expand Down
7 changes: 5 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 @@ -69,6 +69,9 @@ export class NodejsFunction extends lambda.Function {
}),
handler: `index.${handler}`,
});

// Enable connection reuse for aws-sdk
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
9 changes: 7 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,8 @@
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 { 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 @@ -35,6 +35,11 @@ test('NodejsFunction with .ts handler', () => {

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

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": [
"externalServiceRole85A00A90"
Expand Down

0 comments on commit 229eff7

Please sign in to comment.