AWS Compute Blog
Getting started with the AWS Cloud Development Kit for Amazon ECS
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. The AWS CDK integrates fully with AWS services and offers a higher-level object-oriented abstraction to define AWS resources imperatively.
Using the AWS CDK library of infrastructure constructs, you can easily encapsulate AWS best practices in your infrastructure definition and share it without worrying about boilerplate logic. The AWS CDK improves the end-to-end development experience because you get to use the power of modern programming languages to define your AWS infrastructure in a predictable and efficient manner. The AWS CDK is currently available for TypeScript, JavaScript, Java, and .NET.
The AWS CDK now includes constructs for ECS resources, allowing you to deploy a fully functioning containerized application environment on AWS with just a few lines of simple, readable code. Here’s how it works.
Install the AWS CDK
The first step is to install the AWS CDK on your development machine:
mkdir greeter-cdk
cd greeter-cdk
npm init -y
npm install @aws-cdk/cdk
npm install -g aws-cdk
Next, write some JavaScript code that imports the AWS CDK library and uses it to define a skeleton that you can place all your resources in:
index.js
const cdk = require('@aws-cdk/core');
class GreetingStack extends cdk.Stack {
constructor(parent, id, props) {
super(parent, id, props);
}
}
const app = new cdk.App();
const greeting = new GreetingStack(app, 'greeting-stack');
app.synth();
Next, write a small configuration file telling the AWS CDK CLI that index.js is the code that defines your application stack:
cdk.json
{
"app": "node index.js"
}
Now if you run cdk ls –l
, you can see that the AWS CDK has found your stack, and has automatically interpolated some details about it from your development machine’s environment, such as your AWS account ID and default Region:
$ cdk ls -l
- name: greeting-stack
environment:
name: 209640446841/us-east-1
account: '209640446841'
region: us-east-1
Add ECS constructs
It’s time to add some ECS constructs to your stack. To do this, first install the ECS construct library. Also, install a couple of other constructs to help you set up resources linked to your containers:
npm install @aws-cdk/aws-ecs
npm install @aws-cdk/aws-ec2
npm install @aws-cdk/aws-elasticloadbalancingv2
Now it’s time to use the ECS constructs to set up your application environment:
const cdk = require('@aws-cdk/cdk');
const ecs = require('@aws-cdk/aws-ecs');
const ec2 = require('@aws-cdk/aws-ec2');
class GreetingStack extends cdk.Stack {
constructor(parent, id, props) {
super(parent, id, props);
const vpc = new ec2.Vpc(this, 'GreetingVpc', { maxAZs: 2 });
// Create an ECS cluster
const cluster = new ecs.Cluster(this, 'Cluster', { vpc });
// Add capacity to it
cluster.addCapacity('greeter-capacity', {
instanceType: new ec2.InstanceType('t3.xlarge'),
minCapacity: 3,
maxCapacity: 3
});
}
}
With just three calls, you can create a VPC to hold all your application resources, and an ECS cluster with three t3.xlarge instances. All it takes is one command to tell the AWS CDK to automatically deploy this stack on your account:
cdk deploy
Behind the scenes, the AWS CDK synthesizes your JavaScript calls into a CloudFormation template. It asks CloudFormation to deploy the resources described in the synthesized template. You can see a live log of each resource that is being created and what the status is. As you can see from the numbers on the left side of the message stream, those three simple commands added to the AWS CDK stack automatically expanded into 32 lower-level, primitive resources to be created on your AWS account.
After the AWS CDK deployment finishes, you have a fresh ECS cluster ready to run your services. Next you will deploy a simple microservices stack onto this cluster. Your application will be a simple greeting server. The frontend greeter service fetches a random greeting and name from two backend services. There are two tiers to this application: the frontend and backend. The network will look like the following diagram:
There are two load balancers, one of them allows anyone on the internet to talk to your greeter service. The other is internal and designed to allow the greeter service to talk to the other greeting and name services.
In total, you need to add five more high-level constructs to your AWS CDK application: two load balancers and three services.
Add a new ECS service
Adding a new ECS service to the application stack is easy. Define a task definition, add a container to it, and tell the AWS CDK to turn the task definition into a service:
// Name service
const nameTaskDefinition = new ecs.Ec2TaskDefinition(this, 'name-task-definition', {});
const nameContainer = nameTaskDefinition.addContainer('name', {
image: ecs.ContainerImage.fromRegistry('nathanpeck/name'),
memoryLimitMiB: 128
});
nameContainer.addPortMappings({
containerPort: 3000
});
const nameService = new ecs.Ec2Service(this, 'name-service', {
cluster: cluster,
desiredCount: 2,
taskDefinition: nameTaskDefinition
});
// Greeting service
const greetingTaskDefinition = new ecs.Ec2TaskDefinition(this, 'greeting-task-definition', {});
const greetingContainer = greetingTaskDefinition.addContainer('greeting', {
image: ecs.ContainerImage.fromRegistry ('nathanpeck/greeting'),
memoryLimitMiB: 128
});
greetingContainer.addPortMappings({
containerPort: 3000
});
const greetingService = new ecs.Ec2Service(this, 'greeting-service', {
cluster: cluster,
desiredCount: 2,
taskDefinition: greetingTaskDefinition
});
Just like that, you’ve defined two different ECS services that run by loading a public image from Docker Hub. The next step is to create a load balancer and add the services to it:
// Internal load balancer for the backend services
const internalLB = new elbv2.ApplicationLoadBalancer(this, 'internal', {
vpc: vpc,
internetFacing: false
});
const internalListener = internalLB.addListener('PublicListener', { port: 80, open: true });
internalListener.addTargetGroups('default', {
targetGroups: [new elbv2.ApplicationTargetGroup(this, 'default', {
vpc: vpc,
protocol: 'HTTP',
port: 80
})]
});
internalListener.addTargets('name', {
port: 80,
pathPattern: '/name*',
priority: 1,
targets: [nameService]
});
internalListener.addTargets('greeting', {
port: 80,
pathPattern: '/greeting*',
priority: 2,
targets: [greetingService]
});
For this configuration, the code defines a single load balancer with a single listener on port 80, but adds two different services behind it. If the path of the request looks like /name
, it sends the request to your name service. If it looks like /greeting
, it sends the request to the greeting service.
Finally, add the frontend greeter service, which constructs a random greeting phrase by fetching a random name from the name service and a random greeting from the greeting service. To do this, configure the greeter service to know how to make requests to the other two backend services:
// Greeter service
const greeterTaskDefinition = new ecs.Ec2TaskDefinition(this, 'greeter-task-definition', {});
const greeterContainer = greeterTaskDefinition.addContainer('greeter', {
image: ecs.ContainerImage.fromRegistry ('nathanpeck/greeter'),
memoryLimitMiB: 128,
environment: {
GREETING_URL: 'http://' + internalLB.loadBalancerDnsName + '/greeting',
NAME_URL: 'http://' + internalLB.loadBalancerDnsName + '/name'
}
});
greeterContainer.addPortMappings({
containerPort: 3000
});
const greeterService = new ecs.Ec2Service(this, 'greeter-service', {
cluster: cluster,
desiredCount: 2,
taskDefinition: greeterTaskDefinition
});
The AWS CDK has a powerful capability to resolve expressions that you enter in your JavaScript and turn them into a CloudFormation template that resolves the correct values.
In this example, you create a reference to the DNS name of the load balancer, and indicate that you want to assign the following:
· Environment variable = 'NAME_URL'
· Value = 'http://' + internalLB.dnsName + '/name'
If you run cdk synth
, you can see that the AWS CDK generates a CloudFormation template that dynamically inserts the proper DNS name of the load balancer at deployment time:
Type: 'AWS::ECS::TaskDefinition'
Properties:
ContainerDefinitions:
- Environment:
- Name: GREETING_URL
Value:
'Fn::Join':
- ''
- - 'http://'
- 'Fn::GetAtt':
- internal505AC855
- DNSName
- /greeting
- Name: NAME_URL
Value:
'Fn::Join':
- ''
- - 'http://'
- 'Fn::GetAtt':
- internal505AC855
- DNSName
- /name
One final thing to add to your AWS CDK stack is an output. This gives you the DNS name of your service so you can send traffic to it:
this.externalDNS = new cdk.CfnOutput(this, 'ExternalDNS', {
exportName: 'ExternalDNS',
value: externalLB.loadBalancerDnsName
});
Now see what is added when you deploy. Type the following command to see a preview of new or modified resources without actually doing the deployment:
cdk diff
The list of new resources being added looks good so run cdk deploy
again. Again, the AWS CDK synthesizes the CloudFormation template, and initializes its deployment on your AWS account. This time, however, it creates a total of 66 resources, and gives you a URL output where the application is hosted:
To verify that your application is up and accepting traffic at that URL, load that internet ExternalDNS
URL in your browser. The web application was able to talk to the two other backend services to get a greeting and a name:
Conclusion
If you’d like to try deploying this microservice stack yourself or using it as the basis for building your own AWS CDK stack, you can find the full AWS CDK example code on GitHub. Be sure to check out the AWS CDK documentation and the official AWS CDK construct for Amazon ECS on NPM.