diff --git a/packages/@aws-cdk/aws-ecs/lib/container-definition.ts b/packages/@aws-cdk/aws-ecs/lib/container-definition.ts index e7a6d72b8ceb7..b46e66df585fa 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. */ @@ -441,6 +446,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 a433e0049d83e..6eece8eabfa41 100644 --- a/packages/@aws-cdk/aws-ecs/test/container-definition.test.ts +++ b/packages/@aws-cdk/aws-ecs/test/container-definition.test.ts @@ -2005,4 +2005,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', + ], + ], + }); + }); });