-
Notifications
You must be signed in to change notification settings - Fork 0
/
vpc.js
139 lines (121 loc) · 6.01 KB
/
vpc.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
/**
* This file exports env vars from the default vpc for use with CI
*/
const AWS = require("aws-sdk");
const shell = require("shelljs");
const path = require("path");
const getAWSAccountId = (credentials) => new Promise((resolve, reject) =>
(new AWS.STS({credentials}))
.getCallerIdentity({}, (err, data) => {
if (err) {
console.error("Error while calling sts.getCallerIdentity. You most likely forgot to set up aws credentials. See https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/loading-node-credentials-shared.html for more information");
reject(err);
} else if (!data.Account) {
console.error("Error while getting data.Account. This is unexpected.");
reject(data);
} else {
resolve(data.Account);
}
})
);
const getDefaultVpcId = async (credentials) => {
const {Vpcs} = await new Promise((resolve, reject) =>
(new AWS.EC2({credentials}))
.describeVpcs({Filters: [{Name: "isDefault", Values: ["true"]}]}, (err, data) => {
if (err) {
console.error("Error while calling ec2.describeVpcs. You most likely forgot to set up aws credentials. See https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/loading-node-credentials-shared.html for more information");
reject(err);
} else if (!data.Vpcs || !data.Vpcs.length) {
console.error("Error while getting data.Vpcs. This is unexpected.");
reject(data);
} else {
resolve(data);
}
})
);
return Vpcs[0].VpcId;
};
const getDefaultVpcSecurityGroupId = async (credentials, vpcId) => {
const {SecurityGroups} = await new Promise((resolve, reject) =>
(new AWS.EC2({credentials}))
.describeSecurityGroups({Filters: [{Name: "group-name", Values: ["default"]}]}, (err, data) => {
if (err) {
console.error("Error while calling ec2.describeSecurityGroups. You most likely forgot to set up aws credentials. See https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/loading-node-credentials-shared.html for more information");
reject(err);
} else if (!data.SecurityGroups || !data.SecurityGroups.length) {
console.error("Error while getting data.SecurityGroups. This is unexpected");
reject(data);
} else {
resolve(data);
}
})
);
return SecurityGroups[0].GroupId;
};
const getDefaultVpcSubnetIds = async (credentials, vpcId) => {
const {Subnets} = await new Promise((resolve, reject) =>
(new AWS.EC2({credentials}))
.describeSubnets({Filters: [{Name: "vpc-id", Values: [vpcId]}]}, (err, data) => {
if (err) {
console.error("Error while calling ec2.describeSubnets. You most likely forgot to set up aws credentials. See https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/loading-node-credentials-shared.html for more information");
reject(err);
} else if (!data.Subnets || !data.Subnets.length) {
console.error("Error while getting data.Vpcs. This is unexpected.");
reject(data);
} else {
resolve(data);
}
})
);
return Subnets.map(({SubnetId}) => SubnetId);
};
const run = async () => {
if (process.argv.length !== 3) {
throw new Error("Invalid number of arguments. Please run node vpc.js <vpc/subnet/sg>");
}
// console.log("================== ENVIRONMENT VARIABLES ==================");
// NODE_ENV
const DEFAULT_NODE_ENV = 'dev';
// console.log(`NODE_ENV:\t\t\t\t\t\t${shell.env.NODE_ENV}${!shell.env.NODE_ENV?` => ${DEFAULT_NODE_ENV}`:''}`);
shell.env.NODE_ENV = process.env.NODE_ENV || DEFAULT_NODE_ENV;
// AWS_PROFILE
const DEFAULT_AWS_PROFILE = `highsugar`;
// console.log(`AWS_PROFILE:\t\t\t\t\t${shell.env.AWS_PROFILE}${!shell.env.AWS_PROFILE?` => ${DEFAULT_AWS_PROFILE}`:''}`);
shell.env.AWS_PROFILE = process.env.AWS_PROFILE || DEFAULT_AWS_PROFILE;
// AWS_ACCESS_KEY_ID
// console.log(`AWS_ACCESS_KEY_ID:\t\t\t\t${shell.env.AWS_ACCESS_KEY_ID}`);
// AWS_SECRET_ACCESS_KEY
// console.log(`AWS_SECRET_ACCESS_KEY:\t\t\t${shell.env.AWS_SECRET_ACCESS_KEY}`);
// AWS_ACCOUNT_ID
const credentials = (
shell.env.AWS_ACCESS_KEY_ID &&
shell.env.AWS_SECRET_ACCESS_KEY &&
shell.env.CI === true // AWS.Credentials only on CI
)
? new AWS.Credentials(shell.env.AWS_ACCESS_KEY_ID, shell.env.AWS_SECRET_ACCESS_KEY)
: new AWS.SharedIniFileCredentials({profile: shell.env.AWS_PROFILE});
shell.env.AWS_ACCOUNT_ID = await getAWSAccountId(credentials);
// console.log(`AWS_ACCOUNT_ID:\t\t\t\t\t${(shell.env.AWS_ACCOUNT_ID)}`);
// AWS_REGION
const DEFAULT_AWS_REGION = 'ap-southeast-1';
// console.log(`AWS_REGION:\t\t\t\t\t\t${shell.env.AWS_REGION}${!shell.env.AWS_REGION?` => ${DEFAULT_AWS_REGION}`:''}`);
shell.env.AWS_REGION = process.env.AWS_REGION || DEFAULT_AWS_REGION;
// Command
const defaultVpcId = await getDefaultVpcId(credentials);
if (process.argv[2].toLowerCase().includes('vpc')) {
console.log(defaultVpcId);
} else if (process.argv[2].toLowerCase().includes('subnet')) {
const vpcs = await getDefaultVpcSubnetIds(credentials, defaultVpcId);
console.log(vpcs.join(','));
} else if (process.argv[2].toLowerCase().includes('sg')) {
const securityGroups = await getDefaultVpcSecurityGroupId(credentials, defaultVpcId);
console.log(securityGroups);
} else {
throw new Error(`Unrecognized command ${process.argv[2]}. Please use one of vpc/subnet/sg`);
}
};
module.exports = run;
run().catch(err => {
console.error(err);
process.exit(1);
});