Skip to content

Commit c1ce4f7

Browse files
committed
Create aws cdk fargate stack with dynamodb
1 parent 8322f17 commit c1ce4f7

File tree

8 files changed

+127
-37
lines changed

8 files changed

+127
-37
lines changed

.env.example

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
PORT=5000
2+
3+
PROJECT_NAME=demo
4+
TABLE_NAME=sample_table
5+
6+
AWS_FARGATE_CONTAINER_CPU=512
7+
AWS_FARGATE_CONTAINER_MEMORY=1024

.eslintrc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"root": true,
3+
"parser": "@typescript-eslint/parser",
4+
"plugins": [
5+
"@typescript-eslint"
6+
],
7+
"extends": [
8+
"eslint:recommended",
9+
"plugin:@typescript-eslint/eslint-recommended",
10+
"plugin:@typescript-eslint/recommended"
11+
],
12+
"rules": {
13+
"@typescript-eslint/interface-name-prefix": "off",
14+
"@typescript-eslint/explicit-function-return-type": "off",
15+
"@typescript-eslint/explicit-module-boundary-types": "off",
16+
"@typescript-eslint/no-explicit-any": "off",
17+
"@typescript-eslint/no-unused-vars": "off"
18+
}
19+
}

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,16 @@ docker build -t backend_image .
5252
docker run -p 3000:3000 --name backend_app backend_image
5353
```
5454

55+
- Deployment
56+
57+
```
58+
npm run build
59+
cdk bootstrap
60+
cdk synth
61+
cdk deploy --require-approval never
62+
cdk destroy -f
63+
```
64+
5565
## Useful commands
5666

5767
- `npm run build` compile typescript to js

bin/aws-cdk-typescript.ts

Lines changed: 0 additions & 21 deletions
This file was deleted.

bin/aws-cdk.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/env node
2+
import "source-map-support/register";
3+
import * as cdk from "aws-cdk-lib";
4+
import { FargateStack } from "../lib/fargate";
5+
import env from "../backend/src/config";
6+
7+
const app = new cdk.App();
8+
new FargateStack(app, "FargateStack", {
9+
env: { account: env.AWS_ACCOUNT, region: env.AWS_REGION },
10+
});

cdk.context.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"availability-zones:account=975050344965:region=ap-northeast-1": [
3+
"ap-northeast-1a",
4+
"ap-northeast-1c",
5+
"ap-northeast-1d"
6+
]
7+
}

lib/aws-cdk-typescript-stack.ts

Lines changed: 0 additions & 16 deletions
This file was deleted.

lib/fargate.ts

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import env from "../backend/src/config";
2+
import * as cdk from "aws-cdk-lib";
3+
import { Construct } from "constructs";
4+
import {
5+
aws_dynamodb as dynamodb,
6+
aws_ec2 as ec2,
7+
aws_ecs as ecs,
8+
aws_ecs_patterns as ecs_patterns,
9+
} from "aws-cdk-lib";
10+
11+
export class FargateStack extends cdk.Stack {
12+
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
13+
super(scope, id, props);
14+
15+
// Create VPC
16+
const vpc = new ec2.Vpc(this, `${env.PROJECT_NAME}_vpc`, {
17+
maxAzs: 2,
18+
});
19+
20+
// Create fargate cluster
21+
const cluster = new ecs.Cluster(this, `${env.PROJECT_NAME}_cluster`, {
22+
vpc: vpc,
23+
});
24+
25+
// Create dynamodb table
26+
const table = new dynamodb.Table(this, `${env.TABLE_NAME}`, {
27+
partitionKey: {
28+
name: "Id",
29+
type: dynamodb.AttributeType.STRING,
30+
},
31+
billingMode: dynamodb.BillingMode.PAY_PER_REQUEST,
32+
removalPolicy: cdk.RemovalPolicy.DESTROY,
33+
});
34+
35+
// Create ecs fargate service
36+
const fargateService =
37+
new ecs_patterns.ApplicationLoadBalancedFargateService(
38+
this,
39+
`${env.PROJECT_NAME}_ecs`,
40+
{
41+
cluster: cluster,
42+
cpu: Number(env.AWS_FARGATE_CONTAINER_CPU),
43+
memoryLimitMiB: Number(env.AWS_FARGATE_CONTAINER_MEMORY),
44+
desiredCount: 1,
45+
taskImageOptions: {
46+
image: ecs.ContainerImage.fromAsset("backend/"),
47+
environment: {
48+
PORT: `${env.PORT}`,
49+
AWS_DYNAMODB_TABLE_NAME: table.tableName,
50+
},
51+
containerPort: Number(env.PORT),
52+
},
53+
}
54+
);
55+
56+
// Health check
57+
fargateService.targetGroup.configureHealthCheck({
58+
path: "/healthcheck",
59+
});
60+
61+
// Grant ECS task permissions to access the DynamoDB table
62+
table.grantReadWriteData(fargateService.taskDefinition.taskRole);
63+
64+
// Output load balancer url
65+
new cdk.CfnOutput(this, "LoadBalancerDNS", {
66+
value: fargateService.loadBalancer.loadBalancerDnsName,
67+
});
68+
69+
// Output the DynamoDB table name
70+
new cdk.CfnOutput(this, "DynamoDBTableName", {
71+
value: table.tableName,
72+
});
73+
}
74+
}

0 commit comments

Comments
 (0)