Skip to content

Commit

Permalink
feat(ecs): expose image name in container definition (#17793)
Browse files Browse the repository at this point in the history
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*
  • Loading branch information
jogold authored Feb 1, 2022
1 parent c46acd5 commit 1947d7c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
7 changes: 7 additions & 0 deletions packages/@aws-cdk/aws-ecs/lib/container-definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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);
}
Expand Down
26 changes: 26 additions & 0 deletions packages/@aws-cdk/aws-ecs/test/container-definition.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
],
],
});
});
});

0 comments on commit 1947d7c

Please sign in to comment.