Simple AWS ECS Deployment With AWS CDK
AWS CDK provides a library of constructs in many programming languages to easily automate AWS infrastructure. In addition, these constructs can be customized and be used to provision your application as required.
If you have a production application running on any cloud platform, you must think of automating the cloud infrastructure.
Let's deploy the service created in a previous post. Note that I have a health check endpoint, such that the Load balancer can call this endpoint to ensure the container that runs this API is healthy.
I will still Dockerize the API and this is the Dockerfile that will create a docker image of the API.
FROM alpine:latest
RUN apk add --no-cache nodejs npm
WORKDIR /app
COPY package.json /app
RUN npm install
COPY . /app
EXPOSE 1000
CMD ["npm", "start"]
Overview of the Infrastructure
I need to provision several resources in AWS in order to get the API running.
- A VPC (virtual private cloud) to isolate our application within its own network.
- A load balancer to route traffic to the API (the running container)
- An AWS ECS cluster to run the API. Within the cluster I will create a task definition that define the API and a service to run the task definition.
Installing CDK toolkit
I am going to use TypeScript to provision cloud resources using CDK. Therefore make sure you have NodeJS (>= 10.3.0) installed on your computer as well, if you wish to follow along. NodeJS installation is going to install the Node Package Manager(npm) which is needed to install CDK toolkit:
A few commands to be aware of as we continue building the infrastructure:
- If this is your first time using the CDK you'll need to run
cdk bootstrap
cdk init --language typescript
initialize a new typescript CDK applicationcdk synth
generates the CloudFormation template for your infrastructure. This template eventually ends up in AWS CloudFormation as a stack that creates all the resources specified in the template.cdk diff
inspect the differences between a currently deployed stack and any changes made locally. I do this before deploying because there are some useful information printed to the terminal.cdk deploy
deploy the current CDK application to AWS CloudFormation. CloudFormation will then use the template to create the necessary AWS resources.cdk destroy
destroy all the resource created by CloudFormation.
Initializing a CDK project
Now let’s create a new CDK project using the CDK cli. I’ll create a new directory and issue the cdk init command.
// Creating a new directory and change into that directory
mkdir cdk-ecs-demo
cd cdk-ecs-demo
// Initializing a cdk typescript project
cdk init --language typescript
At the root level of the CDK project we created earlier, you should find the lib folder. Let’s create a file called, fargate.ts inside this folder. The code is in this repo.
Let's dig in and see what this code is going to create in AWS
- A VPC with 2 availability zones and all associated networking by default. This includes, public and private subnets, internet gateway, NAT gateway, routing tables, etc.
- An ECS cluster
- An internet facing (public) load balancer
- Fetching the image pushed to AWS ECR
- Create a task definition
- Attach a container to the task definition
- Configure port mappings for the container
- Create the ECS service to run the container
- Create a listener for the load balancer
- Route traffic from the listener to the service
Deploying the Infrastructure to AWS
Finally, I can deploy the infrastructure to AWS. I can update the bin/cdk.ts file to execute the CDK stack when running cdk deploy
#!/usr/bin/env node
import 'source-map-support/register';
import * as cdk from '@aws-cdk/core';
import { FargateDemoStack } from '../lib/fargate'
const app = new cdk.App();
new FargateDemoStack(app, "FargateDemoStack", {
env: { account: "<account-id>", region: "<region>"}
});
Before deploying the stack we can inspect the resources that will be created by the CDK. cdk diff
will output a bunch of text explaining roles and security group changes, and a list of all the resources that will be created. cdk deploy
will generate a CloudFormation template, push it to AWS then begin creating the infrastructure on AWS. Remember, the CDK is all CloudFormation under the hood.
Once the stack is deployed, go to the AWS load balancer console on AWS, find the load balancer DNS and copy, then paste that into your URL bar to see the service response.
Run cdk destroy
to destroy all the resources created in AWS. Checkout the code in this repo.