Created
March 31, 2017 15:26
-
-
Save keymon/469032686fd7c4a606f7d5716c9d982a to your computer and use it in GitHub Desktop.
Delete cloud formation and services running in ECS based on a component name
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Deletes a cloudformation stack | |
| clean_up_cloudformation() { | |
| component_name="$1" | |
| aws cloudformation delete-stack \ | |
| --region eu-west-1 \ | |
| --stack-name "${component_name}" | |
| } | |
| # Checks if the given cloudformation stack is deleted | |
| check_cloudformation(){ | |
| component_name="$1" | |
| watch -be aws cloudformation list-stack-resources \ | |
| --region eu-west-1 \ | |
| --stack-name "${component_name}" \ | |
| --query 'StackResourceSummaries[].{s:ResourceStatus,a:ResourceType,b:PhysicalResourceId}' \ | |
| --output text | |
| } | |
| # Deletes a service from ECS | |
| clean_up_service() { | |
| component_name="$1" | |
| cluster="$2" | |
| aws ecs update-service \ | |
| --region eu-west-1 \ | |
| --cluster "${cluster}" \ | |
| --desired-count 0 \ | |
| --service "${component_name}" | |
| aws ecs delete-service \ | |
| --region eu-west-1 \ | |
| --cluster "${cluster}" \ | |
| --service "${component_name}" | |
| } | |
| # List and then deregister the task definitions | |
| task_definitions_for_family(){ | |
| family=$1 | |
| aws ecs list-task-definitions \ | |
| --region eu-west-1 \ | |
| --status ACTIVE \ | |
| --family-prefix=${family} | \ | |
| jq -r '.taskDefinitionArns[]' | |
| } | |
| deregister_task_definitions_for_family(){ | |
| for task_def in $(task_definitions_for_family $1); do | |
| aws ecs deregister-task-definition \ | |
| --region eu-west-1 \ | |
| --task-definition "${task_def}" | \ | |
| jq -r '"\( .taskDefinition.taskDefinitionArn ) \( .taskDefinition.status )"' | |
| done | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment