Skip to content

Commit 7056ab7

Browse files
Merge pull request #3 from ajeetchaulagain/define-cdk-stack
Define a CDK stack with Lambda, API gateway
2 parents f2e75af + faf8405 commit 7056ab7

3 files changed

Lines changed: 34 additions & 7 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
/node_modules
44
/build
55

6+
# Lambda layer
7+
/layer/nodejs/node_modules
8+
69
# Logs
710
logs
811
*.log

infra/lib/infra-stack.ts

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,39 @@
11
import * as cdk from 'aws-cdk-lib/core';
22
import { Construct } from 'constructs';
3-
// import * as sqs from 'aws-cdk-lib/aws-sqs';
3+
import * as lambda from 'aws-cdk-lib/aws-lambda';
4+
import * as apigwv2 from 'aws-cdk-lib/aws-apigatewayv2';
5+
import * as integrations from 'aws-cdk-lib/aws-apigatewayv2-integrations';
6+
import * as path from 'path';
47

58
export class InfraStack extends cdk.Stack {
69
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
710
super(scope, id, props);
811

9-
// The code that defines your stack goes here
12+
const nodeModulesLayer = new lambda.LayerVersion(this, 'NodeModulesLayer', {
13+
code: lambda.Code.fromAsset(path.join(__dirname, '../../layer')),
14+
compatibleRuntimes: [lambda.Runtime.NODEJS_20_X],
15+
description: 'Production node_modules for NestJS Lambda',
16+
});
1017

11-
// example resource
12-
// const queue = new sqs.Queue(this, 'InfraQueue', {
13-
// visibilityTimeout: cdk.Duration.seconds(300)
14-
// });
18+
const nestApiLambda = new lambda.Function(this, 'NestApiLambdaFunction', {
19+
runtime: lambda.Runtime.NODEJS_20_X,
20+
handler: 'src/lambda.handler',
21+
code: lambda.Code.fromAsset(path.join(__dirname, '../../dist'), {
22+
exclude: ['infra', 'tsconfig*'],
23+
}),
24+
memorySize: 512,
25+
layers: [nodeModulesLayer],
26+
});
27+
28+
const httpApi = new apigwv2.HttpApi(this, 'HttpApi', {
29+
defaultIntegration: new integrations.HttpLambdaIntegration(
30+
'LambdaIntegration',
31+
nestApiLambda,
32+
),
33+
});
34+
35+
new cdk.CfnOutput(this, 'HttpApiUrl', {
36+
value: httpApi.url!,
37+
});
1538
}
1639
}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
"test:watch": "jest --watch",
1818
"test:cov": "jest --coverage",
1919
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
20-
"test:e2e": "jest --config ./test/jest-e2e.json"
20+
"test:e2e": "jest --config ./test/jest-e2e.json",
21+
"build:lambda-layer": "cp package.json package-lock.json layer/nodejs/ && npm ci --prefix layer/nodejs --omit=dev && rm layer/nodejs/package.json layer/nodejs/package-lock.json"
2122
},
2223
"dependencies": {
2324
"@codegenie/serverless-express": "^4.17.1",

0 commit comments

Comments
 (0)