Skip to content

Instantly share code, notes, and snippets.

@imnnquy
Created June 2, 2020 04:38
Show Gist options
  • Save imnnquy/adc84bfe06e53c8b58e12df16e6f4c03 to your computer and use it in GitHub Desktop.
Save imnnquy/adc84bfe06e53c8b58e12df16e6f4c03 to your computer and use it in GitHub Desktop.
#!/bin/bash
REVISION=$(aws ecs list-task-definitions --family-prefix $AWS_ECS_CONTAINER_NAME | jq '.taskDefinitionArns[]' | tr -d '"' | tail -1 | rev | cut -d':' -f 1 | rev)
if [ ! -z "$REVISION" ]; then
aws ecs deregister-task-definition \
--region $AWS_DEFAULT_REGION \
--task-definition $AWS_ECS_CONTAINER_NAME:$REVISION \
>> /dev/null
# Prevent ecs to restart the task to update new code
aws ecs update-service \
--cluster $AWS_ECS_CLUSTER_NAME \
--service $AWS_ECS_CONTAINER_NAME \
--desired-count 0\
--placement-constraints "[]" \
--placement-strategy "[]" >> /dev/null
# Stop current task that is running our application.
# This is what will stop the application.
ecs-cli compose \
--file $COMPOSE_FILE \
--project-name "$AWS_ECS_CONTAINER_NAME" \
service stop
fi
# Start new task which will create fresh new task definition as well.
# This is what brings the application up with the new changes and configurations.
ecs-cli compose \
--file $COMPOSE_FILE \
--project-name "$AWS_ECS_CONTAINER_NAME" \
service up
INSTANCES_COUNT=$(aws ecs list-container-instances --cluster $AWS_ECS_CLUSTER_NAME | jq '.containerInstanceArns | length')
# Configure tasks to spread all instances
aws ecs update-service \
--cluster $AWS_ECS_CLUSTER_NAME \
--service $AWS_ECS_CONTAINER_NAME \
--desired-count $INSTANCES_COUNT \
--placement-constraints type="distinctInstance" \
--placement-strategy type="spread",field="instanceId" >> /dev/null
# Scale to run tasks in all instances
ecs-cli compose \
--file $COMPOSE_FILE \
--project-name "$AWS_ECS_CONTAINER_NAME" \
service scale $INSTANCES_COUNT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment