-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
volume.ts
203 lines (181 loc) · 5.25 KB
/
volume.ts
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
// Unfortunately a mostly-literal copy from ec2/volume.ts because this feature
// existed first in the "autoscaling" module before it existed in the "ec2"
// module so we couldn't standardize the structs in the right way.
/**
* Block device
*/
export interface BlockDevice {
/**
* The device name exposed to the EC2 instance
*
* Supply a value like `/dev/sdh`, `xvdh`.
*
* @see https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/device_naming.html
*/
readonly deviceName: string;
/**
* Defines the block device volume, to be either an Amazon EBS volume or an ephemeral instance store volume
*
* Supply a value like `BlockDeviceVolume.ebs(15)`, `BlockDeviceVolume.ephemeral(0)`.
*/
readonly volume: BlockDeviceVolume;
/**
* If false, the device mapping will be suppressed.
* If set to false for the root device, the instance might fail the Amazon EC2 health check.
* Amazon EC2 Auto Scaling launches a replacement instance if the instance fails the health check.
*
* @default true - device mapping is left untouched
* @deprecated use `BlockDeviceVolume.noDevice()` as the volume to supress a mapping.
*
*/
readonly mappingEnabled?: boolean;
}
/**
* Base block device options for an EBS volume
*/
export interface EbsDeviceOptionsBase {
/**
* Indicates whether to delete the volume when the instance is terminated.
*
* @default - true for Amazon EC2 Auto Scaling, false otherwise (e.g. EBS)
*/
readonly deleteOnTermination?: boolean;
/**
* The number of I/O operations per second (IOPS) to provision for the volume.
*
* Must only be set for {@link volumeType}: {@link EbsDeviceVolumeType.IO1}
*
* The maximum ratio of IOPS to volume size (in GiB) is 50:1, so for 5,000 provisioned IOPS,
* you need at least 100 GiB storage on the volume.
*
* @see https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/EBSVolumeTypes.html
*
* @default - none, required for {@link EbsDeviceVolumeType.IO1}
*/
readonly iops?: number;
/**
* The EBS volume type
* @see https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/EBSVolumeTypes.html
*
* @default {@link EbsDeviceVolumeType.GP2}
*/
readonly volumeType?: EbsDeviceVolumeType;
}
/**
* Block device options for an EBS volume
*/
export interface EbsDeviceOptions extends EbsDeviceOptionsBase {
/**
* Specifies whether the EBS volume is encrypted.
* Encrypted EBS volumes can only be attached to instances that support Amazon EBS encryption
*
* @see https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/EBSEncryption.html#EBSEncryption_supported_instances
*
* @default false
*/
readonly encrypted?: boolean;
}
/**
* Block device options for an EBS volume created from a snapshot
*/
export interface EbsDeviceSnapshotOptions extends EbsDeviceOptionsBase {
/**
* The volume size, in Gibibytes (GiB)
*
* If you specify volumeSize, it must be equal or greater than the size of the snapshot.
*
* @default - The snapshot size
*/
readonly volumeSize?: number;
}
/**
* Properties of an EBS block device
*/
export interface EbsDeviceProps extends EbsDeviceSnapshotOptions {
/**
* The snapshot ID of the volume to use
*
* @default - No snapshot will be used
*/
readonly snapshotId?: string;
}
/**
* Describes a block device mapping for an EC2 instance or Auto Scaling group.
*/
export class BlockDeviceVolume {
/**
* @internal
*/
public static _NO_DEVICE = new BlockDeviceVolume();
/**
* Creates a new Elastic Block Storage device
*
* @param volumeSize The volume size, in Gibibytes (GiB)
* @param options additional device options
*/
public static ebs(volumeSize: number, options: EbsDeviceOptions = {}): BlockDeviceVolume {
return new this({ ...options, volumeSize });
}
/**
* Creates a new Elastic Block Storage device from an existing snapshot
*
* @param snapshotId The snapshot ID of the volume to use
* @param options additional device options
*/
public static ebsFromSnapshot(snapshotId: string, options: EbsDeviceSnapshotOptions = {}): BlockDeviceVolume {
return new this({ ...options, snapshotId });
}
/**
* Creates a virtual, ephemeral device.
* The name will be in the form ephemeral{volumeIndex}.
*
* @param volumeIndex the volume index. Must be equal or greater than 0
*/
public static ephemeral(volumeIndex: number) {
if (volumeIndex < 0) {
throw new Error(`volumeIndex must be a number starting from 0, got "${volumeIndex}"`);
}
return new this(undefined, `ephemeral${volumeIndex}`);
}
/**
* Supresses a volume mapping
*/
public static noDevice() {
return this._NO_DEVICE;
}
/**
* @param ebsDevice EBS device info
* @param virtualName Virtual device name
*/
protected constructor(public readonly ebsDevice?: EbsDeviceProps, public readonly virtualName?: string) {
}
}
/**
* Supported EBS volume types for blockDevices
*/
export enum EbsDeviceVolumeType {
/**
* Magnetic
*/
STANDARD = 'standard',
/**
* Provisioned IOPS SSD - IO1
*/
IO1 = 'io1',
/**
* General Purpose SSD - GP2
*/
GP2 = 'gp2',
/**
* General Purpose SSD - GP3
*/
GP3 = 'gp3',
/**
* Throughput Optimized HDD
*/
ST1 = 'st1',
/**
* Cold HDD
*/
SC1 = 'sc1',
}