# AWS ECS

Elastic Container Service

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662834050089/ici82TVnN.png)

AWS ECS

1.  Elements
2.  Concepts
3.  HandsOn
4.  Service / Load Balancer / Target Group
5.  ECS Service
6.  Backup / Restore Volumes
7.  AWS ECS with Docker Hub
8.  AWS ECS with Image from AWS ECR
9.  Dockerfile docker
10.  MySQL on AWS ECS
11.  References

### 1\. Elements

*   Cluster
*   Task Definition
*   Task
*   Service

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662834051458/C9pVDHlRe.png)

### 2\. Concepts

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662834052955/iXuNU2WOJ.png)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662834054286/O9uWs28Mn.png)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662834055862/hsNr_bKsp.png)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662834057381/Ohn_t5hVW.png)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662834059343/4Gy-N0bo1.png)

<iframe src="https://www.slideshare.net/slideshow/embed_code/key/5IGztT6emQbtPx" width="600" height="500" frameborder="0" scrolling="no"></iframe>

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662834060829/hywJVNRJP.png)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662834062363/TI7fm7COA.png)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662834063753/1ztj1IHGQ.png)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662834065183/DPZ0GfVmi.png)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662834066605/EH70QTHIW.png)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662834068137/LuMxCBhvA.png)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662834069615/ZQ8eHzrHn.png)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662834071058/v9vTb-ICH.png)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662834072491/zZoeXZe0G.png)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662834073933/l8tjw8k-V.png)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662834075386/v51njt7s6.png)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662834076889/6ZV6jJB6O.png)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662834078358/Mpyc-qoNs.png)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662834079903/4wmofh1Sc.png)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662834081271/GCK7lejB4.png)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662834082638/zMefMMLAv.png)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662834084197/J2-toVhjm.png)

<iframe src="https://www.slideshare.net/slideshow/embed_code/key/DnE1w3mErvQlmz" width="600" height="500" frameborder="0" scrolling="no"></iframe>

### 3\. Hands On

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662834085594/kmWZrwhy1.png)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662834086936/cd2gnV062.png)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662834088291/-fUJEWrau.png)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662834089593/OlLvzchjB.png)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662834090865/3ZmJ7cWqS.png)

### 4\. Service / Load Balancer / Target Group

### EC2 / Target Group

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662834092289/zVSx9veVa.png)

EC2 Target Group

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662834093598/GYoNDLunf.png)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662834094923/LcOQTaGDo.png)

public ip — target group

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662834096258/vSyjupiKg.png)

Target Group Created

### EC2 / Load Balancer

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662834097508/WporrCM_M.png)

EC2 Load Balancer

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662834098745/iH1wcQknh.png)

create EC2 Load Balancer

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662834100363/UO_kvZZ65.png)

classic loader balancer

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662834101909/9di21uii3.png)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662834103251/q36y5PhMR.png)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662834104603/oqbNw5pnb.png)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662834105794/CxzEwZCSe.png)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662834107064/kJYtu2U-5.png)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662834108354/mp5D9woEQ.png)

Load Balancer Created

### 5\. ECS Service

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662834109645/ISf_JSUB4.png)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662834110881/76pJj_MO8.png)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662834112170/VfyuVtkxE.png)

Service Created

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662834113429/TFNnZ2S7B4.png)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662834114914/0cjmsbsaa.png)

### 6\. Backup / Restore Volumes Container

[**How can I backup a Docker-container with its data-volumes?**  
*if I want to revert the container I can try to commit an image, and then later delete the container, and create a new…*stackoverflow.com](https://stackoverflow.com/questions/26331651/how-can-i-backup-a-docker-container-with-its-data-volumes#:~:text=To%20backup%20a%20data%20volume,data%20for%20a%20MySQL%20server. "https://stackoverflow.com/questions/26331651/how-can-i-backup-a-docker-container-with-its-data-volumes#:~:text=To%20backup%20a%20data%20volume,data%20for%20a%20MySQL%20server.")[](https://stackoverflow.com/questions/26331651/how-can-i-backup-a-docker-container-with-its-data-volumes#:~:text=To%20backup%20a%20data%20volume,data%20for%20a%20MySQL%20server.)

### Backup Script

a

*#!/bin/bash*

*#*

*\# https://stackoverflow.com/questions/26331651/how-can-i-backup-a-docker-container-with-its-data-volumes#:~:text=To%20backup%20a%20data%20volume,data%20for%20a%20MySQL%20server.*

*#*

*\# $ volume\_backup.sh old\_container /srv/www*

*\# $ sudo docker stop old\_container && sudo docker rm old\_container*

*\# $ sudo docker run -d --name new\_container myrepo/new\_container*

*\# $ volume\_restore.sh new\_container*

*#*

*\# This script allows you to backup a single volume from a container*

*\# Data in given volume is saved in the current directory in a tar archive.*

*#*

*\#              Backup Command*

*\#   ./backup.sh mysql.keycloak.foundation /var/lib/mysql*

*#*

CONTAINER\_NAME=$1

VOLUME\_NAME=$2

*#CONTAINER\_NAME="mysql.keycloak.foundation"*

*#VOLUME\_NAME="keycloak\_mysql\_data"*

usage() {

echo "Usage: $0 \[container name\] \[volume name\]"

exit 1

}

echo "### step 01"

*if* \[ -z $CONTAINER\_NAME \]

*then*

echo "Error: missing container name parameter."

usage

*fi*

echo "### step 02"

*if* \[ -z $VOLUME\_NAME \]

*then*

echo "Error: missing volume name parameter."

usage

*fi*

echo "### step 03"

sudo docker run --rm --volumes-from $CONTAINER\_NAME -v $(pwd):/backup busybox tar cvf /backup/backup.tar $VOLUME\_NAME

### Restore Script

*#!/bin/bash*

*#*

*\# https://stackoverflow.com/questions/26331651/how-can-i-backup-a-docker-container-with-its-data-volumes#:~:text=To%20backup%20a%20data%20volume,data%20for%20a%20MySQL%20server.*

*#*

*#Usage can be like this:*

*#*

*#$ volume\_backup.sh old\_container /srv/www*

*#$ sudo docker stop old\_container && sudo docker rm old\_container*

*#$ sudo docker run -d --name new\_container myrepo/new\_container*

*#$ volume\_restore.sh new\_container*

*#*

*#*

*\## This script allows you to restore a single volume from a container*

*\# Data in restored in volume with same backupped path*

*#*

*\#          Restore Command*

*\# ./restore.sh new\_container*

*#*

*#*

NEW\_CONTAINER\_NAME=$1

usage() {

echo "Usage: $0 \[container name\]"

exit 1

}

*if* \[ -z $NEW\_CONTAINER\_NAME \]

*then*

echo "Error: missing container name parameter."

usage

*fi*

sudo docker run --rm --volumes-from $NEW\_CONTAINER\_NAME -v $(pwd):/backup busybox tar xvf /backup/backup.tar

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662834116016/Ugr-C6INw.png)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662834117013/RCrgiPl_T.png)

Published in

[My Dev Zone](https://mydevelopmentzone.ml/?source=post_page-----f42318bfde1e--------------------------------)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662834118000/91rswfXCC.png)

[Andre Vianna](https://andrevianna-br.medium.com/?source=post_page-----f42318bfde1e--------------------------------)

Dec 27, 2021

·

6 min read

·

Listen

### 6\. Upload Image to Docker Hub

*   docker ps -a
*   docker commit Imagem repo
*   docker push repo
*   docker login -username=username -email=email@docker.com
*   [https://hub.docker.com](https://hub.docker.com/)

  
docker login -u=xxxx -p=yyyydocker login -u="${DOCKER\_USERNAME}" -p="${DOCKER\_PASSWORD}"

### Upload Image Container

*   docker ps -a
*   docker commit Imagem repo
*   docker push repo
*   docker login -username=username -email=email@docker.com
*   [https://hub.docker.com](https://hub.docker.com/)

docker login -u=xxxx -p=yyyydocker login -u="${DOCKER\_USERNAME}" -p="${DOCKER\_PASSWORD}"

### 7\. AWS WCS with Docker Hub

<iframe src="https://www.youtube.com/embed/W3aIzU-7zZc?feature=oembed" width="700" height="393" frameborder="0" scrolling="no"></iframe>

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662834119183/x6uaWjvpK.jpeg)

AWS ECS with Docker Hub

[**Authenticating with Docker Hub for AWS Container Services | Amazon Web Services**  
*Docker Hub has recently updated its terms of service to introduce rate limits for container image pulls. While these…*aws.amazon.com](https://aws.amazon.com/pt/blogs/containers/authenticating-with-docker-hub-for-aws-container-services/ "https://aws.amazon.com/pt/blogs/containers/authenticating-with-docker-hub-for-aws-container-services/")[](https://aws.amazon.com/pt/blogs/containers/authenticating-with-docker-hub-for-aws-container-services/)

### 8.AWS ECS with Image from AWS ECR

#### …Copy from AWS ECR

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662834120622/bOa6BFu_x.png)

#### …Paste to AWS ECS

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662834122128/7c5K3zMyl.png)

### 9\. Dockerfile docker build

Dockerfile

FROM ubuntu

RUN apt-get update

RUN apt-get install --yes lynx

docker build

docker build -t <repo> .

### 10\. MySQL on AWS ECS

[**How to deploy MySQL docker image on AWS ECS?**  
*So, I found out a mistake. THE VERY FIRST THING YOU DO - is you test that docker container on localhost and see if you…*stackoverflow.com](https://stackoverflow.com/questions/57795724/how-to-deploy-mysql-docker-image-on-aws-ecs "https://stackoverflow.com/questions/57795724/how-to-deploy-mysql-docker-image-on-aws-ecs")[](https://stackoverflow.com/questions/57795724/how-to-deploy-mysql-docker-image-on-aws-ecs)

```
TaskDefinition(  
            'WordpressDatabaseTaskDefinition',  
            RequiresCompatibilities=['FARGATE'],  
            Cpu='512',  
            Memory='2048',  
            NetworkMode='awsvpc',     
            ContainerDefinitions=[  
                ContainerDefinition(  
                    Name='WordpressDatabaseContainer',  
                    Image='mysql:5.7',  
                    Environment=[  
                        Environment(Name='MYSQL_ROOT_PASSWORD', Value='root'),  
                        Environment(Name='MYSQL_DATABASE', Value='wpdb'),  
                        Environment(Name='MYSQL_USER', Value='root'),  
                        Environment(Name='MYSQL_PASSWORD', Value='root'),  
                    ],  
                    PortMappings=[  
                        PortMapping(  
                            ContainerPort=3306  
                        )  
                    ]  
                )  
            ]  
        )
```

```
self.wordpress_database_task = TaskDefinition(  
            'WordpressDatabaseTaskDefinition',  
            RequiresCompatibilities=['FARGATE'],  
            Cpu='512',  
            Memory='2048',  
            NetworkMode='awsvpc',  
  
            # If your tasks are using the Fargate launch type, the host and sourcePath parameters are not supported.  
            Volumes=[  
                Volume(  
                    Name='MySqlVolume',  
                    DockerVolumeConfiguration=DockerVolumeConfiguration(  
                        Scope='shared',  
                        Autoprovision=True  
                    )  
                )  
            ],  
  
            ContainerDefinitions=[  
                ContainerDefinition(  
                    Name='WordpressDatabaseContainer',  
                    Image='mysql:5.7',  
                    Environment=[  
                        Environment(Name='MYSQL_ROOT_PASSWORD', Value='root'),  
                        Environment(Name='MYSQL_DATABASE', Value='wpdb'),  
                        Environment(Name='MYSQL_USER', Value='wordpressuser'),  
                        Environment(Name='MYSQL_PASSWORD', Value='wordpressuserpassword'),  
                    ],  
                    PortMappings=[  
                        PortMapping(  
                            ContainerPort=3306  
                        )  
                    ]  
                )  
            ]  
        )  
  
        self.wordpress_database_service = Service(  
            'WordpressDatabaseService',  
            Cluster=Ref(self.ecs_cluster),  
            DesiredCount=1,  
            TaskDefinition=Ref(self.wordpress_database_task),  
            LaunchType='FARGATE',  
            NetworkConfiguration=NetworkConfiguration(  
                AwsvpcConfiguration=AwsvpcConfiguration(  
                    Subnets=[Ref(sub) for sub in VpcFormation().public_subnets],  
                    AssignPublicIp='ENABLED',  
                    SecurityGroups=[Ref(self.security_group)]  
                )  
            ),  
        )
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662834123489/N8xjG1wbo.png)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662834124842/hGecirgvg.png)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662834126161/SrjDXWYkU.png)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662834127538/w9YsLChPQ.png)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662834128866/ncg7i0i2K.png)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662834130280/HwgekuksG.png)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662834138873/wh-ryzzBm.png)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662834140504/1lArufikR.png)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662834141991/GjHukMmmf.png)

### 11\. References

[**O que é o Amazon Elastic Container Service?**  
*O Amazon Elastic Container Service (Amazon ECS) é um serviço de gerenciamento de contêineres altamente rápido e…*docs.aws.amazon.com](https://docs.aws.amazon.com/pt_br/AmazonECS/latest/developerguide/Welcome.html "https://docs.aws.amazon.com/pt_br/AmazonECS/latest/developerguide/Welcome.html")[](https://docs.aws.amazon.com/pt_br/AmazonECS/latest/developerguide/Welcome.html)

<iframe src="https://www.youtube.com/embed/DlpiLxKRXYo?start=878&amp;feature=oembed&amp;start=878" width="700" height="393" frameborder="0" scrolling="no"></iframe>

<iframe src="https://www.youtube.com/embed/zJMqCjc2qIc?feature=oembed" width="700" height="393" frameborder="0" scrolling="no"></iframe>

<iframe src="https://www.youtube.com/embed/z7_LdCVnCRU?feature=oembed" width="700" height="393" frameborder="0" scrolling="no"></iframe>

<iframe src="https://www.youtube.com/embed/z7_LdCVnCRU?feature=oembed" width="700" height="393" frameborder="0" scrolling="no"></iframe>
