Skip to content

Commit 4e0eb52

Browse files
committed
deploy api as AWS Lambda using AWS CDK
1 parent a621a23 commit 4e0eb52

File tree

10 files changed

+1026
-5
lines changed

10 files changed

+1026
-5
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,7 @@ lerna-debug.log*
3636
.elasticbeanstalk/*
3737
!.elasticbeanstalk/*.cfg.yml
3838
!.elasticbeanstalk/*.global.yml
39+
40+
# AWS CDK
41+
cdk.context.json
42+
cdk.out

README.md

+44-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
[travis-url]: https://travis-ci.org/nestjs/nest
77
[linux-image]: https://img.shields.io/travis/nestjs/nest/master.svg?label=linux
88
[linux-url]: https://travis-ci.org/nestjs/nest
9-
9+
1010
<p align="center">A progressive <a href="http://nodejs.org" target="blank">Node.js</a> framework for building efficient and scalable server-side applications, heavily inspired by <a href="https://angular.io" target="blank">Angular</a>.</p>
1111
<p align="center">
1212
<a href="https://www.npmjs.com/~nestjscore"><img src="https://img.shields.io/npm/v/@nestjs/core.svg" alt="NPM Version" /></a>
@@ -60,6 +60,48 @@ $ npm run test:e2e
6060
$ npm run test:cov
6161
```
6262

63+
## Deploy
64+
65+
Deployment is done using AWS CDK. You can find deployment configuration in `./deploy` folder.
66+
67+
To run deployment, you need to have AWS credentials set up on your machine.
68+
69+
Run deployment if you have AWS credentials in your environment variables:
70+
71+
```bash
72+
$ npm run build
73+
$ npm run deploy
74+
```
75+
76+
If you set up AWS Profile in your `~/.aws/credentials` file, you can run deployment with the following command:
77+
78+
```bash
79+
$ npm run build
80+
$ npm run deploy -- --profile <profile-name>
81+
```
82+
83+
When deployment is done, you can find the url of deployed API in the output of the command.
84+
85+
```
86+
Outputs:
87+
nodejs-aws-cart-api.Url = <url>
88+
```
89+
90+
#### How it works
91+
92+
When you run `npm run deploy`, cdk bundles all your code into a single js file using `esbuild`, uploads it to S3, generates CloudFormation template representing the change and deploying it to the AWS account.
93+
94+
### Notes
95+
96+
Due to how NestJS is written, some modules are marked as "external" and are not included in the final bundle:
97+
98+
- @nestjs/microservices
99+
- @nestjs/websockets
100+
- class-transformer
101+
- class-validator
102+
103+
If you want to use any of this modules, you need to add them to package.json, install and remove from `deploy/Stack.ts`.
104+
63105
## Support
64106

65107
Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please [read more here](https://docs.nestjs.com/support).
@@ -72,4 +114,4 @@ Nest is an MIT-licensed open source project. It can grow thanks to the sponsors
72114

73115
## License
74116

75-
Nest is [MIT licensed](LICENSE).
117+
Nest is [MIT licensed](LICENSE).

cdk.json

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"app": "npx ts-node deploy/App.ts",
3+
"context": {
4+
"@aws-cdk/aws-apigateway:usagePlanKeyOrderInsensitiveId": true,
5+
"@aws-cdk/core:stackRelativeExports": true,
6+
"@aws-cdk/aws-lambda:recognizeVersionProps": true,
7+
"@aws-cdk/aws-cloudfront:defaultSecurityPolicyTLSv1.2_2021": true,
8+
"@aws-cdk/core:target-partitions": ["aws", "aws-cn"]
9+
}
10+
}

deploy/App.ts

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import * as cdk from 'aws-cdk-lib';
2+
3+
import { Stack } from './Stack';
4+
5+
const app = new cdk.App({});
6+
7+
new Stack(app, 'nodejs-aws-cart-api', {
8+
env: {
9+
region: 'eu-central-1',
10+
},
11+
});

deploy/Stack.ts

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import * as cdk from 'aws-cdk-lib';
2+
import * as lambda from 'aws-cdk-lib/aws-lambda';
3+
import * as nodejs from 'aws-cdk-lib/aws-lambda-nodejs';
4+
import { Construct } from 'constructs';
5+
6+
export class Stack extends cdk.Stack {
7+
constructor(scope: Construct, id: string, props: cdk.StackProps) {
8+
super(scope, id, props);
9+
10+
const server = new nodejs.NodejsFunction(this, 'server', {
11+
functionName: 'nodejs-aws-cart-api',
12+
entry: 'dist/main.lambda.js',
13+
timeout: cdk.Duration.seconds(30),
14+
memorySize: 1024,
15+
runtime: lambda.Runtime.NODEJS_20_X,
16+
environment: {
17+
RDS_CONNECTION_URL: '<UPDATED_ME>',
18+
},
19+
bundling: {
20+
externalModules: [
21+
'@nestjs/microservices',
22+
'@nestjs/websockets',
23+
'class-transformer',
24+
'class-validator',
25+
],
26+
},
27+
});
28+
29+
// exposes the lambda function via HTTP URL
30+
const { url } = server.addFunctionUrl({
31+
authType: lambda.FunctionUrlAuthType.NONE,
32+
cors: { allowedOrigins: ['*'] },
33+
});
34+
35+
new cdk.CfnOutput(this, 'Url', { value: url });
36+
}
37+
}

0 commit comments

Comments
 (0)