Skip to content

Commit

Permalink
feat(ecs): ability to access tag parameter value of TagParameterConta…
Browse files Browse the repository at this point in the history
…inerImage (#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*
  • Loading branch information
alastair-watts-avrios authored Mar 8, 2021
1 parent d9d58af commit e567a41
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/@aws-cdk/aws-ecs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
},
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
},
},
});

0 comments on commit e567a41

Please sign in to comment.