Skip to content

Commit 1947d7c

Browse files
authored
feat(ecs): expose image name in container definition (#17793)
When adding a container to a task definition it is currently impossible to retrieve its image name because it's the container definition that is responsible for binding the `AssetImage`. Expose the `imageName` so that it's possible to reference it in another construct that needs to use/depend on the asset hash. Example: a custom resource running a Fargate task that needs to rerun if the underlying container image changes but not necessarily when the task definition changes. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent c46acd5 commit 1947d7c

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

Diff for: packages/@aws-cdk/aws-ecs/lib/container-definition.ts

+7
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,11 @@ export class ContainerDefinition extends CoreConstruct {
408408
*/
409409
public readonly referencesSecretJsonField?: boolean;
410410

411+
/**
412+
* The name of the image referenced by this container.
413+
*/
414+
public readonly imageName: string;
415+
411416
/**
412417
* The inference accelerators referenced by this container.
413418
*/
@@ -441,6 +446,8 @@ export class ContainerDefinition extends CoreConstruct {
441446
this.containerName = props.containerName ?? this.node.id;
442447

443448
this.imageConfig = props.image.bind(this, this);
449+
this.imageName = this.imageConfig.imageName;
450+
444451
if (props.logging) {
445452
this.logDriverConfig = props.logging.bind(this, this);
446453
}

Diff for: packages/@aws-cdk/aws-ecs/test/container-definition.test.ts

+26
Original file line numberDiff line numberDiff line change
@@ -2005,4 +2005,30 @@ describe('container definition', () => {
20052005
});
20062006

20072007
});
2008+
2009+
test('exposes image name', () => {
2010+
// GIVEN
2011+
const stack = new cdk.Stack();
2012+
const taskDefinition = new ecs.FargateTaskDefinition(stack, 'TaskDef');
2013+
2014+
// WHEN
2015+
const container = taskDefinition.addContainer('cont', {
2016+
image: ecs.ContainerImage.fromAsset(path.join(__dirname, 'demo-image')),
2017+
});
2018+
2019+
// THEN
2020+
expect(stack.resolve(container.imageName)).toEqual({
2021+
'Fn::Join': [
2022+
'',
2023+
[
2024+
{ Ref: 'AWS::AccountId' },
2025+
'.dkr.ecr.',
2026+
{ Ref: 'AWS::Region' },
2027+
'.',
2028+
{ Ref: 'AWS::URLSuffix' },
2029+
'/aws-cdk/assets:baa2d6eb2a17c75424df631c8c70ff39f2d5f3bee8b9e1a109ee24ca17300540',
2030+
],
2031+
],
2032+
});
2033+
});
20082034
});

0 commit comments

Comments
 (0)