In the previous article of the terraform CDK series, we have registered an EC2 instance to the ECS cluster.
In this article, we are going to add task-definition, service and task for simple Nginx server. i.e. we are going to create one container and run Nginx docker image in the container in our already created ECS cluster.
We need the cluster arn (Amazon Resource Name) of the cluster we created in the previous article.
To run any application in the ECS container, firstly, we need to create a task-definition. Task-definition is nothing but a provided configuration of your task (like the requirement and the purpose of the task). It includes information like cpu and memory required by the task, what all containers you want to run in the cluster, and what all docker images are required to run those containers.
Register a task-definition:
Let's start with creating one task definition for nginx
container. For this, we are using EcsTaskDefinition
resource from the AWS provider.
Import the resource.
Add a task definition.
Here, we are creating a task-definition for our Nginx image to run in a container. And specified few attributes.
family
- A unique name for your task definition. It is a required field.memory
- Amount of memory (in MiB) used by the task. The field is optional. If therequires_compatibilities
isFARGATE
this field is required.cpu
- Number of cpu units used by the task. The field is optional. If therequires_compatibilities
isFARGATE
this field is required.networkMode
- Docker networking mode to use for the containers in the task. Valid values arenone
,bridge
,awsvpc
, andhost
.requiresCompatibilities
- Set of launch types required by the task. This field is optional. The valid values areEC2
andFARGATE
.executionRoleArn
- ARN of the task execution role that the Amazon ECS container agent and the Docker daemon can assume.taskRoleArn
- ARN of IAM role that allows your Amazon ECS container task to make calls to other AWS services.containerDefinitions
- A list of valid container definitions provided as a single valid JSON document.
Let's check, what fields the containerDefinition
attribute support. The container definition JSON can be used directly from the file.
name
- name of the containerimage
- image of the container. This can be docker hub image or the path to image from ECRcpu
- Number of cpu units allocated to the container.memory
- amount of memory required (in MiB) by the container.essential
- boolean parameter and is optional. If theessential
parameter of a container is marked astrue
, and that container fails or stops for any reason, all other containers that are part of the task are stopped. If theessential
parameter of a container is marked asfalse
, then its failure doesn't affect the rest of the containers in a task. If this parameter is omitted, a container is assumed to be essential.portMappings
- Port mappings allow containers to access ports on the host container instance to send or receive traffic. The mapping of host port to container port. Fornginx
, container it is80:80
with port 80 exposed on the host.
There are some advanced container definition parameters available for health check, environment, network setting, etc. You can find them here.
Create a service:
ECS service helps you to run and maintain a specified number of instances of a task definition simultaneously in an Amazon ECS cluster.
If any of your tasks should fail or stop for any reason, the Amazon ECS scheduler launches another instance of your task definition to replace it in order to maintain the desired number of tasks in the service. For more information on services, see Amazon ECS services.
Import service resource.
Create service.
To create a service, we need cluster and task-definition arn that we have created earlier. Don't forget to replace them in the code snippet when you copy the snippet.
name
- Name of the service.cluster
- The short name or full Amazon Resource Name (ARN) of the cluster on which to run your service. If not specifieddefault
cluster is assumed.taskDefinition
- Thefamily
andrevision
(family:revision
) or full Amazon Resource Name (ARN) of the task definition to run in your service. If arevision
isn't specified, the latestACTIVE
revision of the specified family is used.launchType
- The launch type on which to run your service. If not specified,EC2
is used by default. Check this for more information onlaunchType
. We are using "EC2" launch type.desiredCount
- The number of instantiations of the specified task definition to place and keep running on your cluster. This parameter is required if theREPLICA
scheduling strategy is used. If the service uses theDAEMON
scheduling strategy, this parameter is optional.orderedPlacementStrategy
- The placement strategy objects to use for tasks in your service. You can specify a maximum of four strategy rules per service. It has three valid values -random
|spread
|binpack
For more information about the service definition parameters, check this out.
Finally, deploy the changes using: cdktf deploy
This will create task-definition, a service and a task running in container.
So far, we have created cluster, an EC2 instance (ECS optimised), task-definition and the service for our task. In the next article, we'll attach an application load balancer to our task. So that, it'll be available publicly to consume.