From c10c552cc4e2a3e764588d029e1f4666e9b92fa3 Mon Sep 17 00:00:00 2001 From: alastair-watts-avrios <54026832+alastair-watts-avrios@users.noreply.github.com> Date: Mon, 8 Mar 2021 19:14:35 +0100 Subject: [PATCH] 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* --- packages/@aws-cdk/aws-ecs/README.md | 2 ++ .../lib/images/tag-parameter-container-image.ts | 16 ++++++++++++++++ .../images/tag-parameter-container-image.test.ts | 16 ++++++++++++++++ 3 files changed, 34 insertions(+) diff --git a/packages/@aws-cdk/aws-ecs/README.md b/packages/@aws-cdk/aws-ecs/README.md index a038cf65fbaf9..8ac684943e244 100644 --- a/packages/@aws-cdk/aws-ecs/README.md +++ b/packages/@aws-cdk/aws-ecs/README.md @@ -299,6 +299,8 @@ obtained from either DockerHub or from ECR repositories, or built directly from image directly from a `Dockerfile` in your source directory. - `ecs.ContainerImage.fromDockerImageAsset(asset)`: uses an existing `@aws-cdk/aws-ecr-assets.DockerImageAsset` as a container image. +- `new ecs.TagParameterContainerImage(repository)`: use the given ECR repository as the image + but a CloudFormation parameter as the tag. ### Environment variables diff --git a/packages/@aws-cdk/aws-ecs/lib/images/tag-parameter-container-image.ts b/packages/@aws-cdk/aws-ecs/lib/images/tag-parameter-container-image.ts index b033e8783a514..c6479f44b951b 100644 --- a/packages/@aws-cdk/aws-ecs/lib/images/tag-parameter-container-image.ts +++ b/packages/@aws-cdk/aws-ecs/lib/images/tag-parameter-container-image.ts @@ -49,4 +49,20 @@ export class TagParameterContainerImage extends ContainerImage { }, }); } + + /** + * Returns the value of the CloudFormation Parameter that represents the tag of the image + * in the ECR repository. + */ + public get tagParameterValue(): string { + return cdk.Lazy.string({ + produce: () => { + if (this.imageTagParameter) { + return this.imageTagParameter.valueAsString; + } else { + throw new Error('TagParameterContainerImage must be used in a container definition when using tagParameterValue'); + } + }, + }); + } } diff --git a/packages/@aws-cdk/aws-ecs/test/images/tag-parameter-container-image.test.ts b/packages/@aws-cdk/aws-ecs/test/images/tag-parameter-container-image.test.ts index d0fe101252e26..d74ecb5335ac3 100644 --- a/packages/@aws-cdk/aws-ecs/test/images/tag-parameter-container-image.test.ts +++ b/packages/@aws-cdk/aws-ecs/test/images/tag-parameter-container-image.test.ts @@ -21,5 +21,21 @@ nodeunitShim({ test.done(); }, + + 'throws an error when tagParameterValue() is used without binding the image'(test: Test) { + // GIVEN + const stack = new cdk.Stack(); + const repository = new ecr.Repository(stack, 'Repository'); + const tagParameterContainerImage = new ecs.TagParameterContainerImage(repository); + new cdk.CfnOutput(stack, 'Output', { + value: tagParameterContainerImage.tagParameterValue, + }); + + test.throws(() => { + SynthUtils.synthesize(stack); + }, /TagParameterContainerImage must be used in a container definition when using tagParameterValue/); + + test.done(); + }, }, });