-
Notifications
You must be signed in to change notification settings - Fork 0
/
aws.js
93 lines (79 loc) · 2.81 KB
/
aws.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
/* eslint-disable no-await-in-loop */
const { LambdaClient, GetFunctionCommand } = require('@aws-sdk/client-lambda');
const { STSClient, GetCallerIdentityCommand } = require('@aws-sdk/client-sts');
const { CloudFormationClient, DescribeStacksCommand, ListStackResourcesCommand } = require('@aws-sdk/client-cloudformation');
const { AccountClient, ListRegionsCommand } = require('@aws-sdk/client-account');
async function getRegions() {
// region is required but does not matter as we just a list of regions enabled in one account
const accountClient = new AccountClient({ region: 'eu-west-1' });
let nextToken = null;
let regions = [];
do {
const input = {
NextToken: nextToken,
RegionOptStatusContains: [
'ENABLING',
'ENABLED_BY_DEFAULT',
],
};
const command = new ListRegionsCommand(input);
const response = await accountClient.send(command);
regions = regions.concat(response.Regions.map((region) => region.RegionName));
nextToken = response.NextToken;
} while (nextToken);
return regions;
}
async function getAccountId() {
// region is required but does not matter as we just want the account number
const stsClient = new STSClient({ region: 'eu-west-1' });
const command = new GetCallerIdentityCommand({});
const response = await stsClient.send(command);
return response.Account;
}
async function describeCloudformationStacks(region) {
const client = new CloudFormationClient({ region });
let nextToken = null;
let stacks = [];
do {
const input = {
NextToken: nextToken,
};
const command = new DescribeStacksCommand(input);
const response = await client.send(command);
const existingStacks = response.Stacks.filter((s) => s.StackStatus !== 'DELETE_COMPLETE'); // Don't include deleted stacks
stacks = stacks.concat(existingStacks);
nextToken = response.NextToken;
} while (nextToken);
return stacks;
}
async function listStackResourceSummaries(region, stackName) {
const client = new CloudFormationClient({ region });
let nextToken = null;
let stackResourceSummaries = [];
do {
const input = {
StackName: stackName,
NextToken: nextToken,
};
const command = new ListStackResourcesCommand(input);
const response = await client.send(command);
stackResourceSummaries = stackResourceSummaries.concat(response.StackResourceSummaries);
nextToken = response.NextToken;
} while (nextToken);
return stackResourceSummaries;
}
async function getFunction(region, functionName) {
const client = new LambdaClient({ region });
const input = {
FunctionName: functionName,
};
const command = new GetFunctionCommand(input);
return client.send(command);
}
module.exports = {
getRegions,
getAccountId,
describeCloudformationStacks,
listStackResourceSummaries,
getFunction,
};