Skip to content

Commit

Permalink
Add runnable web app for run.sh demo
Browse files Browse the repository at this point in the history
  • Loading branch information
huntharo committed Mar 4, 2023
1 parent c51e074 commit 9f7ac2f
Show file tree
Hide file tree
Showing 10 changed files with 125 additions and 14 deletions.
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
Expand Up @@ -40,15 +40,28 @@
}
}
},
"1c8574159a9201ac5d40fd41139dd5702b99bb25e7f50e6f225139901c887148": {
"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": "1c8574159a9201ac5d40fd41139dd5702b99bb25e7f50e6f225139901c887148.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 @@ -678,7 +678,7 @@
"AWS_NODEJS_CONNECTION_REUSE_ENABLED": "1"
}
},
"Handler": "index.someHandler",
"Handler": "index.handler",
"Runtime": "nodejs14.x"
},
"DependsOn": [
Expand Down Expand Up @@ -723,7 +723,7 @@
"S3Bucket": {
"Fn::Sub": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}"
},
"S3Key": "5017e4b2e278e32bc634202d075b7ed8961b0d784f75450f7918a6a4f6f7df4a.zip"
"S3Key": "a33898f49e24c41ff9be236439c418d30e576c5f57763097162d9ec4245216ce.zip"
},
"Role": {
"Fn::GetAtt": [
Expand All @@ -736,7 +736,21 @@
"AWS_NODEJS_CONNECTION_REUSE_ENABLED": "1"
}
},
"Handler": "run.sh",
"Handler": "ts-web.run.sh",
"Layers": [
{
"Fn::Join": [
"",
[
"arn:aws:lambda:",
{
"Ref": "AWS::Region"
},
":753240598075:layer:LambdaAdapterLayerX86:13"
]
]
}
],
"Runtime": "nodejs14.x"
},
"DependsOn": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,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}/1c8574159a9201ac5d40fd41139dd5702b99bb25e7f50e6f225139901c887148.json",
"stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/76dea8b92881f37d669c02f0271a5aa08e02b115b3fbe57063dbb99c5acb1932.json",
"requiresBootstrapStackVersion": 6,
"bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version",
"additionalDependencies": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1200,7 +1200,7 @@
"AWS_NODEJS_CONNECTION_REUSE_ENABLED": "1"
}
},
"handler": "index.someHandler",
"handler": "index.handler",
"runtime": "nodejs14.x"
}
},
Expand All @@ -1215,6 +1215,14 @@
"version": "0.0.0"
}
},
"lambda-adapter-layer": {
"id": "lambda-adapter-layer",
"path": "cdk-integ-lambda-nodejs/lambda-adapter-layer",
"constructInfo": {
"fqn": "@aws-cdk/core.Resource",
"version": "0.0.0"
}
},
"ts-handler-custom-handler-dots": {
"id": "ts-handler-custom-handler-dots",
"path": "cdk-integ-lambda-nodejs/ts-handler-custom-handler-dots",
Expand Down Expand Up @@ -1312,7 +1320,7 @@
"s3Bucket": {
"Fn::Sub": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}"
},
"s3Key": "5017e4b2e278e32bc634202d075b7ed8961b0d784f75450f7918a6a4f6f7df4a.zip"
"s3Key": "a33898f49e24c41ff9be236439c418d30e576c5f57763097162d9ec4245216ce.zip"
},
"role": {
"Fn::GetAtt": [
Expand All @@ -1325,7 +1333,21 @@
"AWS_NODEJS_CONNECTION_REUSE_ENABLED": "1"
}
},
"handler": "run.sh",
"handler": "ts-web.run.sh",
"layers": [
{
"Fn::Join": [
"",
[
"arn:aws:lambda:",
{
"Ref": "AWS::Region"
},
":753240598075:layer:LambdaAdapterLayerX86:13"
]
]
}
],
"runtime": "nodejs14.x"
}
},
Expand Down
29 changes: 24 additions & 5 deletions packages/@aws-cdk/aws-lambda-nodejs/test/integ.function.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import * as os from 'os';
import * as path from 'path';
import { Vpc } from '@aws-cdk/aws-ec2';
import { Runtime } from '@aws-cdk/aws-lambda';
import { App, Stack, StackProps } from '@aws-cdk/core';
import { LayerVersion, Runtime } from '@aws-cdk/aws-lambda';
import { Aws, App, Stack, StackProps } from '@aws-cdk/core';
import { Construct } from 'constructs';
import * as lambda from '../lib';

Expand Down Expand Up @@ -38,18 +39,36 @@ class TestStack extends Stack {
sourceMap: true,
sourceMapMode: lambda.SourceMapMode.BOTH,
},
handler: 'someHandler',
handler: 'handler',
});

new lambda.NodejsFunction(this, 'ts-handler-custom-handler-dots', {
entry: path.join(__dirname, 'integ-handlers/ts-handler.ts'),
entry: path.join(__dirname, 'integ-handlers/ts-web-handler.ts'),
runtime: Runtime.NODEJS_14_X,
bundling: {
minify: true,
sourceMap: true,
sourceMapMode: lambda.SourceMapMode.BOTH,
commandHooks: {
beforeBundling: () => [],
beforeInstall: () => [],
afterBundling: (_inputDir, outputDir) => [
`${os.platform() === 'win32' ? 'copy' : 'cp'} ${path.join(
__dirname,
'integ-handlers',
'ts-web-run.sh',
)} ${outputDir}`,
],
},
},
handler: 'run.sh',
handler: 'ts-web.run.sh',
layers: [
LayerVersion.fromLayerVersionArn(
this,
'lambda-adapter-layer',
`arn:aws:lambda:${Aws.REGION}:753240598075:layer:LambdaAdapterLayerX86:13`,
),
],
});
}
}
Expand Down

0 comments on commit 9f7ac2f

Please sign in to comment.