@@ -2,7 +2,7 @@ import * as ecs from 'aws-cdk-lib/aws-ecs';
2
2
import { IFileSystem } from 'aws-cdk-lib/aws-efs' ;
3
3
import * as iam from 'aws-cdk-lib/aws-iam' ;
4
4
import * as secretsmanager from 'aws-cdk-lib/aws-secretsmanager' ;
5
- import { DefaultTokenResolver , Lazy , PhysicalName , Size , StringConcat , Tokenization } from 'aws-cdk-lib' ;
5
+ import { Lazy , PhysicalName , Size } from 'aws-cdk-lib' ;
6
6
import { Construct , IConstruct } from 'constructs' ;
7
7
import { CfnJobDefinition } from 'aws-cdk-lib/aws-batch' ;
8
8
import { LinuxParameters } from './linux-parameters' ;
@@ -237,6 +237,7 @@ export interface HostVolumeOptions extends EcsVolumeOptions {
237
237
*/
238
238
readonly hostPath ?: string ;
239
239
}
240
+
240
241
/**
241
242
* Creates a Host volume. This volume will persist on the host at the specified `hostPath`.
242
243
* If the `hostPath` is not specified, Docker will choose the host path. In this case,
@@ -306,6 +307,13 @@ export interface IEcsContainerDefinition extends IConstruct {
306
307
*/
307
308
readonly environment ?: { [ key :string ] : string } ;
308
309
310
+ /**
311
+ * The role used by Amazon ECS container and AWS Fargate agents to make AWS API calls on your behalf.
312
+ *
313
+ * @see https://docs.aws.amazon.com/batch/latest/userguide/execution-IAM-role.html
314
+ */
315
+ readonly executionRole : iam . IRole ;
316
+
309
317
/**
310
318
* The role that the container can assume.
311
319
*
@@ -411,6 +419,15 @@ export interface EcsContainerDefinitionProps {
411
419
*/
412
420
readonly environment ?: { [ key :string ] : string } ;
413
421
422
+ /**
423
+ * The role used by Amazon ECS container and AWS Fargate agents to make AWS API calls on your behalf.
424
+ *
425
+ * @see https://docs.aws.amazon.com/batch/latest/userguide/execution-IAM-role.html
426
+ *
427
+ * @default - a Role will be created
428
+ */
429
+ readonly executionRole ?: iam . IRole ;
430
+
414
431
/**
415
432
* The role that the container can assume.
416
433
*
@@ -474,6 +491,7 @@ abstract class EcsContainerDefinitionBase extends Construct implements IEcsConta
474
491
public readonly memory : Size ;
475
492
public readonly command ?: string [ ] ;
476
493
public readonly environment ?: { [ key :string ] : string } ;
494
+ public readonly executionRole : iam . IRole ;
477
495
public readonly jobRole ?: iam . IRole ;
478
496
public readonly linuxParameters ?: LinuxParameters ;
479
497
public readonly logDriverConfig ?: ecs . LogDriverConfig ;
@@ -482,8 +500,6 @@ abstract class EcsContainerDefinitionBase extends Construct implements IEcsConta
482
500
public readonly user ?: string ;
483
501
public readonly volumes : EcsVolume [ ] ;
484
502
485
- public abstract readonly executionRole ?: iam . IRole ;
486
-
487
503
private readonly imageConfig : ecs . ContainerImageConfig ;
488
504
489
505
constructor ( scope : Construct , id : string , props : EcsContainerDefinitionProps ) {
@@ -493,52 +509,40 @@ abstract class EcsContainerDefinitionBase extends Construct implements IEcsConta
493
509
this . cpu = props . cpu ;
494
510
this . command = props . command ;
495
511
this . environment = props . environment ;
512
+ this . executionRole = props . executionRole ?? createExecutionRole ( this , 'ExecutionRole' ) ;
496
513
this . jobRole = props . jobRole ;
497
514
this . linuxParameters = props . linuxParameters ;
498
515
this . memory = props . memory ;
499
516
500
- // Lazy so this.executionRole can be filled by subclasses
501
- this . logDriverConfig = Lazy . any ( {
502
- produce : ( ) => {
503
- if ( props . logging ) {
504
- return props . logging . bind ( this , {
505
- ...this as any ,
506
- // TS!
507
- taskDefinition : {
508
- obtainExecutionRole : ( ) => this . executionRole ,
509
- } ,
510
- } ) ;
511
- }
512
-
513
- return undefined ;
514
- } ,
515
- } ) as any ;
517
+ if ( props . logging ) {
518
+ this . logDriverConfig = props . logging . bind ( this , {
519
+ ...this as any ,
520
+ // TS!
521
+ taskDefinition : {
522
+ obtainExecutionRole : ( ) => this . executionRole ,
523
+ } ,
524
+ } ) ;
525
+ }
516
526
517
527
this . readonlyRootFilesystem = props . readonlyRootFilesystem ?? false ;
518
528
this . secrets = props . secrets ;
519
529
this . user = props . user ;
520
530
this . volumes = props . volumes ?? [ ] ;
521
531
522
- // Lazy so this.executionRole can be filled by subclasses
523
- this . imageConfig = Lazy . any ( {
524
- produce : ( ) => props . image . bind ( this , {
525
- ...this as any ,
526
- taskDefinition : {
527
- obtainExecutionRole : ( ) => this . executionRole ,
528
- } ,
529
- } ) ,
530
- } ) as any ;
532
+ this . imageConfig = props . image . bind ( this , {
533
+ ...this as any ,
534
+ taskDefinition : {
535
+ obtainExecutionRole : ( ) => this . executionRole ,
536
+ } ,
537
+ } ) ;
531
538
}
532
539
533
540
/**
534
541
* @internal
535
542
*/
536
543
public _renderContainerDefinition ( ) : CfnJobDefinition . ContainerPropertiesProperty {
537
544
return {
538
- image : Tokenization . resolve ( this . imageConfig , {
539
- scope : this ,
540
- resolver : new DefaultTokenResolver ( new StringConcat ( ) ) ,
541
- } ) . imageName ,
545
+ image : this . imageConfig . imageName ,
542
546
command : this . command ,
543
547
environment : Object . keys ( this . environment ?? { } ) . map ( ( envKey ) => ( {
544
548
name : envKey ,
@@ -792,15 +796,6 @@ export interface EcsEc2ContainerDefinitionProps extends EcsContainerDefinitionPr
792
796
* @default - no gpus
793
797
*/
794
798
readonly gpu ?: number ;
795
-
796
- /**
797
- * The role used by Amazon ECS container and AWS Fargate agents to make AWS API calls on your behalf.
798
- *
799
- * @see https://docs.aws.amazon.com/batch/latest/userguide/execution-IAM-role.html
800
- *
801
- * @default - a Role will be created if logging is specified, no role otherwise
802
- */
803
- readonly executionRole ?: iam . IRole ;
804
799
}
805
800
806
801
/**
@@ -811,21 +806,11 @@ export class EcsEc2ContainerDefinition extends EcsContainerDefinitionBase implem
811
806
public readonly ulimits : Ulimit [ ] ;
812
807
public readonly gpu ?: number ;
813
808
814
- /**
815
- * The role used by Amazon ECS container and AWS Fargate agents to make AWS API calls on your behalf.
816
- *
817
- * @see https://docs.aws.amazon.com/batch/latest/userguide/execution-IAM-role.html
818
- *
819
- * @default - a Role will be created if logging is specified, no role otherwise
820
- */
821
- public readonly executionRole ?: iam . IRole ;
822
-
823
809
constructor ( scope : Construct , id : string , props : EcsEc2ContainerDefinitionProps ) {
824
810
super ( scope , id , props ) ;
825
811
this . privileged = props . privileged ;
826
812
this . ulimits = props . ulimits ?? [ ] ;
827
813
this . gpu = props . gpu ;
828
- this . executionRole = props . executionRole ?? ( this . logDriverConfig ? createExecutionRole ( this , 'ExecutionRole' ) : undefined ) ;
829
814
}
830
815
831
816
/**
@@ -919,15 +904,6 @@ export interface EcsFargateContainerDefinitionProps extends EcsContainerDefiniti
919
904
* @default LATEST
920
905
*/
921
906
readonly fargatePlatformVersion ?: ecs . FargatePlatformVersion ;
922
-
923
- /**
924
- * The role used by Amazon ECS container and AWS Fargate agents to make AWS API calls on your behalf.
925
- *
926
- * @see https://docs.aws.amazon.com/batch/latest/userguide/execution-IAM-role.html
927
- *
928
- * @default - a Role will be created
929
- */
930
- readonly executionRole ?: iam . IRole ;
931
907
}
932
908
933
909
/**
@@ -937,20 +913,10 @@ export class EcsFargateContainerDefinition extends EcsContainerDefinitionBase im
937
913
public readonly fargatePlatformVersion ?: ecs . FargatePlatformVersion ;
938
914
public readonly assignPublicIp ?: boolean ;
939
915
940
- /**
941
- * The role used by Amazon ECS container and AWS Fargate agents to make AWS API calls on your behalf.
942
- *
943
- * @see https://docs.aws.amazon.com/batch/latest/userguide/execution-IAM-role.html
944
- *
945
- * @default - a Role will be created
946
- */
947
- public readonly executionRole : iam . IRole ;
948
-
949
916
constructor ( scope : Construct , id : string , props : EcsFargateContainerDefinitionProps ) {
950
917
super ( scope , id , props ) ;
951
918
this . assignPublicIp = props . assignPublicIp ;
952
919
this . fargatePlatformVersion = props . fargatePlatformVersion ;
953
- this . executionRole = props . executionRole ?? createExecutionRole ( this , 'ExecutionRole' ) ;
954
920
}
955
921
956
922
/**
0 commit comments