Easy to use ECS
Tags: ecs, terraform
Published: 2022-07-15 14:00:09

Elastic Container Service(ECS) is a highly scalable and fast container management service that makes it easy to run, stop, and manage containers on a cluster.

Today, I'll show you how to use it!

### Architecture
![Architecture](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f136jzpwhbjhwy83x5z4.png)
 
 

There are several steps as below, and all of source codes have been put in my github repo([infrastructure](https://github.com/pinxiong/infrastructure/tree/ecs) and [golang-web](https://github.com/pinxiong/golang-web)).

### Prerequisites

+ Create an [aws account](https://aws.amazon.com/) with the IAM `AdministratorAccess` permission
+ Install and configure [aws CLI](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-welcome.html)
+ Install and configure [Terraform](https://www.terraform.io/downloads)

### Setup environment

Terraform saves all changes in `*.tfstate` file, so we'd better store `*.tfstate` in `aws s3 bucket` instead of local machine. This step will build a `aws s3 bucket` to store `*.tfstate` file.

+ Init Terraform

Download [infrastructure](https://github.com/pinxiong/infrastructure/tree/ecs) first

```shell
$ cd setup
$ terraform init
```

+ Create s3 bucket
```shell
$ terraform apply
```

You will be prompted to enter an `aws region code`, such as `us-east-1`. After that, you need to make sure the listed resources that will be crated and then enter `yes`

You can see the output `s3_bucket_terraform_state` below
```shell
Outputs:

s3_bucket_terraform_state = "**********-us-east-1"
```

### Build resources

Now, we begin to build the resources including VPC, subnets, ECS and Pipeline.

+ Setup remote backup
```shell
$ cd ../region/virginia 
```
and then, update the block of `s3` in `providers.tf` file
```terraform
backend "s3" {
    bucket  = "**********-us-east-1"
    key     = "terraform/backend.tfstate"
    region  = "us-east-1"
    encrypt = "true"
  }
```

The following things maybe need to be modified:
1. Set `bucket` as the output of `s3_bucket_terraform_state`
2. Set `key` as the path to store `*.tfstate` file in s3 bucket
3. Update `region` as the region code that you entered when creating s3 bucket above
4. Set `encrypt` as `true`

+ Create resources

You can modify the configuration in the `main.tf` file according to your needs, and then run the following commands
```shell
$ terraform init
$ terraform apply
```

You will be prompted to enter `yes` after confirmed the listed resources.
![Confirm the listed resources](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/afxy234gbhf8zh8ei14s.png)
 
After 10 minutes, all resources are created.
![All resources are created](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1i27tz01mtqjxnspsbqy.png)
 
### Deploy your code

Download [golang-web](https://github.com/pinxiong/golang-web) first and then set your git remote as below
```shell
$ git remote add aws ssh://git-codecommit.us-east-1.amazonaws.com/v1/repos/golang-web

$ git remote -v
```
![git remote -v](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hvx1oca56tkhz4dhdi0u.png)
 
```shell
$ git push aws master:master
Enumerating objects: 62, done.
Counting objects: 100% (62/62), done.
Delta compression using up to 12 threads
Compressing objects: 100% (55/55), done.
Writing objects: 100% (62/62), 25.36 KiB | 6.34 MiB/s, done.
Total 62 (delta 26), reused 3 (delta 0), pack-reused 0
To ssh://git-codecommit.us-east-1.amazonaws.com/v1/repos/golang-web
 * [new branch]      master -> master
```

Now, everything is done, let's take a look at the outputs in aws.

### Outputs

First, we can see VPC and subnet have been created.

+ VPC
![VPC](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tf26varnh1rbx4krj7sw.png)
+ Subnets
![Subnets](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bh4ks37jvy7jt3enttve.png)

and then the codepipe has been built
+ CodeCommit
![CodeCommit](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/viaqbpc200pmwsh5h8fh.png)
 
+ CodeBuild
![CodeBuild](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/otbu4zki7zb8odkjoo81.png)
 
+ CodePipeline
![CodePipeline](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bn4uca8kz4olcgxgitv4.png)
 ![CodePipeline](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l8cpjnakuv6rcq0sfy0z.png)

+ ECR
![ECR](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wmwvylvdoei4r7hoz9lw.png) 

we also can see that a public endpoint is created.

+ ELB
![ELB](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1f6qa13xz8yho641a1bq.png)

+ Target group
![Target group](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v77vfyq7rlhyqd52aoti.png)
  
Now, It's time to see CES

+ ECS Dashboard
![ECS Dashboard](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7au7snbtq40iyz5j4j4x.png)

+ ECS Cluster
![ECS Cluster](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vekjc2l6qv2199ap8ssl.png)

+ ECS Service
![ECS Service](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8wi1vggo0icman9mz5e2.png)

+ Task Definition
![Task Definition](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/22835bpabtcbant582tm.png)
 
### Verification

+ Access public endpoint
![Access public endpoint](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w9tpt30t6j9zo40jndev.png)

+ Scale out
Access `start` api will exhaust CPU resource serval times
![Exhaust CPU resource](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gnw6mu6bgc9x56o1c25s.png)
  
and then see the monitor in Cloud watch
![Cloud watch](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/39ss7aetqljsf79d6260.png)

the ecs has been created 2 new tasks
![Created 2 new tasks](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0d9x4osgzo0gm38gr4cy.png)
  
+ Scale in
Access `stop` api will release CPU resource serval times
![Release CPU resource](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pj0u3rdbihx1uk2hnnd1.png)
 
and then see the monitor in Cloud watch
![Cloud watch](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lot0uec1qd7ge9loktp1.png)
 
the ecs has been draining tasks
![Drain tasks](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wajc9fv1873jo3d1fkvw.png)