forked from dnguyenv/aws-cleanup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clean.js
executable file
·350 lines (307 loc) · 12.1 KB
/
clean.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#!/usr/bin/env node
const AWS = require('aws-sdk')
const Promise = require('bluebird')
const _ = require('lodash')
const yargs = require('yargs')
const bunyan = require('bunyan')
//let log
const efs = new AWS.EFS({ apiVersion: '2015-02-01', region: process.env.AWS_REGION })
const ec2 = new AWS.EC2({ apiVersion: '2016-11-15' })
const elb = new AWS.ELBv2({ apiVersion: '2015-12-01' });
const elbClassic = new AWS.ELB();
const yargsFunc = (yargs) => {
yargs.positional('vpc-id', { describe: 'ID of the VPC', default: null })
}
yargs
.command('delete [vpc-id]', 'Delete the VPC and all dependencies', yargsFunc, async (argv) => {
const stream = argv.logToStdout ? process.stdout : process.stderr
//log = bunyan.createLogger({ name: "delete-vpc", level: argv.logLevel, stream })
argv.dryRun = false;
await deleteEC2Instances(argv.vpcId, argv.dryRun) // Remove EC2 Instances
await deleteELBs(argv.vpcId, argv.dryRun) // Remove Load Balancers
await deleteELBsClassic(argv.vpcId, argv.dryRun) // Remove Load Balancers - Classic type
await deleteEFS(argv.vpcId, argv.dryRun) // Remove EFS volumes
await deleteNATGateways(argv.vpcId, argv.dryRun) // Remove NAT Gateways
await deleteNetworkInterfaces(argv.vpcId, argv.dryRun) // Remove Network Interfaces
await deleteSecurityGroups(argv.vpcId, argv.dryRun) // Remove Security Groups
await deleteInternetGateways(argv.vpcId, argv.dryRun) // Remove Internet Gateways
await deleteSubnets(argv.vpcId, argv.dryRun) // Delete subnets
await deleteRouteTables(argv.vpcId, argv.dryRun) // Remove routables
await deleteVPCEndpoints(argv.vpcId, argv.dryRun) // Remove VPC endpoints
await deleteVPC(argv.vpcId, argv.dryRun) // Remove VPC
//await releaseEIPs(argv.vpcId, argv.dryRun) // Remove Instances
})
.option('log-level', { describe: 'Log level (debug, trace, info, warn, error)', default: 'info' })
.option('log-to-stdout', { describe: 'Output logs to STDOUT instead of STDERR', default: true })
.argv
/*
Release all EIPs ...
*/
async function releaseEIPs(VpcId, DryRun) {
console.log('Releasing EIPs instances .... ')
const params = {
Filters: [
{
Name: 'domain',
Values: [ 'vpc' ]
}]
};
const response = await Promise.fromCallback(cb => ec2.describeAddresses(params, cb))
await Promise.map(response.Addresses, async (item) => {
const allocationID = item.AllocationId
const releaseParams = { DryRun, AllocationId:allocationID }
return acm(ec2, 'releaseAddress', releaseParams, { allowedErrorCodes: ['OperationNotPermitted','InvalidAttachmentID.NotFound'], retryErrorCodes: ['AuthFailure','UnsupportedOperation','InvalidParameterValue','OperationalError'] })
})
}
async function deleteELBs(vpcId, DryRun) {
console.log('Deleting ELBs non-classic instances .... ')
const elbs = await Promise.fromCallback(cb => elb.describeLoadBalancers({}, cb))
const elbsInVPC = elbs.LoadBalancers.filter(x => x.VpcId === vpcId)
return await Promise.map(elbsInVPC, async (elbInstance) => {
const deleteParams = {
LoadBalancerArn: elbInstance.LoadBalancerArn
}
if (DryRun) {
return
}
return await acm(elb, 'deleteLoadBalancer', deleteParams)
})
}
async function deleteEC2Instances(vpcId, DryRun) {
console.log('Deleting EC2 instances .... ')
const filterParams = {
Filters: [
{
Name: 'vpc-id',
Values: [ vpcId ]
}]
}
const reservations = await Promise.fromCallback(cb => ec2.describeInstances(filterParams, cb))
const instancesMap = reservations.Reservations.reduce((accumulator, current) => {
current.Instances.forEach(i => { accumulator[i.InstanceId] = i })
return accumulator
}, {})
const Ids = Object.keys(instancesMap)
if (Ids.length === 0) {
return []
}
const deleteParams = { InstanceIds:Ids, DryRun }
return acm(ec2, 'terminateInstances', deleteParams)
}
async function deleteELBsClassic(vpcId, DryRun) {
console.log('Deleting ELBs classic instances .... ')
const elbs = await Promise.fromCallback(cb => elbClassic.describeLoadBalancers({}, cb))
const elbsInVPC = elbs.LoadBalancerDescriptions.filter(x => x.VPCId === vpcId)
return await Promise.map(elbsInVPC, async (elbInstance) => {
const deleteParams = {
LoadBalancerName: elbInstance.LoadBalancerName
}
if (DryRun) {
return
}
return await acm(elbClassic, 'deleteLoadBalancer', deleteParams)
})
}
async function deleteVPCEndpoints(VpcId, DryRun) {
console.log('Deleting VPC endpoint instances .... ')
const params = {
Filters: [
{
Name: 'vpc-id',
Values: [ VpcId ]
}]
};
const response = await Promise.fromCallback(cb => ec2.describeVpcEndpoints(params, cb))
const endPointIds = response.VpcEndpoints.map(x => x.VpcEndpointId)
await Promise.map(endPointIds, async (id) => {
const params = { VpcEndpointIds:[id], DryRun };
await acm(ec2, 'deleteVpcEndpoints', params)
})
return endPointIds
}
async function deleteInternetGateways(VpcId, DryRun) {
console.log('Deleting IG instances .... ')
const params = {
Filters: [
{
Name: "attachment.vpc-id",
Values: [ VpcId ]
}
]
};
const response = await Promise.fromCallback(cb => ec2.describeInternetGateways(params, cb))
const InternetGatewayIds = response.InternetGateways.map(x => x.InternetGatewayId)
await Promise.map(InternetGatewayIds, async (id) => {
const params = { InternetGatewayId:id, DryRun };
const detachParams = Object.assign({}, params, { VpcId })
await acm(ec2, 'detachInternetGateway', detachParams)
await acm(ec2, 'deleteInternetGateway', params)
})
return InternetGatewayIds
}
async function deleteNATGateways(vpcId, DryRun) {
console.log('Deleting NAT Gateways instances .... ')
const params = {
Filter: [
{
Name: "vpc-id",
Values: [ vpcId ]
}
]
};
const response = await Promise.fromCallback(cb => ec2.describeNatGateways(params, cb))
const NatGatewayIds = response.NatGateways.map(x => x.NatGatewayId)
return await Promise.map(NatGatewayIds, async (id) => {
const params = { NatGatewayId:id };
await acm(ec2, 'deleteNatGateway', params)
})
}
async function getSubnetIds (vpcId) {
const params = {
Filters: [{
Name: "vpc-id",
Values: [ vpcId ]
}]
};
const subnetResponse = await Promise.fromCallback(cb => ec2.describeSubnets(params, cb))
return subnetResponse.Subnets.map(x => x.SubnetId)
}
async function deleteEFS (vpcId, DryRun) {
console.log('Deleting EFS instances .... ')
const response = await Promise.fromCallback(cb => efs.describeFileSystems({}, cb))
const fileSystemIds = response.FileSystems.map(x => x.FileSystemId)
const mountTargets = await Promise.reduce(fileSystemIds, async (memo, FileSystemId) => {
const params = { FileSystemId }
const response = await Promise.fromCallback(cb => efs.describeMountTargets(params, cb))
return [].concat(memo).concat(response.MountTargets)
}, [])
const subnetIds = await getSubnetIds(vpcId)
const mountTargetsToBeDeleted = mountTargets.filter(x => {
return subnetIds.includes(x.SubnetId)
})
const fileSystemsToBeDeleted = _.uniq(mountTargetsToBeDeleted.map(x => x.FileSystemId))
await Promise.map(mountTargetsToBeDeleted, async (mountTarget) => {
const params = { MountTargetId: mountTarget.MountTargetId }
return await acm(efs, 'deleteMountTarget', params)
})
await Promise.delay(3000)
await Promise.map(fileSystemsToBeDeleted, async (FileSystemId) => {
const params = { FileSystemId }
return await acm(efs, 'deleteFileSystem', params, { retryErrorCodes: 'FileSystemInUse', retries: 10 })
})
}
async function deleteSubnets (id, DryRun) {
console.log('Deleting subnets instances .... ')
const params = { VpcId: id, DryRun };
const subnetIds = await getSubnetIds(id)
await Promise.delay(3000)
await Promise.map(subnetIds, async (SubId) => {
const params = {SubnetId:SubId, DryRun}
await acm(ec2, 'deleteSubnet', params, { retryErrorCodes: 'DependencyViolation' })
})
}
async function deleteVPC (id, DryRun) {
console.log('Finallay, deleting VPC instances .... ')
const params = { VpcId: id, DryRun };
await acm(ec2, 'deleteVpc', params, { allowedErrorCodes: 'InvalidVpcID.NotFound' })
}
async function deleteSecurityGroups (vpcId, DryRun) {
var params = {
DryRun,
Filters: [{
Name: 'vpc-id',
Values: [ vpcId ]
}]
}
const securityGroups = (await Promise.fromCallback(cb => ec2.describeSecurityGroups(params, cb))).SecurityGroups;
await Promise.mapSeries(securityGroups, async (securityGroup) => {
await Promise.mapSeries(securityGroup.IpPermissions, async (ruleUnfiltered) => {
const rule = {}
rule.GroupId = securityGroup.GroupId
if (!_.isEmpty(ruleUnfiltered.IpRanges)) {
const ipRange = ruleUnfiltered.IpRanges[0]
rule.IpProtocol = ruleUnfiltered.IpProtocol
rule.FromPort = ruleUnfiltered.FromPort
rule.ToPort = ruleUnfiltered.ToPort
rule.CidrIp = ipRange.CidrIp
}
if (!_.isEmpty(ruleUnfiltered.UserIdGroupPairs)) {
rule.IpPermissions = [ _.pick(ruleUnfiltered, ['IpProtocol', 'UserIdGroupPairs', 'FromPort', 'ToPort']) ]
}
await acm(ec2, 'revokeSecurityGroupIngress', rule)
})
return
})
const sgIds = securityGroups.filter(x => x.GroupName !== 'default').map(x => x.GroupId)
await Promise.delay(1000)
await Promise.map(sgIds, async function (id) {
const params = { GroupId:id, DryRun }
return await acm(ec2, 'deleteSecurityGroup', params)
})
}
async function deleteNetworkInterfaces (VpcId, DryRun) {
const queryParams = {
DryRun,
Filters: [
{
Name: 'vpc-id',
Values: [ VpcId ]
}
]
};
const response = await Promise.fromCallback(cb => ec2.describeNetworkInterfaces(queryParams, cb))
const networkInterfaceIds = response.NetworkInterfaces.map(x => x.NetworkInterfaceId)
const networkInterfaceAttachmentIds = response.NetworkInterfaces.map(x => _.get(x, 'Attachment.AttachmentId')).filter(x => !!x)
await Promise.map(networkInterfaceAttachmentIds, async (id) => {
const detachParams = { AttachmentId:id, Force: true, DryRun }
await acm(ec2, 'detachNetworkInterface', detachParams, { allowedErrorCodes: ['OperationNotPermitted','InvalidAttachmentID.NotFound'], retryErrorCodes: ['AuthFailure','UnsupportedOperation','InvalidParameterValue','OperationalError'] })
})
await Promise.map(networkInterfaceIds, async (id) => {
const deleteParams = { DryRun, NetworkInterfaceId:id }
return acm(ec2, 'deleteNetworkInterface', deleteParams, { allowedErrorCodes: 'InvalidNetworkInterfaceID.NotFound', retryErrorCodes: ['AuthFailure','UnsupportedOperation','InvalidParameterValue','OperationalError'] })
})
}
async function deleteRouteTables (VpcId, DryRun) {
const queryParams = {
DryRun,
Filters: [
{
Name: 'vpc-id',
Values: [ VpcId ]
},
]
}
const response = await Promise.fromCallback(cb => ec2.describeRouteTables(queryParams, cb))
const routeTableIds = response.RouteTables.map(x => x.RouteTableId)
return await Promise.map(routeTableIds, async (id) => {
const query = { RouteTableId: id, DryRun }
return acm(ec2, 'deleteRouteTable', query,{ allowedErrorCodes: 'DependencyViolation' } )
})
}
/*
Core function
*/
async function acm(classInstance, methodName, query, opts = {}) {
// const className = classInstance.endpoint.host.split('.')[0]
let response
try {
response = await Promise.fromCallback(cb => classInstance[methodName](query, cb))
} catch (err) {
if (opts.retryErrorCodes && opts.retryErrorCodes.includes(err.code)) {
opts.retries = ((_.isNumber(opts.retries) ? opts.retries : 20) - 1)
if (opts.retries <= 0) {
return
}
await Promise.delay(opts.retryDelay || 5000)
return acm(classInstance, methodName, query, opts)
}
if (opts.allowedErrorCodes && opts.allowedErrorCodes.includes(err.code)) {
return
}
if (opts.unHandledErrorCodes && opts.unHandledErrorCodes.includes(err.code)) {
throw err
}
throw err
}
return response
}