The AWS Cloud Development Kit (AWS CDK) is an open-source software development framework to define cloud infrastructure in code and provision it through AWS CloudFormation.
This example uses the AWS CDK to achieve the following:
- Dockerize
my-service
(a simple express server), and push it to Amazon ECR - Create a VPC with 2 AZs (Availability Zones)
- Create an AWS Fargate service using the Dockerized image from AWS ECR
- Deploy service to an Amazon ECS cluster with 2 running tasks
- Expose service with an internet-facing Application Load Balancer
- Build a CI/CD pipeline to automate the entire process (using this GitHub repository as the source)
Install the CDK and dependencies first.
# Install the AWS CDK
npm install -g aws-cdk
# Install the dependencies for the cdk app
cd cdk
npm install
# Rename .env.sample and add your environment variables
mv .env.sample .env
A GitHub personal access token is required for the CI server to pull from your repo. To configure a personal access token:
- Generate a Github access token for yourself: https://github.com/settings/tokens
- Add the token to the
GITHUB_TOKEN
environment variable incdk/.env
:
To build the app, you need to be in this demos's cdk
folder. Then run the following:
npm run build
# Generate CloudFormation templates
cdk synth
cdk deploy PipelineStack
This will deploy just the PipelineStack
- which will then trigger the CI/CD pipeline to build the EcsFargateStack
and deploy it to AWS.
To avoid unexpected AWS charges, destroy your AWS CDK stacks after you're done with this demo.
cdk destroy "*"
The cdk.json
file in the root of this repository includes
instructions for the CDK toolkit on how to execute this program.
After building your TypeScript code, you will be able to run the CDK toolkits commands as usual:
$ cdk ls
<list all stacks in this program>
$ cdk synth
<generates and outputs cloudformation template>
$ cdk deploy
<deploys stack to your account>
$ cdk diff
<shows diff against deployed stack>
See aws-samples/aws-cdk-examples for more example projects using the AWS CDK.