# AWS ECS

Elastic Container Service

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662210068823/2zqhgqaM1.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/v1662210070224/8Eac_Lyl5.png)

### 2\. Concepts

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662210071724/8nfzDO6Rc.png)

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

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

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662210077542/bUI5p4vbP.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/v1662210079186/c54Cx9oUs.png)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662210080587/OJo-RnJvm.png)

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

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

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

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

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

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662210090911/sI--tAj8C.png)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662210092360/5QHrWEo82.png)

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

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

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662210098231/WubZ-ofbY.png)

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

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662210102435/cbTVI51zB.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/v1662210103868/cS1EHQNTr.png)

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

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662210107774/491Ed7NhT.png)

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

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

### EC2 / Target Group

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

EC2 Target Group

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662210111912/fHHh-El5Z.png)

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

public ip — target group

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

Target Group Created

### EC2 / Load Balancer

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662210115859/vJKaH8w-O.png)

EC2 Load Balancer

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

create EC2 Load Balancer

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

classic loader balancer

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

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

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

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

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662210126494/2NxmPaWaf.png)

Load Balancer Created

### 5\. ECS Service

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662210129405/2MsQ36RUW.png)

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

Service Created

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662210133185/NJEhb-13f.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/v1662210134402/ilnoJBGhdB.png)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662210135369/2KRt8ufRC.png)

Published in

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662210136341/nnkUaMV7V.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/v1662210137510/L7MkJNU9D.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/v1662210138901/vfMfQoZtE.png)

#### …Paste to AWS ECS

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662210140375/apudhzuXb.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/v1662210141675/jSnSdlf6p.png)

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

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

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

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

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

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

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1662210152485/65fqICOWJy.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>
