From 8f9e8b6105840d22a113076786e931908e8899bb Mon Sep 17 00:00:00 2001 From: Jonathan Goldwasser Date: Wed, 1 Dec 2021 12:02:33 +0100 Subject: [PATCH] chore(ecs): exposer container image name --- .../aws-ecs/lib/container-definition.ts | 7 +++++ .../aws-ecs/test/container-definition.test.ts | 26 +++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/packages/@aws-cdk/aws-ecs/lib/container-definition.ts b/packages/@aws-cdk/aws-ecs/lib/container-definition.ts index 625041468a0a4..b325254f750e9 100644 --- a/packages/@aws-cdk/aws-ecs/lib/container-definition.ts +++ b/packages/@aws-cdk/aws-ecs/lib/container-definition.ts @@ -408,6 +408,11 @@ export class ContainerDefinition extends CoreConstruct { */ public readonly referencesSecretJsonField?: boolean; + /** + * The name of the image referenced by this container. + */ + public readonly imageName: string; + /** * The inference accelerators referenced by this container. */ @@ -439,6 +444,8 @@ export class ContainerDefinition extends CoreConstruct { this.containerName = props.containerName ?? this.node.id; this.imageConfig = props.image.bind(this, this); + this.imageName = this.imageConfig.imageName; + if (props.logging) { this.logDriverConfig = props.logging.bind(this, this); } diff --git a/packages/@aws-cdk/aws-ecs/test/container-definition.test.ts b/packages/@aws-cdk/aws-ecs/test/container-definition.test.ts index a384294900342..edda249ef6110 100644 --- a/packages/@aws-cdk/aws-ecs/test/container-definition.test.ts +++ b/packages/@aws-cdk/aws-ecs/test/container-definition.test.ts @@ -2001,4 +2001,30 @@ describe('container definition', () => { }); }); + + test('exposes image name', () => { + // GIVEN + const stack = new cdk.Stack(); + const taskDefinition = new ecs.FargateTaskDefinition(stack, 'TaskDef'); + + // WHEN + const container = taskDefinition.addContainer('cont', { + image: ecs.ContainerImage.fromAsset(path.join(__dirname, 'demo-image')), + }); + + // THEN + expect(stack.resolve(container.imageName)).toEqual({ + 'Fn::Join': [ + '', + [ + { Ref: 'AWS::AccountId' }, + '.dkr.ecr.', + { Ref: 'AWS::Region' }, + '.', + { Ref: 'AWS::URLSuffix' }, + '/aws-cdk/assets:baa2d6eb2a17c75424df631c8c70ff39f2d5f3bee8b9e1a109ee24ca17300540', + ], + ], + }); + }); });