Added checking previus command state

This commit is contained in:
JustFiesta 2024-05-22 15:35:45 +02:00
parent e10acfa3a5
commit 2032c3a085

View file

@ -6,8 +6,9 @@
# Reqiured: configured AWS CLI
read -p "Enter region remove resources from: " REGION
read -p "Enter Project tag key to remove resources from: " TAG_VALUE
read -p "Enter Project tag value to remove resources from: " TAG_KEY
read -p "Enter tag key to remove resources from: " TAG_KEY
read -p "Enter tag value to remove resources from: " TAG_VALUE
read -p "Enter ECR name to remove it from aws: " ECR_NAME
echo "---------------------------------------"
@ -16,6 +17,11 @@ echo ""
echo "Deleting EC2 Instances..."
for instance_id in $(aws ec2 describe-instances --region "$REGION" --query "Reservations[].Instances[?Tags[?Key=='$TAG_KEY'&&Value=='$TAG_VALUE']].InstanceId" --output text); do
aws ec2 terminate-instances --region "$REGION" --instance-ids "$instance_id"
if [ $? -eq 0 ]; then
echo "Instance $instance_id terminated successfully."
else
echo "Error terminating instance $instance_id."
fi
done
echo ""
echo "---------------------------------------"
@ -24,9 +30,12 @@ echo ""
# Deleting Elastic Container Registry (ECR)
echo "Deleting Elastic Container Registries (ECR)..."
for repo_name in $(aws ecr describe-repositories --region "$REGION" --query "repositories[?Tags[?Key=='$TAG_KEY'&&Value=='$TAG_VALUE']].repositoryName" --output text); do
aws ecr delete-repository --region "$REGION" --repository-name "$repo_name" --force
done
aws ecr delete-repository --region "$REGION" --repository-name "$ECR_NAME" --force
if [ $? -eq 0 ]; then
echo "ECR repository $ECR_NAME deleted successfully."
else
echo "Error deleting ECR repository $ECR_NAME."
fi
echo ""
echo "---------------------------------------"
echo ""
@ -36,6 +45,11 @@ echo ""
echo "Deleting VPCs..."
for vpc_id in $(aws ec2 describe-vpcs --region "$REGION" --query "Vpcs[?Tags[?Key=='$TAG_KEY'&&Value=='$TAG_VALUE']].VpcId" --output text); do
aws ec2 delete-vpc --region "$REGION" --vpc-id "$vpc_id"
if [ $? -eq 0 ]; then
echo "VPC $vpc_id deleted successfully."
else
echo "Error deleting VPC $vpc_id."
fi
done
echo ""