Tech stacks:
- NestJS: 1 app, 2 microservices with healthcheck
- Docker
- Github action workflow for ECS services
- AWS CDK typescript
- ECS
- VPC + NAT gateway
- Api gateway
- Network load balancing/Cloudmap
- Service connect
- ECR
- VPC
- Cloudwatch
- ECS
- ElasticCache
- RDS (Postgres)
To efficiently define and provision AWS cloud resources, AWS Cloud Development Kit(CDK) which is an open source software development framework to define your cloud application resources using familiar programming languages is utilized.
Because this solusion is implemented in CDK, we can deploy these cloud resources using CDK CLI. Among the various languages supported, this solution used typescript. Because the types of typescript are very strict, with the help of auto-completion, typescript offers a very nice combination with AWS CDK.
npm installinstall dependenciescdk listlist up stackscdk deploydeploy this stack to your default AWS account/regioncdk diffcompare deployed stack with current statecdk synthemits the synthesized CloudFormation template
First of all, AWS Account and IAM User is required. And then the following modules must be installed.
- AWS CLI: aws configure --profile [profile name]
- Node.js: node --version
- AWS CDK: cdk --version
Please configure your AWS credential to grant AWS roles to your develop PC.
aws configure --profile [your-profile]
AWS Access Key ID [None]: xxxxxx
AWS Secret Access Key [None]:yyyyyy
Default region name [None]: ap-southeast-1
Default output format [None]: json
...
...If you don't know your AWS account information, execute the following commad:
aws sts get-caller-identity --profile [your-profile]
...
...
{
"UserId": ".............",
"Account": "1234*******",
"Arn": "arn:aws:iam::1234*******:user/[your IAM User ID]"
}The cdk.json file tells CDK Toolkit how to execute your app. Our current entry point for our project is src/main.ts.
The config/app-config-demo.json file describes how to configure deploy condition & stack condition. First of all, change project configurations(Account, Profile are essential) in config/app-config-demo.json.
{
"Project": {
"Name": "EcsProject",
"Stage": "Demo",
"Account": "1234*******",
"Region": "ap-southeast-1",
"Profile": "cdk-demo"
},
...
...
}And then set the path of the configuration file through an environment variable.
export APP_CONFIG=config/app-config-demo.jsonsh scripts/setup_initial.sh config/app-config-demo.jsonThis project has 1 stacks, each of which does the following:
- EcsProjectDemo-VpcInfraStack: VPC, ECS Cluster, CloudMap Namespace for a base infrastructure
config/app-config-demo.json file describes how to configure each stack. For example backend's configuration is like this.
...
...
"Stack": {
"VpcInfra": {
"Name": "VpcInfraStack",
"VPCName": "CommonVPC",
"VPCMaxAzs": 3,
"VPCCIDR": "10.0.0.0/16",
"NATGatewayCount": 0,
"ECSClusterName": "MainCluster"
}
}
...
...Before deployment, check whether all configurations are ready. Please execute the following command:
cdk list
...
...
==> CDK App-Config File is config/app-config-demo.json, which is from Environment-Variable.
EcsProjectDemo-VpcInfraStack
...
...Check if you can see a list of stacks as shown above.
sh scripts/deploy_stacks.sh config/app-config-demo.jsonCaution: This solution contains not-free tier AWS services. So be careful about the possible costs.
Now you can find deployment results in AWS CloudFormation.
