Skip to content

Commit

Permalink
fix(lambda-nodejs): Required auto prefix of handler with index. b…
Browse files Browse the repository at this point in the history
…reaks custom non-`index` handler settings used by layers (#24406)

Using `lambda-nodejs` makes it very easy to bundle functions with `esbuild`, but the code currently *always* prefixes the `handler` value with `index.`, which makes it impossible to use some lambda extensions together with this module as they require setting `handler` to specific values and the `index.` prefixing breaks the ability to set the handler to those values.

This PR avoids adding the `index.` prefix if the specified `handler` value contains a `.` already.

Closes #24403

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
huntharo authored Mar 6, 2023
1 parent a70ff1a commit d7a1c34
Show file tree
Hide file tree
Showing 15 changed files with 643 additions and 41 deletions.
4 changes: 4 additions & 0 deletions packages/@aws-cdk/aws-lambda-nodejs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ new nodejs.NodejsFunction(this, 'MyFunction', {
});
```

The handler value will be automatically prefixed with the bundled output file name, `index.`,
unless the handler value contains a `.` character, in which case the handler value is used as-is to
allow for values needed by some Lambda extensions.

For monorepos, the reference architecture becomes:

```plaintext
Expand Down
5 changes: 4 additions & 1 deletion packages/@aws-cdk/aws-lambda-nodejs/lib/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ export interface NodejsFunctionProps extends lambda.FunctionOptions {
/**
* The name of the exported handler in the entry file.
*
* The handler is prefixed with `index.` unless the specified handler value contains a `.`,
* in which case it is used as-is.
*
* @default handler
*/
readonly handler?: string;
Expand Down Expand Up @@ -108,7 +111,7 @@ export class NodejsFunction extends lambda.Function {
depsLockFilePath,
projectRoot,
}),
handler: `index.${handler}`,
handler: handler.indexOf('.') !== -1 ? `${handler}` : `index.${handler}`,
});

// Enable connection reuse for aws-sdk
Expand Down
32 changes: 32 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 @@ -45,6 +45,38 @@ test('NodejsFunction with .ts handler', () => {
});
});

test('NodejsFunction with overridden handler - no dots', () => {
// WHEN
new NodejsFunction(stack, 'handler1', {
handler: 'myHandler',
});

expect(Bundling.bundle).toHaveBeenCalledWith(expect.objectContaining({
entry: expect.stringContaining('function.test.handler1.ts'), // Automatically finds .ts handler file
}));

Template.fromStack(stack).hasResourceProperties('AWS::Lambda::Function', {
Handler: 'index.myHandler', // automatic index. prefix
Runtime: 'nodejs14.x',
});
});

test('NodejsFunction with overridden handler - with dots', () => {
// WHEN
new NodejsFunction(stack, 'handler1', {
handler: 'run.sh',
});

expect(Bundling.bundle).toHaveBeenCalledWith(expect.objectContaining({
entry: expect.stringContaining('function.test.handler1.ts'), // Automatically finds .ts handler file
}));

Template.fromStack(stack).hasResourceProperties('AWS::Lambda::Function', {
Handler: 'run.sh', // No index. prefix
Runtime: 'nodejs14.x',
});
});

test('NodejsFunction with .js handler', () => {
// WHEN
new NodejsFunction(stack, 'handler2');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Server } from 'http';
import { mult } from './util';

// Create simple http server
const server = new Server((_req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end(`${mult(3, 4)}`);
console.log(mult(3, 4)); // eslint-disable-line no-console
});

const port = parseInt(process.env.PORT || '3001', 10);
server.listen(port);
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/sh

# For AWS Lambda Adapter
# https://github.com/awslabs/aws-lambda-web-adapter
export READINESS_CHECK_PATH="${READINESS_CHECK_PATH:-/health}"
export AWS_LAMBDA_EXEC_WRAPPER="${AWS_LAMBDA_EXEC_WRAPPER:-/opt/bootstrap}"
export RUST_LOG="${RUST_LOG:-info}"
export AWS_LWA_ENABLE_COMPRESSION="${AWS_LWA_ENABLE_COMPRESSION:-true}"
export PORT="${PORT:-3001}"

exec node index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/sh

# For AWS Lambda Adapter
# https://github.com/awslabs/aws-lambda-web-adapter
export READINESS_CHECK_PATH="${READINESS_CHECK_PATH:-/health}"
export AWS_LAMBDA_EXEC_WRAPPER="${AWS_LAMBDA_EXEC_WRAPPER:-/opt/bootstrap}"
export RUST_LOG="${RUST_LOG:-info}"
export AWS_LWA_ENABLE_COMPRESSION="${AWS_LWA_ENABLE_COMPRESSION:-true}"
export PORT="${PORT:-3001}"

exec node index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "20.0.0",
"version": "30.1.0",
"files": {
"5017e4b2e278e32bc634202d075b7ed8961b0d784f75450f7918a6a4f6f7df4a": {
"source": {
Expand Down Expand Up @@ -40,15 +40,28 @@
}
}
},
"90cb9162c12b37a3990ecbde27ea037907171ccb64eeb60f046f098a9ae069cd": {
"a33898f49e24c41ff9be236439c418d30e576c5f57763097162d9ec4245216ce": {
"source": {
"path": "asset.a33898f49e24c41ff9be236439c418d30e576c5f57763097162d9ec4245216ce",
"packaging": "zip"
},
"destinations": {
"current_account-current_region": {
"bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}",
"objectKey": "a33898f49e24c41ff9be236439c418d30e576c5f57763097162d9ec4245216ce.zip",
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}"
}
}
},
"76dea8b92881f37d669c02f0271a5aa08e02b115b3fbe57063dbb99c5acb1932": {
"source": {
"path": "cdk-integ-lambda-nodejs.template.json",
"packaging": "file"
},
"destinations": {
"current_account-current_region": {
"bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}",
"objectKey": "90cb9162c12b37a3990ecbde27ea037907171ccb64eeb60f046f098a9ae069cd.json",
"objectKey": "76dea8b92881f37d669c02f0271a5aa08e02b115b3fbe57063dbb99c5acb1932.json",
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}"
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,136 @@
"VpcPrivateSubnet2DefaultRoute060D2087",
"VpcPrivateSubnet2RouteTableAssociationA89CAD56"
]
},
"tshandlercustomhandlernodotsServiceRole1775E15E": {
"Type": "AWS::IAM::Role",
"Properties": {
"AssumeRolePolicyDocument": {
"Statement": [
{
"Action": "sts:AssumeRole",
"Effect": "Allow",
"Principal": {
"Service": "lambda.amazonaws.com"
}
}
],
"Version": "2012-10-17"
},
"ManagedPolicyArns": [
{
"Fn::Join": [
"",
[
"arn:",
{
"Ref": "AWS::Partition"
},
":iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
]
]
}
]
}
},
"tshandlercustomhandlernodots381F62EE": {
"Type": "AWS::Lambda::Function",
"Properties": {
"Code": {
"S3Bucket": {
"Fn::Sub": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}"
},
"S3Key": "5017e4b2e278e32bc634202d075b7ed8961b0d784f75450f7918a6a4f6f7df4a.zip"
},
"Role": {
"Fn::GetAtt": [
"tshandlercustomhandlernodotsServiceRole1775E15E",
"Arn"
]
},
"Environment": {
"Variables": {
"AWS_NODEJS_CONNECTION_REUSE_ENABLED": "1"
}
},
"Handler": "index.handler",
"Runtime": "nodejs14.x"
},
"DependsOn": [
"tshandlercustomhandlernodotsServiceRole1775E15E"
]
},
"tshandlercustomhandlerdotsServiceRole0575D3CB": {
"Type": "AWS::IAM::Role",
"Properties": {
"AssumeRolePolicyDocument": {
"Statement": [
{
"Action": "sts:AssumeRole",
"Effect": "Allow",
"Principal": {
"Service": "lambda.amazonaws.com"
}
}
],
"Version": "2012-10-17"
},
"ManagedPolicyArns": [
{
"Fn::Join": [
"",
[
"arn:",
{
"Ref": "AWS::Partition"
},
":iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
]
]
}
]
}
},
"tshandlercustomhandlerdots2695F653": {
"Type": "AWS::Lambda::Function",
"Properties": {
"Code": {
"S3Bucket": {
"Fn::Sub": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}"
},
"S3Key": "a33898f49e24c41ff9be236439c418d30e576c5f57763097162d9ec4245216ce.zip"
},
"Role": {
"Fn::GetAtt": [
"tshandlercustomhandlerdotsServiceRole0575D3CB",
"Arn"
]
},
"Environment": {
"Variables": {
"AWS_NODEJS_CONNECTION_REUSE_ENABLED": "1"
}
},
"Handler": "ts-web.run.sh",
"Layers": [
{
"Fn::Join": [
"",
[
"arn:aws:lambda:",
{
"Ref": "AWS::Region"
},
":753240598075:layer:LambdaAdapterLayerX86:13"
]
]
}
],
"Runtime": "nodejs14.x"
},
"DependsOn": [
"tshandlercustomhandlerdotsServiceRole0575D3CB"
]
}
},
"Parameters": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"version":"20.0.0"}
{"version":"30.1.0"}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "20.0.0",
"version": "30.1.0",
"testCases": {
"integ.function": {
"stacks": [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
{
"version": "20.0.0",
"version": "30.1.0",
"artifacts": {
"Tree": {
"type": "cdk:tree",
"properties": {
"file": "tree.json"
}
},
"cdk-integ-lambda-nodejs.assets": {
"type": "cdk:asset-manifest",
"properties": {
Expand All @@ -23,7 +17,7 @@
"validateOnSynth": false,
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-deploy-role-${AWS::AccountId}-${AWS::Region}",
"cloudFormationExecutionRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-cfn-exec-role-${AWS::AccountId}-${AWS::Region}",
"stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/90cb9162c12b37a3990ecbde27ea037907171ccb64eeb60f046f098a9ae069cd.json",
"stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/76dea8b92881f37d669c02f0271a5aa08e02b115b3fbe57063dbb99c5acb1932.json",
"requiresBootstrapStackVersion": 6,
"bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version",
"additionalDependencies": [
Expand Down Expand Up @@ -219,6 +213,30 @@
"data": "tshandlervpcA502E26A"
}
],
"/cdk-integ-lambda-nodejs/ts-handler-custom-handler-no-dots/ServiceRole/Resource": [
{
"type": "aws:cdk:logicalId",
"data": "tshandlercustomhandlernodotsServiceRole1775E15E"
}
],
"/cdk-integ-lambda-nodejs/ts-handler-custom-handler-no-dots/Resource": [
{
"type": "aws:cdk:logicalId",
"data": "tshandlercustomhandlernodots381F62EE"
}
],
"/cdk-integ-lambda-nodejs/ts-handler-custom-handler-dots/ServiceRole/Resource": [
{
"type": "aws:cdk:logicalId",
"data": "tshandlercustomhandlerdotsServiceRole0575D3CB"
}
],
"/cdk-integ-lambda-nodejs/ts-handler-custom-handler-dots/Resource": [
{
"type": "aws:cdk:logicalId",
"data": "tshandlercustomhandlerdots2695F653"
}
],
"/cdk-integ-lambda-nodejs/BootstrapVersion": [
{
"type": "aws:cdk:logicalId",
Expand All @@ -233,6 +251,12 @@
]
},
"displayName": "cdk-integ-lambda-nodejs"
},
"Tree": {
"type": "cdk:tree",
"properties": {
"file": "tree.json"
}
}
}
}
Loading

0 comments on commit d7a1c34

Please sign in to comment.