-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathamis.ts
424 lines (377 loc) · 13.7 KB
/
amis.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
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
import * as ec2 from '../../aws-ec2';
import * as ssm from '../../aws-ssm';
// v2 - keep this import as a separate section to reduce merge conflict when forward merging with the v2 branch.
// eslint-disable-next-line
import { Construct } from 'constructs';
/**
* The ECS-optimized AMI variant to use. For more information, see
* [Amazon ECS-optimized AMIs](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/ecs-optimized_AMI.html).
*/
export enum AmiHardwareType {
/**
* Use the standard Amazon ECS-optimized AMI.
*/
STANDARD = 'Standard',
/**
* Use the Amazon ECS GPU-optimized AMI.
*/
GPU = 'GPU',
/**
* Use the Amazon ECS-optimized Amazon Linux 2 (arm64) AMI.
*/
ARM = 'ARM64',
/**
* Use the Amazon ECS-optimized Amazon Linux 2 (Neuron) AMI.
*/
NEURON = 'Neuron',
}
/**
* ECS-optimized Windows version list
*/
export enum WindowsOptimizedVersion {
SERVER_2022 = '2022',
SERVER_2019 = '2019',
SERVER_2016 = '2016',
}
const BR_IMAGE_SYMBOL = Symbol.for(
'@aws-cdk/aws-ecs/lib/amis.BottleRocketImage',
);
/*
* TODO:v2.0.0
* * remove `export` keyword
* * remove @deprecated
*/
/**
* The properties that define which ECS-optimized AMI is used.
*
* @deprecated see `EcsOptimizedImage`
*/
export interface EcsOptimizedAmiProps {
/**
* The Amazon Linux generation to use.
*
* @default AmazonLinuxGeneration.AmazonLinux2
*/
readonly generation?: ec2.AmazonLinuxGeneration;
/**
* The Windows Server version to use.
*
* @default none, uses Linux generation
*/
readonly windowsVersion?: WindowsOptimizedVersion;
/**
* The ECS-optimized AMI variant to use.
*
* @default AmiHardwareType.Standard
*/
readonly hardwareType?: AmiHardwareType;
/**
* Whether the AMI ID is cached to be stable between deployments
*
* By default, the newest image is used on each deployment. This will cause
* instances to be replaced whenever a new version is released, and may cause
* downtime if there aren't enough running instances in the AutoScalingGroup
* to reschedule the tasks on.
*
* If set to true, the AMI ID will be cached in `cdk.context.json` and the
* same value will be used on future runs. Your instances will not be replaced
* but your AMI version will grow old over time. To refresh the AMI lookup,
* you will have to evict the value from the cache using the `cdk context`
* command. See https://docs.aws.amazon.com/cdk/latest/guide/context.html for
* more information.
*
* Can not be set to `true` in environment-agnostic stacks.
*
* @default false
*/
readonly cachedInContext?: boolean;
}
/*
* TODO:v2.0.0 remove EcsOptimizedAmi
*/
/**
* Construct a Linux or Windows machine image from the latest ECS Optimized AMI published in SSM
*
* @deprecated see `EcsOptimizedImage#amazonLinux`, `EcsOptimizedImage#amazonLinux` and `EcsOptimizedImage#windows`
*/
export class EcsOptimizedAmi implements ec2.IMachineImage {
private readonly generation?: ec2.AmazonLinuxGeneration;
private readonly windowsVersion?: WindowsOptimizedVersion;
private readonly hwType: AmiHardwareType;
private readonly amiParameterName: string;
private readonly cachedInContext: boolean;
/**
* Constructs a new instance of the EcsOptimizedAmi class.
*/
constructor(props?: EcsOptimizedAmiProps) {
this.hwType = (props && props.hardwareType) || AmiHardwareType.STANDARD;
if (props && props.generation) { // generation defined in the props object
if (props.generation === ec2.AmazonLinuxGeneration.AMAZON_LINUX && this.hwType !== AmiHardwareType.STANDARD) {
throw new Error('Amazon Linux does not support special hardware type. Use Amazon Linux 2 instead');
} else if (props.windowsVersion) {
throw new Error('"windowsVersion" and Linux image "generation" cannot be both set');
} else {
this.generation = props.generation;
}
} else if (props && props.windowsVersion) {
if (this.hwType !== AmiHardwareType.STANDARD) {
throw new Error('Windows Server does not support special hardware type');
} else {
this.windowsVersion = props.windowsVersion;
}
} else { // generation not defined in props object
// always default to Amazon Linux v2 regardless of HW
this.generation = ec2.AmazonLinuxGeneration.AMAZON_LINUX_2;
}
// set the SSM parameter name
this.amiParameterName = '/aws/service/'
+ (this.windowsVersion ? 'ami-windows-latest/' : 'ecs/optimized-ami/')
+ (this.generation === ec2.AmazonLinuxGeneration.AMAZON_LINUX ? 'amazon-linux/' : '')
+ (this.generation === ec2.AmazonLinuxGeneration.AMAZON_LINUX_2 ? 'amazon-linux-2/' : '')
+ (this.generation === ec2.AmazonLinuxGeneration.AMAZON_LINUX_2023 ? 'amazon-linux-2023/' : '')
+ (this.windowsVersion ? `Windows_Server-${this.windowsVersion}-English-Full-ECS_Optimized/` : '')
+ (this.hwType === AmiHardwareType.GPU ? 'gpu/' : '')
+ (this.hwType === AmiHardwareType.ARM ? 'arm64/' : '')
+ (this.hwType === AmiHardwareType.NEURON ? 'inf/' : '')
+ (this.windowsVersion ? 'image_id' : 'recommended/image_id');
this.cachedInContext = props?.cachedInContext ?? false;
}
/**
* Return the correct image
*/
public getImage(scope: Construct): ec2.MachineImageConfig {
const ami = lookupImage(scope, this.cachedInContext, this.amiParameterName);
const osType = this.windowsVersion ? ec2.OperatingSystemType.WINDOWS : ec2.OperatingSystemType.LINUX;
return {
imageId: ami,
osType,
userData: ec2.UserData.forOperatingSystem(osType),
};
}
}
/**
* Additional configuration properties for EcsOptimizedImage factory functions
*/
export interface EcsOptimizedImageOptions {
/**
* Whether the AMI ID is cached to be stable between deployments
*
* By default, the newest image is used on each deployment. This will cause
* instances to be replaced whenever a new version is released, and may cause
* downtime if there aren't enough running instances in the AutoScalingGroup
* to reschedule the tasks on.
*
* If set to true, the AMI ID will be cached in `cdk.context.json` and the
* same value will be used on future runs. Your instances will not be replaced
* but your AMI version will grow old over time. To refresh the AMI lookup,
* you will have to evict the value from the cache using the `cdk context`
* command. See https://docs.aws.amazon.com/cdk/latest/guide/context.html for
* more information.
*
* Can not be set to `true` in environment-agnostic stacks.
*
* @default false
*/
readonly cachedInContext?: boolean;
}
/**
* Construct a Linux or Windows machine image from the latest ECS Optimized AMI published in SSM
*/
export class EcsOptimizedImage implements ec2.IMachineImage {
/**
* Construct an Amazon Linux 2023 image from the latest ECS Optimized AMI published in SSM
*
* @param hardwareType ECS-optimized AMI variant to use
*/
public static amazonLinux2023(hardwareType = AmiHardwareType.STANDARD, options: EcsOptimizedImageOptions = {}): EcsOptimizedImage {
return new EcsOptimizedImage({
generation: ec2.AmazonLinuxGeneration.AMAZON_LINUX_2023,
hardwareType,
cachedInContext: options.cachedInContext,
});
}
/**
* Construct an Amazon Linux 2 image from the latest ECS Optimized AMI published in SSM
*
* @param hardwareType ECS-optimized AMI variant to use
*/
public static amazonLinux2(hardwareType = AmiHardwareType.STANDARD, options: EcsOptimizedImageOptions = {}): EcsOptimizedImage {
return new EcsOptimizedImage({
generation: ec2.AmazonLinuxGeneration.AMAZON_LINUX_2,
hardwareType,
cachedInContext: options.cachedInContext,
});
}
/**
* Construct an Amazon Linux AMI image from the latest ECS Optimized AMI published in SSM
*/
public static amazonLinux(options: EcsOptimizedImageOptions = {}): EcsOptimizedImage {
return new EcsOptimizedImage({
generation: ec2.AmazonLinuxGeneration.AMAZON_LINUX,
cachedInContext: options.cachedInContext,
});
}
/**
* Construct a Windows image from the latest ECS Optimized AMI published in SSM
*
* @param windowsVersion Windows Version to use
*/
public static windows(windowsVersion: WindowsOptimizedVersion, options: EcsOptimizedImageOptions = {}): EcsOptimizedImage {
return new EcsOptimizedImage({
windowsVersion,
cachedInContext: options.cachedInContext,
});
}
private readonly generation?: ec2.AmazonLinuxGeneration;
private readonly windowsVersion?: WindowsOptimizedVersion;
private readonly hwType?: AmiHardwareType;
private readonly amiParameterName: string;
private readonly cachedInContext: boolean;
/**
* Constructs a new instance of the EcsOptimizedAmi class.
*/
private constructor(props: EcsOptimizedAmiProps) {
this.hwType = props && props.hardwareType;
if (props.windowsVersion) {
this.windowsVersion = props.windowsVersion;
} else if (props.generation) {
this.generation = props.generation;
} else {
throw new Error('This error should never be thrown');
}
// set the SSM parameter name
this.amiParameterName = '/aws/service/'
+ (this.windowsVersion ? 'ami-windows-latest/' : 'ecs/optimized-ami/')
+ (this.generation === ec2.AmazonLinuxGeneration.AMAZON_LINUX ? 'amazon-linux/' : '')
+ (this.generation === ec2.AmazonLinuxGeneration.AMAZON_LINUX_2 ? 'amazon-linux-2/' : '')
+ (this.generation === ec2.AmazonLinuxGeneration.AMAZON_LINUX_2023 ? 'amazon-linux-2023/' : '')
+ (this.windowsVersion ? `Windows_Server-${this.windowsVersion}-English-Full-ECS_Optimized/` : '')
+ (this.hwType === AmiHardwareType.GPU ? 'gpu/' : '')
+ (this.hwType === AmiHardwareType.ARM ? 'arm64/' : '')
+ (this.hwType === AmiHardwareType.NEURON ? 'inf/' : '')
+ (this.windowsVersion ? 'image_id' : 'recommended/image_id');
this.cachedInContext = props?.cachedInContext ?? false;
}
/**
* Return the correct image
*/
public getImage(scope: Construct): ec2.MachineImageConfig {
const ami = lookupImage(scope, this.cachedInContext, this.amiParameterName);
const osType = this.windowsVersion ? ec2.OperatingSystemType.WINDOWS : ec2.OperatingSystemType.LINUX;
return {
imageId: ami,
osType,
userData: ec2.UserData.forOperatingSystem(osType),
};
}
}
/**
* Amazon ECS variant
*/
export enum BottlerocketEcsVariant {
/**
* aws-ecs-1 variant
*/
AWS_ECS_1 = 'aws-ecs-1',
/**
* aws-ecs-1-nvidia variant
*/
AWS_ECS_1_NVIDIA = 'aws-ecs-1-nvidia',
/**
* aws-ecs-2 variant
*/
AWS_ECS_2 = 'aws-ecs-2',
/**
* aws-ecs-2-nvidia variant
*/
AWS_ECS_2_NVIDIA = 'aws-ecs-2-nvidia',
}
/**
* Properties for BottleRocketImage
*/
export interface BottleRocketImageProps {
/**
* The Amazon ECS variant to use.
*
* @default - BottlerocketEcsVariant.AWS_ECS_1
*/
readonly variant?: BottlerocketEcsVariant;
/**
* The CPU architecture
*
* @default - x86_64
*/
readonly architecture?: ec2.InstanceArchitecture;
/**
* Whether the AMI ID is cached to be stable between deployments
*
* By default, the newest image is used on each deployment. This will cause
* instances to be replaced whenever a new version is released, and may cause
* downtime if there aren't enough running instances in the AutoScalingGroup
* to reschedule the tasks on.
*
* If set to true, the AMI ID will be cached in `cdk.context.json` and the
* same value will be used on future runs. Your instances will not be replaced
* but your AMI version will grow old over time. To refresh the AMI lookup,
* you will have to evict the value from the cache using the `cdk context`
* command. See https://docs.aws.amazon.com/cdk/latest/guide/context.html for
* more information.
*
* Can not be set to `true` in environment-agnostic stacks.
*
* @default false
*/
readonly cachedInContext?: boolean;
}
/**
* Construct an Bottlerocket image from the latest AMI published in SSM
*/
export class BottleRocketImage implements ec2.IMachineImage {
/**
* Return whether the given object is a BottleRocketImage
*/
public static isBottleRocketImage(x: any): x is BottleRocketImage {
return x !== null && typeof x === 'object' && BR_IMAGE_SYMBOL in x;
}
private readonly amiParameterName: string;
/**
* Amazon ECS variant for Bottlerocket AMI
*/
private readonly variant: string;
/**
* Instance architecture
*/
private readonly architecture: ec2.InstanceArchitecture;
private readonly cachedInContext: boolean;
/**
* Constructs a new instance of the BottleRocketImage class.
*/
public constructor(props: BottleRocketImageProps = {}) {
this.variant = props.variant ?? BottlerocketEcsVariant.AWS_ECS_1;
this.architecture = props.architecture ?? ec2.InstanceArchitecture.X86_64;
// set the SSM parameter name
this.amiParameterName = `/aws/service/bottlerocket/${this.variant}/${this.architecture}/latest/image_id`;
this.cachedInContext = props.cachedInContext ?? false;
}
/**
* Return the correct image
*/
public getImage(scope: Construct): ec2.MachineImageConfig {
const ami = lookupImage(scope, this.cachedInContext, this.amiParameterName);
return {
imageId: ami,
osType: ec2.OperatingSystemType.LINUX,
userData: ec2.UserData.custom(''),
};
}
}
Object.defineProperty(BottleRocketImage.prototype, BR_IMAGE_SYMBOL, {
value: true,
enumerable: false,
writable: false,
});
function lookupImage(scope: Construct, cachedInContext: boolean | undefined, parameterName: string) {
return cachedInContext
? ssm.StringParameter.valueFromLookup(scope, parameterName)
: ssm.StringParameter.valueForTypedStringParameterV2(scope, parameterName, ssm.ParameterValueType.AWS_EC2_IMAGE_ID);
}