AWS CDK Examples¶
This topic contains some examples to help you get started using some of the advanced constructs offered by the AWS CDK.
- Amazon ECS Example walks you through creating a Fargate service running on an ECS cluster fronted by an internet-facing
application load balancer.
- Apples Example walks you through creating the resources for a simple widget dispensing service.
Creating an Amazon ECS AWS CDK App¶
Amazon Elastic Container Service (Amazon ECS) is a highly scalable, fast, container management service that makes it easy to run, stop, and manage Docker containers on a cluster. You can host your cluster on infrastructure that is managed by Amazon ECS by launching your services or tasks using the Fargate launch type. For more control you can host your tasks on a cluster of Amazon Elastic Compute Cloud (Amazon EC2) instances that you manage by using the EC2 launch type.
This example shows you how to launch some services using the Fargate launch type. If you’ve ever used the console to create a Fargate service, you know that there are many steps you must follow to accomplish that task. AWS has a number of tutorials and documentation topics that walk you through creating a Fargate service, including:
- How to Deploy Docker Containers - AWS
- Setting up with Amazon ECS and Getting Started with Amazon ECS using Fargate
This example creates a similar Fargate service in AWS CDK code.
Since Amazon ECS can be used with a number of AWS services, you should understand how the Amazon ECS construct that we use in this example gives you a leg up on using AWS services by providing the following benefits:
Automatically configures a load balancer.
Automatic security group opening for load balancers, which enables load balancers to communicate with instances without you explictly creating a security group.
Automatic ordering dependency between service and load balancer attaching to a target group, where the AWS CDK enforces the correct order of creating the listener before an instance is created
Automatic userdata configuration on auto-scaling group, which creates the correct configuration to associate a cluster to AMI(s).
Early validation of parameter combinations, which exposes AWS CloudFormation issues earlier, thus saving you deployment time. For example, depending upon the task, it is easy to mis-configure the memory settings. Previously you would not encounter an error until you deployed your app, but now the AWS CDK can detect a misconfiguration and emit an error when you synthesize your app.
Automatically adds permissions for Amazon ECR if you use an image from Amazon ECR When you use an image from Amazon ECR, the AWS CDK adds the correct permissions.
Convenient API for autoscaling The AWS CDK supplies a method so you can autoscale instances when you use an Amazon EC2 cluster; this functionality is done automatically when you use an instance in a Fargate cluster.
In addition, the AWS CDK will prevent instances from being deleted when autoscaling tries to kill an instance, but either a task is running or is scheduled on that instance.
Previously, you had to create a Lambda function to have this functionality.
Asset support, so that you can deploy source from your machine to Amazon ECS in one step Previously, to use application source you had to perform a number of manual steps (upload to Amazon ECR, create Docker image, etc.).
Step 1: Create the Directory and Initialize the AWS CDK¶
Let’s start with creating a new directory to hold our AWS CDK code and create a new app in that directory.
mkdir MyEcsConstruct cd MyEcsConstruct
Step 2: Add the Amazon EC2 and Amazon ECS Packages¶
Install support for Amazon EC2 and Amazon ECS.
Step 3: Create a Fargate Service¶
There are two different ways of running your container tasks with Amazon ECS:
- Using the Fargate launch type, where Amazon ECS manages the physical machines that your containers are running on for you
- Using the EC2 launch type, where you do the managing, such as specifying autoscaling
The following example creates a Fargate service running on an ECS cluster fronted by an internet-facing application load balancer.
That’s how easy it is to create a Fargate service to run a Docker image.