Class TagParameterContainerImage

All Implemented Interfaces:

@Generated(value="jsii-pacmak/1.84.0 (build 5404dcf)", date="2023-06-19T16:30:47.478Z") @Stability(Stable) public class TagParameterContainerImage extends ContainerImage
A special type of ContainerImage that uses an ECR repository for the image, but a CloudFormation Parameter for the tag of the image in that repository.

This allows providing this tag through the Parameter at deploy time, for example in a CodePipeline that pushes a new tag of the image to the repository during a build step, and then provides that new tag through the CloudFormation Parameter in the deploy step.


  * These are the construction properties for `EcsAppStack`.
  * They extend the standard Stack properties,
  * but also require providing the ContainerImage that the service will use.
  * That Image will be provided from the Stack containing the CodePipeline.
 public class EcsAppStackProps extends StackProps {
     private ContainerImage image;
     public ContainerImage getImage() {
         return this.image;
     public EcsAppStackProps image(ContainerImage image) {
         this.image = image;
         return this;
  * This is the Stack containing a simple ECS Service that uses the provided ContainerImage.
 public class EcsAppStack extends Stack {
     public EcsAppStack(Construct scope, String id, EcsAppStackProps props) {
         super(scope, id, props);
         TaskDefinition taskDefinition = TaskDefinition.Builder.create(this, "TaskDefinition")
         taskDefinition.addContainer("AppContainer", ContainerDefinitionOptions.builder()
         FargateService.Builder.create(this, "EcsService")
                 .cluster(Cluster.Builder.create(this, "Cluster")
                         .vpc(Vpc.Builder.create(this, "Vpc")
  * This is the Stack containing the CodePipeline definition that deploys an ECS Service.
 public class PipelineStack extends Stack {
     public final TagParameterContainerImage tagParameterContainerImage;
     public PipelineStack(Construct scope, String id) {
         this(scope, id, null);
     public PipelineStack(Construct scope, String id, StackProps props) {
         super(scope, id, props);
         /* ********** ECS part **************** */
         // this is the ECR repository where the built Docker image will be pushed
         Repository appEcrRepo = new Repository(this, "EcsDeployRepository");
         // the build that creates the Docker image, and pushes it to the ECR repo
         PipelineProject appCodeDockerBuild = PipelineProject.Builder.create(this, "AppCodeDockerImageBuildAndPushProject")
                         // we need to run Docker
                         "version", "0.2",
                         "phases", Map.of(
                                 "build", Map.of(
                                         "commands", List.of("$(aws ecr get-login --region $AWS_DEFAULT_REGION --no-include-email)", "docker build -t $REPOSITORY_URI:$CODEBUILD_RESOLVED_SOURCE_VERSION .")),
                                 "post_build", Map.of(
                                         "commands", List.of("docker push $REPOSITORY_URI:$CODEBUILD_RESOLVED_SOURCE_VERSION", "export imageTag=$CODEBUILD_RESOLVED_SOURCE_VERSION"))),
                         "env", Map.of(
                                 // save the imageTag environment variable as a CodePipeline Variable
                                 "exported-variables", List.of("imageTag")))))
                         "REPOSITORY_URI", BuildEnvironmentVariable.builder()
         // needed for `docker push`
         // create the ContainerImage used for the ECS application Stack
         this.tagParameterContainerImage = new TagParameterContainerImage(appEcrRepo);
         PipelineProject cdkCodeBuild = PipelineProject.Builder.create(this, "CdkCodeBuildProject")
                         "version", "0.2",
                         "phases", Map.of(
                                 "install", Map.of(
                                         "commands", List.of("npm install")),
                                 "build", Map.of(
                                         "commands", List.of("npx cdk synth --verbose"))),
                         "artifacts", Map.of(
                                 // store the entire Cloud Assembly as the output artifact
                                 "base-directory", "cdk.out",
                                 "files", "**/*"))))
         /* ********** Pipeline part **************** */
         Artifact appCodeSourceOutput = new Artifact();
         Artifact cdkCodeSourceOutput = new Artifact();
         Artifact cdkCodeBuildOutput = new Artifact();
         CodeBuildAction appCodeBuildAction = CodeBuildAction.Builder.create()
         Pipeline.Builder.create(this, "CodePipelineDeployingEcsApplication")
                 .artifactBucket(Bucket.Builder.create(this, "ArtifactBucket")
                             // this is the Action that takes the source of your application code
                                     .repository(Repository.Builder.create(this, "AppCodeSourceRepository").repositoryName("AppCodeSourceRepository").build())
                             // this is the Action that takes the source of your CDK code
                             // (which would probably include this Pipeline code as well)
                                     .repository(Repository.Builder.create(this, "CdkCodeSourceRepository").repositoryName("CdkCodeSourceRepository").build())
                         .build(), StageProps.builder()
                         .build(), StageProps.builder()
                                     // this name has to be the same name as used below in the CDK code for the application Stack
                                             // read the tag pushed to the ECR repository from the CodePipeline Variable saved by the application build step,
                                             // and pass it as the CloudFormation Parameter for the tag
                                             this.tagParameterContainerImage.getTagParameterName(), appCodeBuildAction.variable("imageTag")))
 App app = new App();
 // the CodePipeline Stack needs to be created first
 PipelineStack pipelineStack = new PipelineStack(app, "aws-cdk-pipeline-ecs-separate-sources");
 // we supply the image to the ECS application Stack from the CodePipeline Stack
 // we supply the image to the ECS application Stack from the CodePipeline Stack
 new EcsAppStack(app, "EcsStackDeployedInPipeline", new EcsAppStackProps()

See Also:
  • Constructor Details

    • TagParameterContainerImage

      protected TagParameterContainerImage( objRef)
    • TagParameterContainerImage

      protected TagParameterContainerImage( initializationMode)
    • TagParameterContainerImage

      @Stability(Stable) public TagParameterContainerImage(@NotNull IRepository repository)
      repository - This parameter is required.
  • Method Details

    • bind

      @Stability(Stable) @NotNull public ContainerImageConfig bind(@NotNull Construct scope, @NotNull ContainerDefinition containerDefinition)
      Called when the image is used by a ContainerDefinition.

      Specified by:
      bind in class ContainerImage
      scope - This parameter is required.
      containerDefinition - This parameter is required.
    • getTagParameterName

      @Stability(Stable) @NotNull public String getTagParameterName()
      Returns the name of the CloudFormation Parameter that represents the tag of the image in the ECR repository.
    • getTagParameterValue

      @Stability(Stable) @NotNull public String getTagParameterValue()
      Returns the value of the CloudFormation Parameter that represents the tag of the image in the ECR repository.