Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
July 15, 2022 02:00 pm GMT

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
  • Create s3 bucket
$ 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

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
$ cd ../region/virginia 

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"  }

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

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

git remote -v

$ git push aws master:masterEnumerating objects: 62, done.Counting objects: 100% (62/62), done.Delta compression using up to 12 threadsCompressing 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 0To 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.

  • VPCVPC
  • SubnetsSubnets

and then the codepipe has been built

  • 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 inAccess stop api will release CPU resource serval timesRelease CPU resource

and then see the monitor in Cloud watch
Cloud watch

the ecs has been draining tasks
Drain tasks


Original Link: https://dev.to/aws-builders/easy-to-use-ecs-2700

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To