typescript / aws-ecs-devops-using-aws-cdk

This repository provides a general DevOps practices such MSA, IaC, CICD and Monitoring. AWS various services are used to provide DevOps best practices.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

AWS ECS DevOps using AWS CDK

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:

Solution Architecture

  • 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

solution-arcitecture

CDK-Project Build & Deploy

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.

AWSCDKIntro

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.

Prerequisites

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.

Configure AWS Credential

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]"
}

Check cdk project's default launch config

The cdk.json file tells CDK Toolkit how to execute your app.

Set up deploy config

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

Install dependecies & Bootstrap

sh ./script/setup_initial.sh config/app-config-demo.json

Deploy stacks

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. cloudformation-stacks

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
...
...

initial-web-page The initial screen is a php sample screen(in public DockerHub) as we haven't uploaded the source code yet.

How to update logic

AWS CDK made CodeCommit(git) repository for you. Please visit CodeCommit in AWS management web console, note remote address of that. codecommit-repo

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.

code-pipeline

code-pipeline

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. new-web-page

Also you can check a realtime dashboard of cloud resources in Amazon CloudWatch Dashboard.

dashboard-one

How to add a new service

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.

CDK Useful commands

  • npm install install dependencies
  • cdk list list up stacks
  • cdk deploy deploy this stack to your default AWS account/region
  • cdk diff compare deployed stack with current state
  • cdk synth emits the synthesized CloudFormation template

How to clean up

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

Security

See CONTRIBUTING for more information.

License Summary

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.

About

This repository provides a general DevOps practices such MSA, IaC, CICD and Monitoring. AWS various services are used to provide DevOps best practices.

License:Other


Languages

Language:TypeScript 86.7%Language:Python 5.5%Language:Shell 4.1%Language:HTML 1.6%Language:JavaScript 1.4%Language:Dockerfile 0.7%