-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
ecs-container-definition.ts
1070 lines (943 loc) · 30 KB
/
ecs-container-definition.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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import * as ecs from 'aws-cdk-lib/aws-ecs';
import { IFileSystem } from 'aws-cdk-lib/aws-efs';
import * as iam from 'aws-cdk-lib/aws-iam';
import * as ssm from 'aws-cdk-lib/aws-ssm';
import * as secretsmanager from 'aws-cdk-lib/aws-secretsmanager';
import { Lazy, PhysicalName, Size } from 'aws-cdk-lib/core';
import { Construct, IConstruct } from 'constructs';
import { CfnJobDefinition } from 'aws-cdk-lib/aws-batch';
import { LinuxParameters } from './linux-parameters';
import { LogGroup } from 'aws-cdk-lib/aws-logs';
const EFS_VOLUME_SYMBOL = Symbol.for('aws-cdk-lib/aws-batch/lib/container-definition.EfsVolume');
const HOST_VOLUME_SYMBOL = Symbol.for('aws-cdk-lib/aws-batch/lib/container-definition.HostVolume');
/**
* Specify the secret's version id or version stage
*/
export interface SecretVersionInfo {
/**
* version id of the secret
*
* @default - use default version id
*/
readonly versionId?: string;
/**
* version stage of the secret
*
* @default - use default version stage
*/
readonly versionStage?: string;
}
/**
* A secret environment variable.
*/
export abstract class Secret {
/**
* Creates an environment variable value from a parameter stored in AWS
* Systems Manager Parameter Store.
*/
public static fromSsmParameter(parameter: ssm.IParameter): Secret {
return {
arn: parameter.parameterArn,
grantRead: grantee => parameter.grantRead(grantee),
};
}
/**
* Creates a environment variable value from a secret stored in AWS Secrets
* Manager.
*
* @param secret the secret stored in AWS Secrets Manager
* @param field the name of the field with the value that you want to set as
* the environment variable value. Only values in JSON format are supported.
* If you do not specify a JSON field, then the full content of the secret is
* used.
*/
public static fromSecretsManager(secret: secretsmanager.ISecret, field?: string): Secret {
return {
arn: field ? `${secret.secretArn}:${field}::` : secret.secretArn,
hasField: !!field,
grantRead: grantee => secret.grantRead(grantee),
};
}
/**
* Creates a environment variable value from a secret stored in AWS Secrets
* Manager.
*
* @param secret the secret stored in AWS Secrets Manager
* @param versionInfo the version information to reference the secret
* @param field the name of the field with the value that you want to set as
* the environment variable value. Only values in JSON format are supported.
* If you do not specify a JSON field, then the full content of the secret is
* used.
*/
public static fromSecretsManagerVersion(secret: secretsmanager.ISecret, versionInfo: SecretVersionInfo, field?: string): Secret {
return {
arn: `${secret.secretArn}:${field ?? ''}:${versionInfo.versionStage ?? ''}:${versionInfo.versionId ?? ''}`,
hasField: !!field,
grantRead: grantee => secret.grantRead(grantee),
};
}
/**
* The ARN of the secret
*/
public abstract readonly arn: string;
/**
* Whether this secret uses a specific JSON field
*/
public abstract readonly hasField?: boolean;
/**
* Grants reading the secret to a principal
*/
public abstract grantRead(grantee: iam.IGrantable): iam.Grant;
}
/**
* Options to configure an EcsVolume
*/
export interface EcsVolumeOptions {
/**
* the name of this volume
*/
readonly name: string;
/**
* the path on the container where this volume is mounted
*/
readonly containerPath: string;
/**
* if set, the container will have readonly access to the volume
*
* @default false
*/
readonly readonly?: boolean;
}
/**
* Represents a Volume that can be mounted to a container that uses ECS
*/
export abstract class EcsVolume {
/**
* Creates a Volume that uses an AWS Elastic File System (EFS); this volume can grow and shrink as needed
*
* @see https://docs.aws.amazon.com/batch/latest/userguide/efs-volumes.html
*/
static efs(options: EfsVolumeOptions) {
return new EfsVolume(options);
}
/**
* Creates a Host volume. This volume will persist on the host at the specified `hostPath`.
* If the `hostPath` is not specified, Docker will choose the host path. In this case,
* the data may not persist after the containers that use it stop running.
*/
static host(options: HostVolumeOptions) {
return new HostVolume(options);
}
/**
* The name of this volume
*/
public readonly name: string;
/**
* The path on the container that this volume will be mounted to
*/
public readonly containerPath: string;
/**
* Whether or not the container has readonly access to this volume
*
* @default false
*/
public readonly readonly?: boolean;
constructor(options: EcsVolumeOptions) {
this.name = options.name;
this.containerPath = options.containerPath;
this.readonly = options.readonly;
}
}
/**
* Options for configuring an EfsVolume
*/
export interface EfsVolumeOptions extends EcsVolumeOptions {
/**
* The EFS File System that supports this volume
*/
readonly fileSystem: IFileSystem;
/**
* The directory within the Amazon EFS file system to mount as the root directory inside the host.
* If this parameter is omitted, the root of the Amazon EFS volume is used instead.
* Specifying `/` has the same effect as omitting this parameter.
* The maximum length is 4,096 characters.
*
* @default - root of the EFS File System
*/
readonly rootDirectory?: string;
/**
* Enables encryption for Amazon EFS data in transit between the Amazon ECS host and the Amazon EFS server
*
* @see https://docs.aws.amazon.com/efs/latest/ug/encryption-in-transit.html
*
* @default false
*/
readonly enableTransitEncryption?: boolean;
/**
* The port to use when sending encrypted data between the Amazon ECS host and the Amazon EFS server.
* The value must be between 0 and 65,535.
*
* @see https://docs.aws.amazon.com/efs/latest/ug/efs-mount-helper.html
*
* @default - chosen by the EFS Mount Helper
*/
readonly transitEncryptionPort?: number;
/**
* The Amazon EFS access point ID to use.
* If an access point is specified, `rootDirectory` must either be omitted or set to `/`
* which enforces the path set on the EFS access point.
* If an access point is used, `enableTransitEncryption` must be `true`.
*
* @see https://docs.aws.amazon.com/efs/latest/ug/efs-access-points.html
*
* @default - no accessPointId
*/
readonly accessPointId?: string;
/**
* Whether or not to use the AWS Batch job IAM role defined in a job definition when mounting the Amazon EFS file system.
* If specified, `enableTransitEncryption` must be `true`.
*
* @see https://docs.aws.amazon.com/batch/latest/userguide/efs-volumes.html#efs-volume-accesspoints
*
* @default false
*/
readonly useJobRole?: boolean;
}
/**
* A Volume that uses an AWS Elastic File System (EFS); this volume can grow and shrink as needed
*/
export class EfsVolume extends EcsVolume {
/**
* Returns true if x is an EfsVolume, false otherwise
*/
public static isEfsVolume(x: any) : x is EfsVolume {
return x !== null && typeof(x) === 'object' && EFS_VOLUME_SYMBOL in x;
}
/**
* The EFS File System that supports this volume
*/
public readonly fileSystem: IFileSystem;
/**
* The directory within the Amazon EFS file system to mount as the root directory inside the host.
* If this parameter is omitted, the root of the Amazon EFS volume is used instead.
* Specifying `/` has the same effect as omitting this parameter.
* The maximum length is 4,096 characters.
*
* @default - root of the EFS File System
*/
public readonly rootDirectory?: string;
/**
* Enables encryption for Amazon EFS data in transit between the Amazon ECS host and the Amazon EFS server
*
* @see https://docs.aws.amazon.com/efs/latest/ug/encryption-in-transit.html
*
* @default false
*/
public readonly enableTransitEncryption?: boolean;
/**
* The port to use when sending encrypted data between the Amazon ECS host and the Amazon EFS server.
* The value must be between 0 and 65,535.
*
* @see https://docs.aws.amazon.com/efs/latest/ug/efs-mount-helper.html
*
* @default - chosen by the EFS Mount Helper
*/
public readonly transitEncryptionPort?: number;
/**
* The Amazon EFS access point ID to use.
* If an access point is specified, `rootDirectory` must either be omitted or set to `/`
* which enforces the path set on the EFS access point.
* If an access point is used, `enableTransitEncryption` must be `true`.
*
* @see https://docs.aws.amazon.com/efs/latest/ug/efs-access-points.html
*
* @default - no accessPointId
*/
public readonly accessPointId?: string;
/**
* Whether or not to use the AWS Batch job IAM role defined in a job definition when mounting the Amazon EFS file system.
* If specified, `enableTransitEncryption` must be `true`.
*
* @see https://docs.aws.amazon.com/batch/latest/userguide/efs-volumes.html#efs-volume-accesspoints
*
* @default false
*/
public readonly useJobRole?: boolean;
constructor(options: EfsVolumeOptions) {
super(options);
this.fileSystem = options.fileSystem;
this.rootDirectory = options.rootDirectory;
this.enableTransitEncryption = options.enableTransitEncryption;
this.transitEncryptionPort = options.transitEncryptionPort;
this.accessPointId = options.accessPointId;
this.useJobRole = options.useJobRole;
}
}
Object.defineProperty(EfsVolume.prototype, EFS_VOLUME_SYMBOL, {
value: true,
enumerable: false,
writable: false,
});
/**
* Options for configuring an ECS HostVolume
*/
export interface HostVolumeOptions extends EcsVolumeOptions {
/**
* The path on the host machine this container will have access to
*
* @default - Docker will choose the host path.
* The data may not persist after the containers that use it stop running.
*/
readonly hostPath?: string;
}
/**
* Creates a Host volume. This volume will persist on the host at the specified `hostPath`.
* If the `hostPath` is not specified, Docker will choose the host path. In this case,
* the data may not persist after the containers that use it stop running.
*/
export class HostVolume extends EcsVolume {
/**
* returns `true` if `x` is a `HostVolume`, `false` otherwise
*/
public static isHostVolume(x: any): x is HostVolume {
return x !== null && typeof (x) === 'object' && HOST_VOLUME_SYMBOL in x;
}
/**
* The path on the host machine this container will have access to
*/
public readonly hostPath?: string;
constructor(options: HostVolumeOptions) {
super(options);
this.hostPath = options.hostPath;
}
}
Object.defineProperty(HostVolume.prototype, HOST_VOLUME_SYMBOL, {
value: true,
enumerable: false,
writable: false,
});
/**
* A container that can be run with ECS orchestration
*/
export interface IEcsContainerDefinition extends IConstruct {
/**
* The image that this container will run
*/
readonly image: ecs.ContainerImage;
/**
* The number of vCPUs reserved for the container.
* Each vCPU is equivalent to 1,024 CPU shares.
* For containers running on EC2 resources, you must specify at least one vCPU.
*/
readonly cpu: number;
/**
* The memory hard limit present to the container.
* If your container attempts to exceed the memory specified, the container is terminated.
* You must specify at least 4 MiB of memory for a job.
*/
readonly memory: Size;
/**
* The command that's passed to the container
*
* @see https://docs.docker.com/engine/reference/builder/#cmd
*/
readonly command?: string[];
/**
* The environment variables to pass to a container.
* Cannot start with `AWS_BATCH`.
* We don't recommend using plaintext environment variables for sensitive information, such as credential data.
*
* @default - no environment variables
*/
readonly environment?: { [key:string]: string };
/**
* The role used by Amazon ECS container and AWS Fargate agents to make AWS API calls on your behalf.
*
* @see https://docs.aws.amazon.com/batch/latest/userguide/execution-IAM-role.html
*/
readonly executionRole: iam.IRole;
/**
* The role that the container can assume.
*
* @default - no jobRole
*
* @see https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-iam-roles.html
*/
readonly jobRole?: iam.IRole;
/**
* Linux-specific modifications that are applied to the container, such as details for device mappings.
*
* @default none
*/
readonly linuxParameters?: LinuxParameters;
/**
* The configuration of the log driver
*/
readonly logDriverConfig?: ecs.LogDriverConfig;
/**
* Gives the container readonly access to its root filesystem.
*
* @default false
*/
readonly readonlyRootFilesystem?: boolean;
/**
* A map from environment variable names to the secrets for the container. Allows your job definitions
* to reference the secret by the environment variable name defined in this property.
*
* @see https://docs.aws.amazon.com/batch/latest/userguide/specifying-sensitive-data.html
*
* @default - no secrets
*/
readonly secrets?: { [envVarName: string]: Secret };
/**
* The user name to use inside the container
*
* @default - no user
*/
readonly user?: string;
/**
* The volumes to mount to this container. Automatically added to the job definition.
*
* @default - no volumes
*/
readonly volumes: EcsVolume[];
/**
* Renders this container to CloudFormation
*
* @internal
*/
_renderContainerDefinition(): CfnJobDefinition.ContainerPropertiesProperty;
/**
* Add a Volume to this container
*/
addVolume(volume: EcsVolume): void;
}
/**
* Props to configure an EcsContainerDefinition
*/
export interface EcsContainerDefinitionProps {
/**
* The image that this container will run
*/
readonly image: ecs.ContainerImage;
/**
* The number of vCPUs reserved for the container.
* Each vCPU is equivalent to 1,024 CPU shares.
* For containers running on EC2 resources, you must specify at least one vCPU.
*/
readonly cpu: number;
/**
* The memory hard limit present to the container.
* If your container attempts to exceed the memory specified, the container is terminated.
* You must specify at least 4 MiB of memory for a job.
*/
readonly memory: Size;
/**
* The command that's passed to the container
*
* @see https://docs.docker.com/engine/reference/builder/#cmd
*
* @default - no command
*/
readonly command?: string[];
/**
* The environment variables to pass to a container.
* Cannot start with `AWS_BATCH`.
* We don't recommend using plaintext environment variables for sensitive information, such as credential data.
*
* @default - no environment variables
*/
readonly environment?: { [key:string]: string };
/**
* The role used by Amazon ECS container and AWS Fargate agents to make AWS API calls on your behalf.
*
* @see https://docs.aws.amazon.com/batch/latest/userguide/execution-IAM-role.html
*
* @default - a Role will be created
*/
readonly executionRole?: iam.IRole;
/**
* The role that the container can assume.
*
* @see https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-iam-roles.html
*
* @default - no job role
*/
readonly jobRole?: iam.IRole;
/**
* Linux-specific modifications that are applied to the container, such as details for device mappings.
*
* @default none
*/
readonly linuxParameters?: LinuxParameters;
/**
* The loging configuration for this Job
*
* @default - the log configuration of the Docker daemon
*/
readonly logging?: ecs.LogDriver;
/**
* Gives the container readonly access to its root filesystem.
*
* @default false
*/
readonly readonlyRootFilesystem?: boolean;
/**
* A map from environment variable names to the secrets for the container. Allows your job definitions
* to reference the secret by the environment variable name defined in this property.
*
* @see https://docs.aws.amazon.com/batch/latest/userguide/specifying-sensitive-data.html
*
* @default - no secrets
*/
readonly secrets?: { [envVarName: string]: Secret };
/**
* The user name to use inside the container
*
* @default - no user
*/
readonly user?: string;
/**
* The volumes to mount to this container. Automatically added to the job definition.
*
* @default - no volumes
*/
readonly volumes?: EcsVolume[];
}
/**
* Abstract base class for ECS Containers
*/
abstract class EcsContainerDefinitionBase extends Construct implements IEcsContainerDefinition {
public readonly image: ecs.ContainerImage;
public readonly cpu: number;
public readonly memory: Size;
public readonly command?: string[];
public readonly environment?: { [key:string]: string };
public readonly executionRole: iam.IRole;
public readonly jobRole?: iam.IRole;
public readonly linuxParameters?: LinuxParameters;
public readonly logDriverConfig?: ecs.LogDriverConfig;
public readonly readonlyRootFilesystem?: boolean;
public readonly secrets?: { [envVarName: string]: Secret };
public readonly user?: string;
public readonly volumes: EcsVolume[];
private readonly imageConfig: ecs.ContainerImageConfig;
constructor(scope: Construct, id: string, props: EcsContainerDefinitionProps) {
super(scope, id);
this.image = props.image;
this.cpu = props.cpu;
this.command = props.command;
this.environment = props.environment;
this.executionRole = props.executionRole ?? createExecutionRole(this, 'ExecutionRole', props.logging ? true : false);
this.jobRole = props.jobRole;
this.linuxParameters = props.linuxParameters;
this.memory = props.memory;
if (props.logging) {
this.logDriverConfig = props.logging.bind(this, {
...this as any,
// TS!
taskDefinition: {
obtainExecutionRole: () => this.executionRole,
},
});
}
this.readonlyRootFilesystem = props.readonlyRootFilesystem ?? false;
this.secrets = props.secrets;
this.user = props.user;
this.volumes = props.volumes ?? [];
this.imageConfig = props.image.bind(this, {
...this as any,
taskDefinition: {
obtainExecutionRole: () => this.executionRole,
},
});
}
/**
* @internal
*/
public _renderContainerDefinition(): CfnJobDefinition.ContainerPropertiesProperty {
return {
image: this.imageConfig.imageName,
command: this.command,
environment: Object.keys(this.environment ?? {}).map((envKey) => ({
name: envKey,
value: (this.environment ?? {})[envKey],
})),
jobRoleArn: this.jobRole?.roleArn,
executionRoleArn: this.executionRole?.roleArn,
linuxParameters: this.linuxParameters && this.linuxParameters.renderLinuxParameters(),
logConfiguration: this.logDriverConfig,
readonlyRootFilesystem: this.readonlyRootFilesystem,
resourceRequirements: this._renderResourceRequirements(),
secrets: this.secrets ? Object.entries(this.secrets).map(([name, secret]) => {
secret.grantRead(this.executionRole);
return {
name,
valueFrom: secret.arn,
};
}) : undefined,
mountPoints: Lazy.any({
produce: () => {
if (this.volumes.length === 0) {
return undefined;
}
return this.volumes.map((volume) => {
return {
containerPath: volume.containerPath,
readOnly: volume.readonly,
sourceVolume: volume.name,
};
});
},
}),
volumes: Lazy.any({
produce: () => {
if (this.volumes.length === 0) {
return undefined;
}
return this.volumes.map((volume) => {
if (EfsVolume.isEfsVolume(volume)) {
return {
name: volume.name,
efsVolumeConfiguration: {
fileSystemId: volume.fileSystem.fileSystemId,
rootDirectory: volume.rootDirectory,
transitEncryption: volume.enableTransitEncryption ? 'ENABLED' : (volume.enableTransitEncryption === false ? 'DISABLED' : undefined),
transitEncryptionPort: volume.transitEncryptionPort,
authorizationConfig: volume.accessPointId || volume.useJobRole ? {
accessPointId: volume.accessPointId,
iam: volume.useJobRole ? 'ENABLED' : (volume.useJobRole === false ? 'DISABLED' : undefined),
} : undefined,
},
};
} else if (HostVolume.isHostVolume(volume)) {
return {
name: volume.name,
host: {
sourcePath: volume.hostPath,
},
};
}
throw new Error('unsupported Volume encountered');
});
},
}),
user: this.user,
};
}
public addVolume(volume: EcsVolume): void {
this.volumes.push(volume);
}
/**
* @internal
*/
protected _renderResourceRequirements() {
const resourceRequirements = [];
resourceRequirements.push({
type: 'MEMORY',
value: this.memory.toMebibytes().toString(),
});
resourceRequirements.push({
type: 'VCPU',
value: this.cpu.toString(),
});
return resourceRequirements;
}
}
/**
* Sets limits for a resource with `ulimit` on linux systems.
* Used by the Docker daemon.
*/
export interface Ulimit {
/**
* The hard limit for this resource. The container will
* be terminated if it exceeds this limit.
*/
readonly hardLimit: number;
/**
* The resource to limit
*/
readonly name: UlimitName;
/**
* The reservation for this resource. The container will
* not be terminated if it exceeds this limit.
*/
readonly softLimit: number;
}
/**
* The resources to be limited
*/
export enum UlimitName {
/**
* max core dump file size
*/
CORE = 'core',
/**
* max cpu time (seconds) for a process
*/
CPU = 'cpu',
/**
* max data segment size
*/
DATA = 'data',
/**
* max file size
*/
FSIZE = 'fsize',
/**
* max number of file locks
*/
LOCKS = 'locks',
/**
* max locked memory
*/
MEMLOCK = 'memlock',
/**
* max POSIX message queue size
*/
MSGQUEUE = 'msgqueue',
/**
* max nice value for any process this user is running
*/
NICE = 'nice',
/**
* maximum number of open file descriptors
*/
NOFILE = 'nofile',
/**
* maximum number of processes
*/
NPROC = 'nproc',
/**
* size of the process' resident set (in pages)
*/
RSS = 'rss',
/**
* max realtime priority
*/
RTPRIO = 'rtprio',
/**
* timeout for realtime tasks
*/
RTTIME = 'rttime',
/**
* max number of pending signals
*/
SIGPENDING = 'sigpending',
/**
* max stack size (in bytes)
*/
STACK = 'stack',
}
/**
* A container orchestrated by ECS that uses EC2 resources
*/
export interface IEcsEc2ContainerDefinition extends IEcsContainerDefinition {
/**
* When this parameter is true, the container is given elevated permissions on the host container instance (similar to the root user).
*
* @default false
*/
readonly privileged?: boolean;
/**
* Limits to set for the user this docker container will run as
*/
readonly ulimits: Ulimit[];
/**
* The number of physical GPUs to reserve for the container.
* Make sure that the number of GPUs reserved for all containers in a job doesn't exceed
* the number of available GPUs on the compute resource that the job is launched on.
*
* @default - no gpus
*/
readonly gpu?: number;
/**
* Add a ulimit to this container
*/
addUlimit(ulimit: Ulimit): void;
}
/**
* Props to configure an EcsEc2ContainerDefinition
*/
export interface EcsEc2ContainerDefinitionProps extends EcsContainerDefinitionProps {
/**
* When this parameter is true, the container is given elevated permissions on the host container instance (similar to the root user).
*
* @default false
*/
readonly privileged?: boolean;
/**
* Limits to set for the user this docker container will run as
*
* @default - no ulimits
*/
readonly ulimits?: Ulimit[];
/**
* The number of physical GPUs to reserve for the container.
* Make sure that the number of GPUs reserved for all containers in a job doesn't exceed
* the number of available GPUs on the compute resource that the job is launched on.
*
* @default - no gpus
*/
readonly gpu?: number;
}
/**
* A container orchestrated by ECS that uses EC2 resources
*/
export class EcsEc2ContainerDefinition extends EcsContainerDefinitionBase implements IEcsEc2ContainerDefinition {
public readonly privileged?: boolean;
public readonly ulimits: Ulimit[];
public readonly gpu?: number;
constructor(scope: Construct, id: string, props: EcsEc2ContainerDefinitionProps) {
super(scope, id, props);
this.privileged = props.privileged;
this.ulimits = props.ulimits ?? [];
this.gpu = props.gpu;
}
/**
* @internal
*/
public _renderContainerDefinition(): CfnJobDefinition.ContainerPropertiesProperty {
return {
...super._renderContainerDefinition(),
ulimits: Lazy.any({
produce: () => {
if (this.ulimits.length === 0) {
return undefined;
}
return this.ulimits.map((ulimit) => ({
hardLimit: ulimit.hardLimit,
name: ulimit.name,
softLimit: ulimit.softLimit,
}));
},
}),
privileged: this.privileged,
resourceRequirements: this._renderResourceRequirements(),
};
};
/**
* Add a ulimit to this container
*/
addUlimit(ulimit: Ulimit): void {
this.ulimits.push(ulimit);
}
/**
* @internal
*/
protected _renderResourceRequirements() {
const resourceRequirements = super._renderResourceRequirements();
if (this.gpu) {
resourceRequirements.push({
type: 'GPU',
value: this.gpu.toString(),
});
}
return resourceRequirements;
}
}
/**
* A container orchestrated by ECS that uses Fargate resources and is orchestrated by ECS
*/
export interface IEcsFargateContainerDefinition extends IEcsContainerDefinition {
/**
* Indicates whether the job has a public IP address.
* For a job that's running on Fargate resources in a private subnet to send outbound traffic to the internet
* (for example, to pull container images), the private subnet requires a NAT gateway be attached to route requests to the internet.
*
* @see https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-networking.html
*
* @default false
*/
readonly assignPublicIp?: boolean;
/**
* Which version of Fargate to use when running this container
*
* @default LATEST
*/
readonly fargatePlatformVersion?: ecs.FargatePlatformVersion;
/**
* The size for ephemeral storage.
*
* @default - 20 GiB
*/
readonly ephemeralStorageSize?: Size;
}
/**
* Props to configure an EcsFargateContainerDefinition
*/
export interface EcsFargateContainerDefinitionProps extends EcsContainerDefinitionProps {
/**
* Indicates whether the job has a public IP address.
* For a job that's running on Fargate resources in a private subnet to send outbound traffic to the internet
* (for example, to pull container images), the private subnet requires a NAT gateway be attached to route requests to the internet.
*
* @see https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-networking.html
*
* @default false
*/
readonly assignPublicIp?: boolean;
/**
* Which version of Fargate to use when running this container