-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEC2.js
186 lines (177 loc) · 5.63 KB
/
EC2.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
require('rubico/global')
const AWSEC2 = require('aws-sdk/clients/ec2')
const AWSEC2DescribeInstancesFilters = require('./internal/AWSEC2DescribeInstancesFilters.js')
const filterExistsAndNotEmpty = require('./internal/filterExistsAndNotEmpty')
/**
* @name EC2
*
* @synopsis
* ```coffeescript [specscript]
* new EC2(options {
* ...({
* accessKeyId: string,
* secretAccessKey: string,
* region: string,
* })|({
* endpoint: string,
* region: string,
* })
* }) -> EC2
* ```
*
* @description
* https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/EC2.html
*/
const EC2 = function (options) {
this.awsEc2 = new AWSEC2({
apiVersion: '2016-11-15',
...pick([
'accessKeyId',
'secretAccessKey',
'region',
'endpoint',
])(options),
})
return this
}
/**
* @name EC2.prototype.listInstances
*
* @synopsis
* ```coffeescript [specscript]
* import EC2ListInstancesDescribeFilterOptions from './internal/EC2ListInstancesDescribeFilterOptions.ss'
*
* new EC2(...).listInstances(options? {
* ...EC2ListInstancesDescribeFilterOptions.map(value => value|Array<value>),
* limit?: 5-1000, // default 1000
* nextToken?: string, // last result's NextToken
* }) -> Promise<{
* Instances: Array<{
* InstanceId: string,
* AmiLaunchIndex: number, // 0
* ImageId: string, // 'ami-09625adacc474a7b4'
* InstanceId: 'i-04a9d2103428c73c0'
* InstanceType: string, // 't2.micro'
* KeyName: string, // 'solumlibs-test'
* LaunchTime: Date, // 2022-04-28T17:08:41.000Z
* Monitoring: {
* State: string, // 'disabled'
* },
* Placement: {
* AvailabilityZone: string, // 'us-west-1a'
* GroupName: string, // ''
* Tenancy: string, // 'default'
* },
* PrivateDnsName: string, // 'ip-172-31-31-44.us-west-1.compute.internal'
* PrivateIpAddress: string, // '172.31.31.44'
* ProductCodes: Array<string>,
* PublicDnsName: string, // 'ec2-3-101-88-163.us-west-1.compute.amazonaws.com'
* PublicIpAddress: string, // '3.101.88.163'
* State: {
* Code: number, // 16
* Name: string, // 'running'
* },
* StateTransitionReason: string,
* SubnetId: string, // 'subnet-916bb8f7'
* VpcId: string, // 'vpc-9c42a2fa'
* Architecture: string, // 'x86_64'
* BlockDeviceMappings: Array<Object>,
* ClientToken: string,
* EbsOptimized: boolean,
* EnaSupport: boolean,
* Hypervisor: string, // 'xen'
* ElasticGpuAssociations: Array,
* ElasticInferenceAcceleratorAssociations: Array,
* NetworkInterfaces: Array<Object>,
* RootDeviceName: string, // '/dev/xvda'
* RootDeviceType: string, // 'ebs'
* SecurityGroups: Array<Object>,
* SourceDestCheck: boolean,
* Tags: Array<Object>,
* VirtualizationType: string, // 'hvm'
* CpuOptions: {
* CoreCount: number, // 1
* ThreadsPerCore: number, // 1
* },
* CapacityReservationSpecification: {
* CapacityReservationPreference: string, // 'open'
* },
* HibernationOptions: {
* Configured: boolean, // false
* },
* Licenses: [],
* MetadataOptions: {
* State: string, // 'applied'
* HttpTokens: string, // 'optional'
* HttpPutResponseHopLimit: number, // 1
* HttpEndpoint: string, // 'enabled'
* },
* EnclaveOptions: {
* Enabled: boolean, // false
* },
* AvailabilityZone: string, // 'us-west-1a'
* Events: Array,
* InstanceState: { // the intended state of the instance
* Code: number, // 16
* Name: string, // 'running'
* },
* InstanceStatus: { // reports impaired functionality that stems from issues internal to the instance, such as impaired reachability
* Details: Array,
* Status: string, // 'ok'
* },
* SystemStatus: { // reports impaired functionality that stems from issues related to systems that support the instance, such as hardware failures and network connectivity problems
* Details: Array,
* Status: string, // 'ok'
* }
* }>,
* NextToken: string|null,
* }>
* ```
*/
EC2.prototype.listInstances = async function (options = {}) {
const {
Reservations: reservations,
NextToken: nextToken,
} = await this.awsEc2.describeInstances(filterExistsAndNotEmpty({
Filters: AWSEC2DescribeInstancesFilters(options),
MaxResults: options.limit ?? 1000,
NextToken: options.nextToken,
})).promise()
const instances = reservations.flatMap(get('Instances'))
const instanceIds = instances.map(get('InstanceId'))
const instanceIdStatusMap = new Map()
let instanceIndex = 0
while (instanceIndex < instanceIds.length) {
const { InstanceStatuses: instanceStatuses } =
await this.awsEc2.describeInstanceStatus({
InstanceIds: instanceIds.slice(instanceIndex, (instanceIndex += 100)),
IncludeAllInstances: true,
}).promise()
for (const instanceStatus of instanceStatuses) {
instanceIdStatusMap.set(instanceStatus.InstanceId, instanceStatus)
}
}
const instancesWithStatusFields = instances.map(instance => ({
...instance,
...instanceIdStatusMap.get(instance.InstanceId),
}))
return {
Instances: instancesWithStatusFields,
NextToken: nextToken,
}
}
/**
* @name EC2.prototype.terminateInstances
*
* @synopsis
* ```coffeescript [specscript]
* new EC2(...).terminateInstances(instanceIds Array<string>)
* -> Promise<{
* TerminatingInstances: Array<{}>,
* }>
* ```
*/
EC2.prototype.terminateInstances = async function (instanceIds) {
return this.awsEc2.terminateInstances({ InstanceIds: instanceIds }).promise()
}
module.exports = EC2