-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create aws cdk fargate stack with dynamodb
- Loading branch information
1 parent
8322f17
commit 7b5d70c
Showing
7 changed files
with
111 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}); | ||
} | ||
} |