0

I am using AWS Code Pipeline, Code Build to create a new Docker container and push it to ECR.

My application is a simple straight forward single Container based. What would be less friction approach to pull down current running Container and re-launch a new Container from the ECS registry (output of Code Build thru Code Pipeline).

I tried CloudFormation with EC2 user data, custom scripts on one side and CloudFormation with ECS with task definition on the other side (not successful yet). I strongly feel there must be more obvious and simpler approach.

Naveen Vijay
  • 15,928
  • 7
  • 71
  • 92

1 Answers1

1

did you check aws ecs command-line tool? It allows to write a simple bash script to update a task and a service:

cluster='<cluster-id>'
service='<service-id>'
project='<project-name>'
elb='<ELB-id>'

version=$(git rev-parse --short=12 HEAD)

sed "s#{image}#${repo}:app-${version}#g" ./docker-compose.yml | sed "s#{frontend}#${repo}:frontend-${version}#g" | sed "s#{logger}#${repo}:logger-${version}#g"> /tmp/docker-compose.yml
ecs-cli compose --file /tmp/docker-compose.yml --project-name ${project} create

taskDefinition=$(aws ecs list-task-definitions --family-prefix "ecscompose-${project}" --output text --sort DESC | awk '{print $2}' | head -n 1)

if [[ $(aws ecs list-services --cluster "${cluster}" --output text) =~ ${service} ]]; then
    aws ecs update-service --cluster "${cluster}"  --service ${service} --task-definition "$taskDefinition" >> /dev/null
else
    aws ecs create-service --cluster "${cluster}" --service ${service} --task-definition "$taskDefinition" --load-balancers loadBalancerName=${elb},containerName=nginx,containerPort=80 --desired-count 5 --role arn:aws:iam::395910257915:role/ecsServiceRole
fi
max
  • 2,757
  • 22
  • 19