Skip to content

Commit

Permalink
Create aws cdk fargate stack with dynamodb
Browse files Browse the repository at this point in the history
  • Loading branch information
ernitingarg committed Jan 8, 2024
1 parent 8322f17 commit 7b5d70c
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 37 deletions.
10 changes: 10 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
PORT=5000

PROJECT_NAME=demo
TABLE_NAME=sample_table

AWS_DEFAULT_ACCOUNT=975050344965
AWS_DEFAULT_REGION=ap-northeast-1

AWS_FARGATE_CONTAINER_CPU=512
AWS_FARGATE_CONTAINER_MEMORY=1024
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@ docker build -t backend_image .
docker run -p 3000:3000 --name backend_app backend_image
```

- Deployment

```
npm run build
cdk bootstrap
cdk synth
cdk deploy --require-approval never
cdk destroy -f
```

## Useful commands

- `npm run build` compile typescript to js
Expand Down
21 changes: 0 additions & 21 deletions bin/aws-cdk-typescript.ts

This file was deleted.

10 changes: 10 additions & 0 deletions bin/aws-cdk.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env node
import "source-map-support/register";
import * as cdk from "aws-cdk-lib";
import { FargateStack } from "../lib/fargate";
import env from "../backend/src/config";

const app = new cdk.App();
new FargateStack(app, "FargateStack", {
env: { account: env.AWS_DEFAULT_ACCOUNT, region: env.AWS_DEFAULT_REGION },
});
7 changes: 7 additions & 0 deletions cdk.context.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"availability-zones:account=975050344965:region=ap-northeast-1": [
"ap-northeast-1a",
"ap-northeast-1c",
"ap-northeast-1d"
]
}
16 changes: 0 additions & 16 deletions lib/aws-cdk-typescript-stack.ts

This file was deleted.

74 changes: 74 additions & 0 deletions lib/fargate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import env from "../backend/src/config";
import * as cdk from "aws-cdk-lib";
import { Construct } from "constructs";
import {
aws_dynamodb as dynamodb,
aws_ec2 as ec2,
aws_ecs as ecs,
aws_ecs_patterns as ecs_patterns,
} from "aws-cdk-lib";

export class FargateStack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);

// Create VPC
const vpc = new ec2.Vpc(this, `${env.PROJECT_NAME}_vpc`, {
maxAzs: 2,
});

// Create fargate cluster
const cluster = new ecs.Cluster(this, `${env.PROJECT_NAME}_cluster`, {
vpc: vpc,
});

// Create dynamodb table
const table = new dynamodb.Table(this, `${env.TABLE_NAME}`, {
partitionKey: {
name: "Id",
type: dynamodb.AttributeType.STRING,
},
billingMode: dynamodb.BillingMode.PAY_PER_REQUEST,
removalPolicy: cdk.RemovalPolicy.DESTROY,
});

// Create ecs fargate service
const fargateService =
new ecs_patterns.ApplicationLoadBalancedFargateService(
this,
`${env.PROJECT_NAME}_ecs`,
{
cluster: cluster,
cpu: Number(env.AWS_FARGATE_CONTAINER_CPU),
memoryLimitMiB: Number(env.AWS_FARGATE_CONTAINER_MEMORY),
desiredCount: 1,
taskImageOptions: {
image: ecs.ContainerImage.fromAsset("backend/"),
environment: {
PORT: `${env.PORT}`,
AWS_DYNAMODB_TABLE_NAME: table.tableName,
},
containerPort: Number(env.PORT),
},
}
);

// Health check
fargateService.targetGroup.configureHealthCheck({
path: "/healthcheck",
});

// Grant ECS task permissions to access the DynamoDB table
table.grantReadWriteData(fargateService.taskDefinition.taskRole);

// Output load balancer url
new cdk.CfnOutput(this, "LoadBalancerDNS", {
value: fargateService.loadBalancer.loadBalancerDnsName,
});

// Output the DynamoDB table name
new cdk.CfnOutput(this, "DynamoDBTableName", {
value: table.tableName,
});
}
}

0 comments on commit 7b5d70c

Please sign in to comment.