This repository provides a general DevOps practices such MSA, IaC, CICD and Monitoring. AWS various services are used to provide DevOps best practices. All necessary cloud resources are modeled and deployed through AWS CDK.
Other "Using AWS CDK" series can be found at:
- Amazon Sagemaker Model Serving Using AWS CDK
- AWS IoT Greengrass Ver2 using AWS CDK
- AWS Serverless Using AWS CDK
- Container-based MSA: each micro-services are implemented using AWS ECS(Cluster/Service/Task)
- Programming-based IaC: all cloud resources are modeld and provisioned using AWS CDK(Typescript)
- Fully managed CICD: Continuous integration and continuous deploy using AWS Code Series(Pipeline/Commit/Build/Deploy)
- Fully managed Monitoring: logging, metric, dashboard using Amazon CloudWatch
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, typescrip offers a very nice combination with AWS CDK.
Caution: This solution contains not-free tier AWS services. So be careful about the possible costs.
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
- jq: jq --version
Please refer to the kind guide in CDK Workshop.
aws configure --profile [your-profile]
AWS Access Key ID [None]: xxxxxx
AWS Secret Access Key [None]:yyyyyyyyyyyyyyyyyyyyyyyyyyyyyy
Default region name [None]: us-east-2
Default output format [None]: json
aws sts get-caller-identity --profile [your-profile]
...
...
{
"UserId": ".............",
"Account": "75157*******",
"Arn": "arn:aws:iam::75157*******:user/[your IAM User ID]"
}
The cdk.json
file tells CDK Toolkit how to execute your app.
The config/app-config-demo.json
files tell 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": "75157*******",
"Region": "us-east-2",
"Profile": "cdk-demo"
},
...
...
}
And then set the path of the configuration file through an environment variable.
export APP_CONFIG=config/app-config-demo.json
sh ./script/setup_initial.sh config/app-config-demo.json
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-EcsAlbStack
EcsProjectDemo-VpcInfraStack
...
...
Check if you can see the list of stacks as shown above.
If there is no problem, finally run the following command.
sh ./script/deploy_stacks.sh config/app-config-demo.json
You can find the deployment results in AWS CloudFormation as shown in the following picture.
Open a web-browser and enter LoadBalancer's domain name(which is the output of sh script/deploy_stacks
) to see the following screen.
...
...
Outputs:
EcsProjectDemo-EcsAlbStack.EcsAlbInfraConstrunctServiceLoadBalancerDNSF445CBCD = EcsPr-EcsAl-1TNJ82PAWJ4IV-1937786873.us-east-2.elb.amazonaws.com
EcsProjectDemo-EcsAlbStack.EcsAlbInfraConstrunctServiceServiceURL290953F6 = http://EcsPr-EcsAl-1TNJ82PAWJ4IV-1937786873.us-east-2.elb.amazonaws.com
...
...
The initial screen is a php sample screen(in public DockerHub) as we haven't uploaded the source code yet.
AWS CDK made CodeCommit(git) repository for you. Please visit CodeCommit in AWS management web console, note remote address of that.
Finally add git remote address in your development environment. Thease codes are sample for you.
git remote -v
git remote add [your-new-remote-origin-name] [your-codecommit-address]
After modifying logic codes(in this repository, logic codes are in codes/sample-flask-web
), push only that to AWS CodeCommit. Please commit this directory only, because each micro-servie has its own repository. This will trigger AWS CodePipeline and automatically re-deploy the new container image to AWS ECS Service/Task. You have to click Review
button in Approve stage.
Caution: If you have modified this path( codes/sample-flask-web
), please reflect the changed path in config/app-config-demo.son
file.
...
...
"EcsAlb": {
"Name": "EcsAlbStack",
"InfraVersion": "'1.0.0'",
"PortNumber": 80,
"AppPath": "codes/sample-flask-web", <------ Here
"RepoName": "sample-flask-web",
"DashboardName": "SampleFlaskWeb"
},
...
...
After provisioning, you can check the updated web page like the following screen.
Also you can check a realtime dashboard of cloud resources in Amazon CloudWatch Dashboard.
Just add a new stack configuration in config/app-config-demo.json
.
And then instantiate Class
like the following codes.
Since AWS CDK(Typescript, Java, Python, ...) supports object-oriented programming language, you can create a new resource simply by instantiating the object without copying the code. This is one of the great advantages of AWS CDK.
npm install
install dependenciescdk list
list up stackscdk deploy
deploy this stack to your default AWS account/regioncdk diff
compare deployed stack with current statecdk synth
emits the synthesized CloudFormation template
Execute the following command, which will destroy all resources except ECR-Repository and DynamoDB Tables. So destroy these resources in AWS web console manually.
sh ./script/destroy_stacks.sh config/app-config-demo.json
See CONTRIBUTING for more information.
The documentation is made available under the Creative Commons Attribution-ShareAlike 4.0 International License. See the LICENSE file.
The sample code within this documentation is made available under the MIT-0 license. See the LICENSE-SAMPLECODE file.