Easy to use ECS

pinxiongcn

Pin Xiong

Posted on July 15, 2022

Easy to use ECS

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

There are several steps as below, and all of source codes have been put in my github repo(infrastructure and golang-web).

Prerequisites

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 first

$ cd setup
$ terraform init
Enter fullscreen mode Exit fullscreen mode
  • Create s3 bucket
$ terraform apply
Enter fullscreen mode Exit fullscreen mode

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

Outputs:

s3_bucket_terraform_state = "**********-us-east-1"
Enter fullscreen mode Exit fullscreen mode

Build resources

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

  • Setup remote backup
$ cd ../region/virginia 
Enter fullscreen mode Exit fullscreen mode

and then, update the block of s3 in providers.tf file

backend "s3" {
    bucket  = "**********-us-east-1"
    key     = "terraform/backend.tfstate"
    region  = "us-east-1"
    encrypt = "true"
  }
Enter fullscreen mode Exit fullscreen mode

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

$ terraform init
$ terraform apply
Enter fullscreen mode Exit fullscreen mode

You will be prompted to enter yes after confirmed the listed resources.
Confirm the listed resources

After 10 minutes, all resources are created.
All resources are created

Deploy your code

Download golang-web first and then set your git remote as below

$ git remote add aws ssh://git-codecommit.us-east-1.amazonaws.com/v1/repos/golang-web

$ git remote -v
Enter fullscreen mode Exit fullscreen mode

git remote -v

$ 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
Enter fullscreen mode Exit fullscreen mode

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
  • Subnets Subnets

and then the codepipe has been built

  • CodeCommit
    CodeCommit

  • CodeBuild
    CodeBuild

  • CodePipeline
    CodePipeline
    CodePipeline

  • ECR
    ECR

we also can see that a public endpoint is created.

  • ELB
    ELB

  • Target group
    Target group

Now, It's time to see CES

  • ECS Dashboard
    ECS Dashboard

  • ECS Cluster
    ECS Cluster

  • ECS Service
    ECS Service

  • Task Definition
    Task Definition

Verification

  • Access public endpoint
    Access public endpoint

  • Scale out
    Access start api will exhaust CPU resource serval times
    Exhaust CPU resource

and then see the monitor in Cloud watch
Cloud watch

the ecs has been created 2 new tasks
Created 2 new tasks

  • Scale in Access stop api will release CPU resource serval times Release CPU resource

and then see the monitor in Cloud watch
Cloud watch

the ecs has been draining tasks
Drain tasks

💖 💪 🙅 🚩
pinxiongcn
Pin Xiong

Posted on July 15, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related