Example how with AWS CDK you can deploy a continuous delivery pipeline using AWS CodePipeline, AWS CodeBuild and AWS Fargate. I have included all the best practices with a strong focus on the KISS principle. The infrastructure code is written in TypeScript. The infrastructure is a sidecar with Nginx as proxy and a Flask "hello world" application on Gunicorn
code
docker
infrastructure
dedicated to Flask code
dedicated to Docker definitions: sidecard of Nginx + Gunicorn
dedicated to AWS CDK infrastructure definition
cd infrastructure
### Install the CDK framework
npm install -g aws-cdk
npm install
### Authenticate in your AWS account:
Follow this guide: Configuring the AWS CLI
Create a personal access token in GitHub and store it in AWS SecretsManager. Needed to configure your repo webhooks.
aws secretsmanager create-secret \
--name my_secret_token \
--secret-string yourtokenhereyourtokenhere \
--region eu-west-1
The first step is to exporting the AWS variables to obtain the rights:
export AWS_PROFILE="profilename"
export AWS_DEFAULT_REGION="eu-west-1"
edit the app_config.json file for defining the project name and the existing VPC
{
"PROJECT_NAME": "awsome",
"VPC_NAME": "default"
}
You can create a continuous integration service bonded to your current git branch.
Suppose that you are in the master branch:
git branch --show-current
master
You can create the pipeline triggerable from any commit to master branch:
cdk deploy "*" --context tier=pipeline
✅ awsome-master-pipeline
Outputs:
awsome-master-pipeline.LinkCodePipelinePage = https://eu-west-1.console.aws.amazon.com/codesuite/codepipeline/pipelines/awsome-master-pipeline-PipelineC660917D-11U99LG5Y4H4V/view?region=eu-west-1
The pipeline after the creation and by every commits in the branch will be triggered. It launches the staging env, after a manual approval, the production env.
Alternatively you can deploy staging env directly without passing by codepipeline and get the staging http endpoints:
cdk deploy "*" --context tier=stg
✅ awsome-master-stg-app
Outputs:
awsome-master-stg-app.fargateLoadBalancerDNSB13ECB0B = awsom-farga-1KNVPTS0GNV8J-XXXXXXXXX.eu-west-1.elb.amazonaws.com
awsome-master-stg-app.LinkEcsClusterPage = https://eu-west-1.console.aws.amazon.com/ecs/home?region=eu-west-1#/clusters/awsome-master-stg-app-cluster611F8AFF-okLxuoDdfc1o/fargateServices
awsome-master-stg-app.LinkCLoudWatchDashboard = https://eu-west-1.console.aws.amazon.com/cloudwatch//home?region=eu-west-1#dashboards:name=awsome-dashboard-stg-app
awsome-master-stg-app.fargateServiceURL145CCBE8 = http://awsom-farga-1KNVPTS0GNV8J-XXXXXXXXX.eu-west-1.elb.amazonaws.com
or you can deploy the production env and get the production http endpoints:
cdk deploy "*" --context tier=prd
✅ awsome-master-prd-app
Outputs:
awsome-master-prd-app.fargateLoadBalancerDNSB13ECB0B = awsom-farga-1KNVPTS0GNV8J-XXXXXXXXX.eu-west-1.elb.amazonaws.com
awsome-master-prd-app.LinkEcsClusterPage = https://eu-west-1.console.aws.amazon.com/ecs/home?region=eu-west-1#/clusters/awsome-master-prd-app-cluster611F8AFF-okLxuoDdfc1o/fargateServices
awsome-master-prd-app.LinkCLoudWatchDashboard = https://eu-west-1.console.aws.amazon.com/cloudwatch//home?region=eu-west-1#dashboards:name=awsome-master-prd-app
awsome-master-prd-app.fargateServiceURL145CCBE8 = http://awsom-farga-1KNVPTS0GNV8J-XXXXXXXXX.eu-west-1.elb.amazonaws.com
You can customize the code inside the docker/code directory