Skip to content

Commit 2c445ee

Browse files
author
Sean Sundberg
authored
Adds delete-vpc-sh script with instructions (#21)
Signed-off-by: Sean Sundberg <[email protected]>
1 parent 08ff215 commit 2c445ee

File tree

2 files changed

+187
-0
lines changed

2 files changed

+187
-0
lines changed

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,33 @@ module "dev_vpc" {
3838
ibmcloud_api_key = var.ibmcloud_api_key
3939
}
4040
```
41+
42+
## Supporting resources
43+
44+
### delete-vpc.sh
45+
46+
Cleaning up a VPC instance can be difficult because the resources need to be removed in a particular order. Running a `terraform delete` from the terraform state that provisioned the VPC instance is the most reliable way to clean up the resources. However, if the terraform state gets corrupted or lost or the VPC resources were provisioned by hand then an alternative approach is required. In order to address this issue, a script has been provided in [scripts/delete-vpc.sh](./scripts/delete-vpc.sh).
47+
48+
#### Prerequisites
49+
50+
##### Software
51+
52+
The `delete-vpc.sh` script has the following software requirements:
53+
54+
- ibmcloud cli - https://cloud.ibm.com/docs/cli?topic=cli-install-ibmcloud-cli
55+
- ibmcloud vpc infrastructure (is) plugin - https://cloud.ibm.com/docs/cli?topic=vpc-infrastructure-cli-plugin-vpc-reference
56+
- `jq` cli - https://stedolan.github.io/jq/download/
57+
58+
##### Environment
59+
60+
The `delete-vpc.sh` script assumes that you have already logged into the IBM Cloud account where the VPC resources have been deployed using the ibmcloud cli. For more information see https://cloud.ibm.com/docs/cli?topic=cli-ibmcloud_cli#ibmcloud_login
61+
62+
#### Usage
63+
64+
Assuming the prerequisites have been met, the script can be run by passing the name of the VPC to remove as the only argument. E.g.
65+
66+
```shell
67+
./delete-vpc.sh my-vpc
68+
```
69+
70+
The script will delete all of the resources under the VPC in order then finally delete the VPC instance itself.

scripts/delete-vpc.sh

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
#!/bin/bash
2+
3+
VPC_NAME="$1"
4+
5+
set -e
6+
7+
echo "*** Deleting instances..."
8+
echo ""
9+
10+
ibmcloud is instances --all-resource-groups --output JSON | \
11+
jq -c --arg VPC_NAME $VPC_NAME '.[] | select(.vpc.name == $VPC_NAME)' | \
12+
while read instance;
13+
do
14+
15+
id=$(echo "$instance" | jq -r '.id')
16+
name=$(echo "$instance" | jq -r '.name')
17+
18+
echo $instance | jq -c '.network_interfaces | .[] | .floating_ips | .[]' | \
19+
while read floating_ip;
20+
do
21+
22+
floating_ip_id=$(echo "$floating_ip" | jq -r '.id')
23+
floating_ip_name=$(echo "$floating_ip" | jq -r '.name')
24+
25+
echo "Releasing instance floating ip: ${floating_ip_name} (${floating_ip_id})"
26+
ibmcloud is floating-ip-release -f "${floating_ip_id}"
27+
done
28+
29+
echo "Stopping instance: ${name} (${id})"
30+
ibmcloud is instance-stop -f "${id}"
31+
32+
count=0
33+
while [[ $(ibmcloud is instance "${id}" --output JSON | jq -r '.status') == "stopping" ]] && [[ ${count} -lt 10 ]]; do
34+
echo "Waiting for instance to stop: ${name} (${id})"
35+
sleep 30
36+
37+
count=$((count + 1))
38+
done
39+
if [[ $count -eq 10 ]]; then
40+
echo "Timed out waiting for instance to stop: ${name} (${id})"
41+
exit 1
42+
fi
43+
44+
echo "Deleting instance: ${name} (${id})"
45+
ibmcloud is instance-delete -f "${id}"
46+
47+
count=0
48+
while ibmcloud is instance "${id}" && [[ ${count} -lt 10 ]]; do
49+
echo "Waiting for instance to be deleted: ${name} (${id})"
50+
sleep 30
51+
52+
count=$((count + 1))
53+
done
54+
if [[ $count -eq 10 ]]; then
55+
echo "Timed out waiting for instance to be deleted: ${name} (${id})"
56+
exit 1
57+
fi
58+
done
59+
60+
echo ""
61+
echo "*** Deleting endpoint gateways..."
62+
echo ""
63+
64+
ibmcloud is endpoint-gateways --all-resource-groups --output JSON | \
65+
jq -c --arg VPC_NAME sms-vpn-mgmt-vpc '.[] | select(.vpc.name == $VPC_NAME)' | \
66+
while read endpoint;
67+
do
68+
69+
id=$(echo "$endpoint" | jq -r '.id')
70+
name=$(echo "$endpoint" | jq -r '.name')
71+
72+
echo "$endpoint" | jq -c '.ips | .[]' | \
73+
while read endpoint_ip;
74+
do
75+
ip_id=$(echo "$endpoint_ip" | jq -r '.id')
76+
ip_name=$(echo "$endpoint_ip" | jq -r '.name')
77+
78+
echo "Unbinding endpoint gateway ip: ${ip_name} (${ip_id})"
79+
ibmcloud is endpoint-gateway-reserved-ip-unbind -f "${id}" --reserved-ip-id "${ip_id}"
80+
done
81+
82+
echo "Deleting endpoint gateway: ${name} (${id})"
83+
ibmcloud is endpoint-gateway-delete -f "${id}"
84+
done
85+
86+
echo ""
87+
echo "*** Deleting subnets..."
88+
echo ""
89+
90+
ibmcloud is subnets --all-resource-groups --output JSON | \
91+
jq -c --arg VPC_NAME "${VPC_NAME}" '.[] | select(.vpc.name == $VPC_NAME)' | \
92+
while read subnet;
93+
do
94+
95+
id=$(echo "$subnet" | jq -r '.id')
96+
name=$(echo "$subnet" | jq -r '.name')
97+
public_gateway_id=$(echo "$subnet" | jq -r '.public_gateway.id // ""')
98+
public_gateway_name=$(echo "$subnet" | jq -r '.public_gateway.name // ""')
99+
100+
if [[ -n "${public_gateway_id}" ]]; then
101+
echo "Detach public gateway from subnet: ${public_gateway_name} (${public_gateway_id})"
102+
ibmcloud is subnet-public-gateway-detach "${id}"
103+
fi
104+
105+
ibmcloud is subnet-reserved-ips "${id}" | \
106+
jq -c '.[]' | \
107+
while read reserved_ip;
108+
do
109+
ip_id=$(echo "$reserved_ip" | jq -r '.id')
110+
ip_name=$(echo "$reserved_ip" | jq -r '.name')
111+
112+
echo "Deleting reserved ip: ${ip_name} (${ip_id})"
113+
ibmcloud is subnet-reserved-ip-delete -f "${id}" "${ip_id}"
114+
done
115+
116+
echo "Deleting subnet: ${name} (${id})"
117+
ibmcloud is subnet-delete -f "${id}"
118+
done
119+
120+
echo ""
121+
echo "*** Deleting public gateways..."
122+
echo ""
123+
124+
ibmcloud is public-gateways --all-resource-groups --output JSON | \
125+
jq -c --arg VPC_NAME sms-vpn-vpc '.[] | select(.vpc.name == $VPC_NAME)' | \
126+
while read gateway;
127+
do
128+
129+
id=$(echo "$gateway" | jq -r '.id')
130+
name=$(echo "$gateway" | jq -r '.name')
131+
floating_ip_id=$(echo "$gateway" | jq -r '.floating_ip.id')
132+
floating_ip_name=$(echo "$gateway" | jq -r '.floating_ip.name')
133+
134+
echo "Deleting public gateway: ${name} (${id})"
135+
ibmcloud is public-gateway-delete -f "${id}"
136+
137+
# if [[ -n "${floating_ip_id}" ]]; then
138+
# echo "Releasing gateway floating ip: ${floating_ip_name} (${floating_ip_id})"
139+
# ibmcloud is floating-ip-release -f "${floating_ip_id}"
140+
# fi
141+
done
142+
143+
echo ""
144+
echo "*** Deleting VPC..."
145+
echo ""
146+
147+
ibmcloud is vpcs --all-resource-groups --output JSON | \
148+
jq -c --arg VPC_NAME "${VPC_NAME}" '.[] | select(.name == $VPC_NAME)' | \
149+
while read vpc;
150+
do
151+
152+
id=$(echo "$vpc" | jq -r '.id')
153+
name="${VPC_NAME}"
154+
155+
echo "Deleting VPC: ${name} (${id})"
156+
ibmcloud is vpc-delete -f "${id}"
157+
done

0 commit comments

Comments
 (0)