Launch ECS container using AWS CLI - Step 5 - Cleanup
Delete all the resources that were created in this series.
This is the last part of a multi-post thread involving these steps:
- Network Setup
- Launch EC2 Instance
- Create Docker Image
- Create Service
- Cleanup (this post)
Define Names
# service
service="ecs-test-service"
task="ecs-test"
# task definition
task_def_family="ecs-test"
# cluster
cluster="cluster-ecs"
# IAM
instance_profile="instance-profile-ecs"
instance_role="instance-role-ecs"
# EC2
instance="instance-ecs"
# VPC
vpc="vpc-ecs"
# Subnets
subnet_1="subnet-ecs-1"
subnet_2="subnet-ecs-2"
# Internet Gateway
internet_gateway="igw-ecs"
# Route Table
route_table="rtb-ecs"
# Security Group
security_group="security-ecs"
Cleanup
Delete all resources in reverse order of creation:
echo "Delete service $service ..."
aws ecs delete-service --force --cluster $cluster --service $service
echo "Deregister task definition $task ..."
task_def_arn=$(aws ecs list-task-definitions --sort DESC --family-prefix $task_def_family | jq -r '.taskDefinitionArns[0]')
aws ecs deregister-task-definition --task-definition $task_def_arn
echo "Delete EC2 Instance ..."
instance_id=$(aws ec2 describe-instances \
--filters \
Name=tag:Name,Values=$instance \
Name=instance-state-name,Values=running \
| jq -r '.Reservations[0].Instances[0].InstanceId')
aws ec2 terminate-instances --instance-ids $instance_id
echo "Wait until EC2 instance terminates ..."
aws ec2 wait instance-terminated --instance-ids $instance_id
echo "Delete EC2 Instance Profile ..."
aws iam remove-role-from-instance-profile \
--instance-profile-name $instance_profile \
--role-name $instance_role
aws iam delete-instance-profile --instance-profile-name $instance_profile
echo "Delete EC2 Instance Role ..."
aws iam detach-role-policy \
--role-name $instance_role \
--policy-arn arn:aws:iam::aws:policy/service-role/AmazonEC2ContainerServiceforEC2Role
aws iam delete-role --role-name $instance_role
echo "Delete ECS cluster ..."
aws ecs delete-cluster --cluster $cluster
echo "Delete Security Group ..."
group_id=$(aws ec2 describe-security-groups --filters Name=tag:Name,Values=$security_group | jq -r '.SecurityGroups[0].GroupId')
aws ec2 delete-security-group --group-id $group_id
echo "Delete Subnets ..."
subnet_1_id=$(aws ec2 describe-subnets --filters Name=tag:Name,Values=$subnet_1 | jq -r '.Subnets[0].SubnetId')
aws ec2 delete-subnet --subnet-id $subnet_1_id
subnet_2_id=$(aws ec2 describe-subnets --filters Name=tag:Name,Values=$subnet_2 | jq -r '.Subnets[0].SubnetId')
aws ec2 delete-subnet --subnet-id $subnet_2_id
echo "Delete Route Table ..."
route_table_id=$(aws ec2 describe-route-tables --filters Name=tag:Name,Values=$route_table | jq -r '.RouteTables[0].RouteTableId')
aws ec2 delete-route-table --route-table-id $route_table_id
echo "Delete Internet Gateway ..."
internet_gateway_id=$(aws ec2 describe-internet-gateways --filters Name=tag:Name,Values=$internet_gateway | jq -r '.InternetGateways[0].InternetGatewayId')
vpc_id=$(aws ec2 describe-vpcs --filters Name=tag:Name,Values=$vpc | jq -r '.Vpcs[0].VpcId')
aws ec2 detach-internet-gateway --internet-gateway-id $internet_gateway_id --vpc-id $vpc_id
aws ec2 delete-internet-gateway --internet-gateway-id $internet_gateway_id
echo "Delete VPC ..."
aws ec2 delete-vpc --vpc-id $vpc_id
!!! DANGER !!!
Delete latest
image
repo="ecs-test"
registryId=$(aws ecr describe-registry | jq -r '.registryId')
aws ecr batch-delete-image \
--registry-id $registryId \
--repository-name $repo \
--image-ids imageTag=latest
Delete repository
NOTE: This will delete all images in the repo and the repo itself: