AWS ECS

Andre Vianna
My Dev Zone
Published in
7 min readJul 10, 2022

Elastic Container Service

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

2. Concepts

3. Hands On

4. Service / Load Balancer / Target Group

EC2 / Target Group

EC2 Target Group
public ip — target group
Target Group Created

EC2 / Load Balancer

EC2 Load Balancer
create EC2 Load Balancer
classic loader balancer
Load Balancer Created

5. ECS Service

Service Created

6. Backup / Restore Volumes Container

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=$1VOLUME_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 ]thenecho "Error: missing container name parameter."usagefiecho "### step 02"if [ -z $VOLUME_NAME ]thenecho "Error: missing volume name parameter."usagefiecho "### 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=$1usage() {echo "Usage: $0 [container name]"exit 1}if [ -z $NEW_CONTAINER_NAME ]thenecho "Error: missing container name parameter."usagefisudo docker run --rm --volumes-from $NEW_CONTAINER_NAME -v $(pwd):/backup busybox tar xvf /backup/backup.tar

Published in

My Dev Zone

Andre Vianna

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

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
docker login -u=xxxx -p=yyyydocker login -u="${DOCKER_USERNAME}" -p="${DOCKER_PASSWORD}"

8.AWS ECS with Image from AWS ECR

…Copy from AWS ECR

…Paste to AWS ECS

9. Dockerfile docker build

Dockerfile

FROM ubuntuRUN apt-get updateRUN apt-get install --yes lynx

docker build

docker build -t <repo> .

10. MySQL 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)]
)
),
)

--

--

Andre Vianna
My Dev Zone

Software Engineer & Data Scientist #ESG #Vision2030 #Blockchain #DataScience #iot #bigdata #analytics #machinelearning #deeplearning #dataviz