Skip to content

Commit e567a41

Browse files
feat(ecs): ability to access tag parameter value of TagParameterContainerImage (#13340)
Allows you to use the tag elsewhere within the container definition (e.g. to inform monitoring services of the release version). Fixes: #13202 ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent d9d58af commit e567a41

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

packages/@aws-cdk/aws-ecs/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,8 @@ obtained from either DockerHub or from ECR repositories, or built directly from
299299
image directly from a `Dockerfile` in your source directory.
300300
- `ecs.ContainerImage.fromDockerImageAsset(asset)`: uses an existing
301301
`@aws-cdk/aws-ecr-assets.DockerImageAsset` as a container image.
302+
- `new ecs.TagParameterContainerImage(repository)`: use the given ECR repository as the image
303+
but a CloudFormation parameter as the tag.
302304

303305
### Environment variables
304306

packages/@aws-cdk/aws-ecs/lib/images/tag-parameter-container-image.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,20 @@ export class TagParameterContainerImage extends ContainerImage {
4949
},
5050
});
5151
}
52+
53+
/**
54+
* Returns the value of the CloudFormation Parameter that represents the tag of the image
55+
* in the ECR repository.
56+
*/
57+
public get tagParameterValue(): string {
58+
return cdk.Lazy.string({
59+
produce: () => {
60+
if (this.imageTagParameter) {
61+
return this.imageTagParameter.valueAsString;
62+
} else {
63+
throw new Error('TagParameterContainerImage must be used in a container definition when using tagParameterValue');
64+
}
65+
},
66+
});
67+
}
5268
}

packages/@aws-cdk/aws-ecs/test/images/tag-parameter-container-image.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,21 @@ nodeunitShim({
2121

2222
test.done();
2323
},
24+
25+
'throws an error when tagParameterValue() is used without binding the image'(test: Test) {
26+
// GIVEN
27+
const stack = new cdk.Stack();
28+
const repository = new ecr.Repository(stack, 'Repository');
29+
const tagParameterContainerImage = new ecs.TagParameterContainerImage(repository);
30+
new cdk.CfnOutput(stack, 'Output', {
31+
value: tagParameterContainerImage.tagParameterValue,
32+
});
33+
34+
test.throws(() => {
35+
SynthUtils.synthesize(stack);
36+
}, /TagParameterContainerImage must be used in a container definition when using tagParameterValue/);
37+
38+
test.done();
39+
},
2440
},
2541
});

0 commit comments

Comments
 (0)