AWS – Elastic Container Service(ECS)

ECS with AWS CLI

Get list of clusters within ECS

aws ecs list-clusters --profile {profile-name}

// output
{
    "clusterArns": [
        "arn:aws:ecs:us-east-1:123456789:cluster/server-api",
        "arn:aws:ecs:us-east-1:123456789:cluster/server-web"
    ]
}

 

Get list of services within a cluster

aws ecs list-services --cluster server-api --profile {profile-name}

//output
{
    "serviceArns": [
        "arn:aws:ecs:us-east-1:12345679:service/server-api-service"
    ]
}

Build docker and push image to ECR

# authenticate to ECR
aws ecr get-login-password --region us-east-1 --profile folau | docker login --username AWS --password-stdin {account-num}.dkr.ecr.us-east-1.amazonaws.com

# build image
docker build -t backend-api .

# tag
docker tag backend-api:latest {account-num}.dkr.ecr.us-east-1.amazonaws.com/backend-api:latest

# push imge to ECR
docker push {account-num}.dkr.ecr.us-east-1.amazonaws.com/backend-api:latest

Build docker, push image to ECR, and deploy image to ECS Fargate

#!/bin/sh

# us-east-1
aws ecr get-login-password --region us-east-1 --profile {aws-profile} | docker login --username AWS --password-stdin {aws-account-number}.dkr.ecr.{aws-region}.amazonaws.com

docker build -t {project-name} .

docker tag {project-name}:latest {aws-account-number}.dkr.ecr.{aws-region}.amazonaws.com/{ecr-repository}:latest

docker push {aws-account-number}.dkr.ecr.{aws-region}.amazonaws.com/{ecr-repository}:latest

# task-definition with no version will use the lastest version
aws ecs update-service \
--cluster {cluster-name} \
--service {service-name} \
--task-definition {task-def-name} \
--force-new-deployment \
--profile {profileName}

# Example
#!/bin/sh

aws ecr get-login-password --region us-east-1 --profile folau | docker login --username AWS --password-stdin 1231231231.dkr.ecr.us-east-1.amazonaws.com

docker build -t backend-api .

docker tag backend-api:latest 1231231231.dkr.ecr.us-east-1.amazonaws.com/backend-api:latest

docker push 1231231231.dkr.ecr.us-east-1.amazonaws.com/backend-api:latest

aws ecs update-service \
--cluster backend-api \
--service backend-api \
--task-definition backend-api \
--force-new-deployment \
--profile folau

Update desired count

aws ecs update-service --cluster backend-api --service backend-api --task-definition backend-api \ 
--desired-count 0 \ 
--profile folau

 




Subscribe To Our Newsletter
You will receive our latest post and tutorial.
Thank you for subscribing!

required
required


Leave a Reply

Your email address will not be published. Required fields are marked *