1
1
import autoscaling = require( '@aws-cdk/aws-autoscaling' ) ;
2
- import cloudwatch = require ( '@aws-cdk/aws-cloudwatch' ) ;
2
+ import cloudwatch = require( '@aws-cdk/aws-cloudwatch' ) ;
3
3
import ec2 = require( '@aws-cdk/aws-ec2' ) ;
4
4
import iam = require( '@aws-cdk/aws-iam' ) ;
5
5
import cloudmap = require( '@aws-cdk/aws-servicediscovery' ) ;
6
6
import ssm = require( '@aws-cdk/aws-ssm' ) ;
7
- import { Construct , Duration , IResource , Resource , Stack } from '@aws-cdk/core' ;
8
- import { InstanceDrainHook } from './drain-hook/instance-drain-hook' ;
9
- import { CfnCluster } from './ecs.generated' ;
7
+ import { Construct , Duration , IResource , Resource , Stack } from '@aws-cdk/core' ;
8
+ import { InstanceDrainHook } from './drain-hook/instance-drain-hook' ;
9
+ import { CfnCluster } from './ecs.generated' ;
10
10
11
11
/**
12
12
* The properties used to define an ECS cluster.
@@ -195,6 +195,10 @@ export class Cluster extends Resource implements ICluster {
195
195
autoScalingGroup . addUserData ( 'echo ECS_AWSVPC_BLOCK_IMDS=true >> /etc/ecs/ecs.config' ) ;
196
196
}
197
197
198
+ if ( autoScalingGroup . spotPrice && options . spotInstanceDraining ) {
199
+ autoScalingGroup . addUserData ( 'echo ECS_ENABLE_SPOT_INSTANCE_DRAINING=true >> /etc/ecs/ecs.config' ) ;
200
+ }
201
+
198
202
// ECS instances must be able to do these things
199
203
// Source: https://docs.aws.amazon.com/AmazonECS/latest/developerguide/instance_IAM_role.html
200
204
autoScalingGroup . addToRolePolicy ( new iam . PolicyStatement ( {
@@ -252,7 +256,7 @@ export class Cluster extends Resource implements ICluster {
252
256
* @default average over 5 minutes
253
257
*/
254
258
public metricMemoryReservation ( props ?: cloudwatch . MetricOptions ) : cloudwatch . Metric {
255
- return this . metric ( 'MemoryReservation' , props ) ;
259
+ return this . metric ( 'MemoryReservation' , props ) ;
256
260
}
257
261
258
262
/**
@@ -350,12 +354,12 @@ export class EcsOptimizedAmi implements ec2.IMachineImage {
350
354
351
355
// set the SSM parameter name
352
356
this . amiParameterName = "/aws/service/ecs/optimized-ami/"
353
- + ( this . generation === ec2 . AmazonLinuxGeneration . AMAZON_LINUX ? "amazon-linux/" : "" )
354
- + ( this . generation === ec2 . AmazonLinuxGeneration . AMAZON_LINUX_2 ? "amazon-linux-2/" : "" )
355
- + ( this . windowsVersion ? `windows_server/${ this . windowsVersion } /english/full/` : "" )
356
- + ( this . hwType === AmiHardwareType . GPU ? "gpu/" : "" )
357
- + ( this . hwType === AmiHardwareType . ARM ? "arm64/" : "" )
358
- + "recommended/image_id" ;
357
+ + ( this . generation === ec2 . AmazonLinuxGeneration . AMAZON_LINUX ? "amazon-linux/" : "" )
358
+ + ( this . generation === ec2 . AmazonLinuxGeneration . AMAZON_LINUX_2 ? "amazon-linux-2/" : "" )
359
+ + ( this . windowsVersion ? `windows_server/${ this . windowsVersion } /english/full/` : "" )
360
+ + ( this . hwType === AmiHardwareType . GPU ? "gpu/" : "" )
361
+ + ( this . hwType === AmiHardwareType . ARM ? "arm64/" : "" )
362
+ + "recommended/image_id" ;
359
363
}
360
364
361
365
/**
@@ -380,14 +384,14 @@ export class EcsOptimizedImage implements ec2.IMachineImage {
380
384
* @param hardwareType ECS-optimized AMI variant to use
381
385
*/
382
386
public static amazonLinux2 ( hardwareType = AmiHardwareType . STANDARD ) : EcsOptimizedImage {
383
- return new EcsOptimizedImage ( { generation : ec2 . AmazonLinuxGeneration . AMAZON_LINUX_2 , hardwareType} ) ;
387
+ return new EcsOptimizedImage ( { generation : ec2 . AmazonLinuxGeneration . AMAZON_LINUX_2 , hardwareType } ) ;
384
388
}
385
389
386
390
/**
387
391
* Construct an Amazon Linux AMI image from the latest ECS Optimized AMI published in SSM
388
392
*/
389
393
public static amazonLinux ( ) : EcsOptimizedImage {
390
- return new EcsOptimizedImage ( { generation : ec2 . AmazonLinuxGeneration . AMAZON_LINUX } ) ;
394
+ return new EcsOptimizedImage ( { generation : ec2 . AmazonLinuxGeneration . AMAZON_LINUX } ) ;
391
395
}
392
396
393
397
/**
@@ -396,7 +400,7 @@ export class EcsOptimizedImage implements ec2.IMachineImage {
396
400
* @param windowsVersion Windows Version to use
397
401
*/
398
402
public static windows ( windowsVersion : WindowsOptimizedVersion ) : EcsOptimizedImage {
399
- return new EcsOptimizedImage ( { windowsVersion} ) ;
403
+ return new EcsOptimizedImage ( { windowsVersion } ) ;
400
404
}
401
405
402
406
private readonly generation ?: ec2 . AmazonLinuxGeneration ;
@@ -421,12 +425,12 @@ export class EcsOptimizedImage implements ec2.IMachineImage {
421
425
422
426
// set the SSM parameter name
423
427
this . amiParameterName = "/aws/service/ecs/optimized-ami/"
424
- + ( this . generation === ec2 . AmazonLinuxGeneration . AMAZON_LINUX ? "amazon-linux/" : "" )
425
- + ( this . generation === ec2 . AmazonLinuxGeneration . AMAZON_LINUX_2 ? "amazon-linux-2/" : "" )
426
- + ( this . windowsVersion ? `windows_server/${ this . windowsVersion } /english/full/` : "" )
427
- + ( this . hwType === AmiHardwareType . GPU ? "gpu/" : "" )
428
- + ( this . hwType === AmiHardwareType . ARM ? "arm64/" : "" )
429
- + "recommended/image_id" ;
428
+ + ( this . generation === ec2 . AmazonLinuxGeneration . AMAZON_LINUX ? "amazon-linux/" : "" )
429
+ + ( this . generation === ec2 . AmazonLinuxGeneration . AMAZON_LINUX_2 ? "amazon-linux-2/" : "" )
430
+ + ( this . windowsVersion ? `windows_server/${ this . windowsVersion } /english/full/` : "" )
431
+ + ( this . hwType === AmiHardwareType . GPU ? "gpu/" : "" )
432
+ + ( this . hwType === AmiHardwareType . ARM ? "arm64/" : "" )
433
+ + "recommended/image_id" ;
430
434
}
431
435
432
436
/**
@@ -614,6 +618,14 @@ export interface AddAutoScalingGroupCapacityOptions {
614
618
* @default Duration.minutes(5)
615
619
*/
616
620
readonly taskDrainTime ?: Duration ;
621
+
622
+ /**
623
+ * Specify whether to enable Automated Draining for Spot Instances running Amazon ECS Services.
624
+ * For more information, see [Using Spot Instances](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/container-instance-spot.html).
625
+ *
626
+ * @default false
627
+ */
628
+ readonly spotInstanceDraining ?: boolean
617
629
}
618
630
619
631
/**
0 commit comments