generated from ministryofjustice/template-repository
-
Notifications
You must be signed in to change notification settings - Fork 291
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6881 from ministryofjustice/feature/update-defaul…
…t-vpc-scripts Add `remove default vpc` job to `new-environment`workflow and corresponding script
- Loading branch information
Showing
4 changed files
with
109 additions
and
7 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
53 changes: 53 additions & 0 deletions
53
scripts/internal/remove-default-vpc/remove_default_vpc_new_account.sh
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#!/bin/bash | ||
regions="eu-central-1 eu-west-1 eu-west-2 us-east-1" | ||
account_id="$1" | ||
ROOT_AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID | ||
ROOT_AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY | ||
ROOT_AWS_SESSION_TOKEN=$AWS_SESSION_TOKEN | ||
|
||
getAssumeRoleCfg() { | ||
account_id=$1 | ||
aws sts assume-role --role-arn "arn:aws:iam::${account_id}:role/ModernisationPlatformAccess" --role-session-name "test" --output json > credentials.json | ||
export AWS_ACCESS_KEY_ID=$(jq -r '.Credentials.AccessKeyId' credentials.json) | ||
export AWS_SECRET_ACCESS_KEY=$(jq -r '.Credentials.SecretAccessKey' credentials.json) | ||
export AWS_SESSION_TOKEN=$(jq -r '.Credentials.SessionToken' credentials.json) | ||
} | ||
|
||
echo "account: $account_id" | ||
getAssumeRoleCfg "$account_id" | ||
for region in $regions; do | ||
#Skipping region due to insufficient permissions. | ||
if ! aws ec2 describe-vpcs --region $region &> /dev/null; then | ||
continue | ||
fi | ||
# Get default VPC ID | ||
vpc_id=$(aws ec2 describe-vpcs --region $region --filters Name=isDefault,Values=true | jq -r .Vpcs[0].VpcId) | ||
echo "region: $region vpc: $vpc_id" | ||
if [ "$vpc_id" != "null" ]; then | ||
echo "Deleting default VPC ($vpc_id) in region $region..." | ||
# Detach and delete any internet gateway associated with the VPC | ||
internet_gateway_id=$(aws ec2 describe-internet-gateways --region $region --filters Name=attachment.vpc-id,Values=$vpc_id | jq -r .InternetGateways[0].InternetGatewayId) | ||
if [ "$internet_gateway_id" != "null" ]; then | ||
if ! aws ec2 detach-internet-gateway --region $region --internet-gateway-id $internet_gateway_id --vpc-id $vpc_id &> /dev/null; then | ||
echo "Error: Failed to detach internet gateway for account $account_id. Continuing with the next account..." | ||
continue # Skip to the next iteration of the loop for the next account | ||
fi | ||
aws ec2 delete-internet-gateway --region $region --internet-gateway-id $internet_gateway_id | ||
fi | ||
|
||
# Delete subnets associated with the VPC | ||
subnets=$(aws ec2 describe-subnets --region $region --filters Name=vpc-id,Values=$vpc_id | jq -r .Subnets[].SubnetId) | ||
if [ "$subnets" != "null" ]; then | ||
for subnet_id in $subnets; do | ||
aws ec2 delete-subnet --region $region --subnet-id $subnet_id | ||
done | ||
fi | ||
|
||
# Delete the VPC | ||
aws ec2 delete-vpc --region $region --vpc-id $vpc_id | ||
fi | ||
done | ||
export AWS_ACCESS_KEY_ID=$ROOT_AWS_ACCESS_KEY_ID | ||
export AWS_SECRET_ACCESS_KEY=$ROOT_AWS_SECRET_ACCESS_KEY | ||
export AWS_SESSION_TOKEN=$ROOT_AWS_SESSION_TOKEN | ||
rm credentials.json |