Skip to content

❗️NOTICE: ECR: Template error: Cannot use Fn::ImportValue in Conditions. #32238

@Joe-Zer0

Description

@Joe-Zer0

Please add your +1 👍 to let us know you have encountered this

Status: RESOLVED

Overview:

When retrieving an image with a tag equal to the environment name, which is stored as a CFN Export.

aws_ecs.ContainerImage.from_ecr_repository(
    repository=aws_ecr.Repository.from_repository_arn(
        self, "ECRRepo", repository_arn="arn:aws:ecr:us-east-1:12345:repository/my-repo"
    ),
    tag=Fn.import_value("Environment")
)

Resultant CFN YAML
aws-cdk-lib==2.166.0

  TaskDefinitionB36D86D9:
    Type: AWS::ECS::TaskDefinition
    Properties:
      ContainerDefinitions:
        - Essential: true
          Image:
            Fn::Join:
              - ""
              - - 12345.dkr.ecr.us-east-1.
                - Ref: AWS::URLSuffix
                - "/my-repo:"
                - Fn::ImportValue: Environment
          Name: Container

aws-cdk-lib==2.167.0

  TaskDefinitionB36D86D9:
    Type: AWS::ECS::TaskDefinition
    Properties:
      ContainerDefinitions:
        - Essential: true
          Image:
            Fn::Join:
              - ""
              - - 12345.dkr.ecr.us-east-1.
                - Ref: AWS::URLSuffix
                - /my-repo
                - Fn::If:
                    - ECRRepoIsInputDigest1074C652E
                    - Fn::Join:
                        - ""
                        - - "@"
                          - Fn::ImportValue: Environment
                    - Fn::Join:
                        - ""
                        - - ":"
                          - Fn::ImportValue: Environment
          Name: Container

In 2.167.0 there exists Fn::ImportValue is now in an Fn::If Condition. Which gives the error "Template error: Cannot use Fn::ImportValue in Conditions." when deploying.

Complete Error Message:

Template error: Cannot use Fn::ImportValue in Conditions.

Workaround:

Pin aws-cdk-lib to "2.166.0".

Solution:

Describe the bug

This works in 2.166.0, but does not work in 2.167.0. I believe it is due to this commit.

We are retrieving an image with a tag equal to the environment name, which is stored as a CFN Export.

aws_ecs.ContainerImage.from_ecr_repository(
    repository=aws_ecr.Repository.from_repository_arn(
        self, "ECRRepo", repository_arn="arn:aws:ecr:us-east-1:12345:repository/my-repo"
    ),
    tag=Fn.import_value("Environment")
)

Resultant CFN YAML
aws-cdk-lib==2.166.0

  TaskDefinitionB36D86D9:
    Type: AWS::ECS::TaskDefinition
    Properties:
      ContainerDefinitions:
        - Essential: true
          Image:
            Fn::Join:
              - ""
              - - 12345.dkr.ecr.us-east-1.
                - Ref: AWS::URLSuffix
                - "/my-repo:"
                - Fn::ImportValue: Environment
          Name: Container

aws-cdk-lib==2.167.0

  TaskDefinitionB36D86D9:
    Type: AWS::ECS::TaskDefinition
    Properties:
      ContainerDefinitions:
        - Essential: true
          Image:
            Fn::Join:
              - ""
              - - 12345.dkr.ecr.us-east-1.
                - Ref: AWS::URLSuffix
                - /my-repo
                - Fn::If:
                    - ECRRepoIsInputDigest1074C652E
                    - Fn::Join:
                        - ""
                        - - "@"
                          - Fn::ImportValue: Environment
                    - Fn::Join:
                        - ""
                        - - ":"
                          - Fn::ImportValue: Environment
          Name: Container

In 2.167.0 you can see that the Fn::ImportValue is now in an Fn::If Condition. Which gives the error "Template error: Cannot use Fn::ImportValue in Conditions." when deploying.

Regression Issue

  • Select this option if this issue appears to be a regression.

Last Known Working CDK Version

2.166.0

Expected Behavior

Resultant CFN will deploy to AWS.

Current Behavior

Resultant CFN gives the error "Template error: Cannot use Fn::ImportValue in Conditions." when deploying to AWS.

Reproduction Steps

Run cdk synth with the following files with aws-cdk-lib==2.167.0.

cdk.json

{
  "app": "python app.py"
}

app.py

from aws_cdk import (
    App,
    Environment,
    Fn,
    Stack,
    aws_ecr,
    aws_ecs
)
from constructs import Construct
import os

class AwsCdkStack(Stack):
    def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
        super().__init__(scope, construct_id, **kwargs)

        task_definition = aws_ecs.FargateTaskDefinition(
            self,
            'TaskDefinition'
        )
        task_definition.add_container(
            'Container',
            image = aws_ecs.ContainerImage.from_ecr_repository(
                repository=aws_ecr.Repository.from_repository_arn(
                    self, "ECRRepo", repository_arn="arn:aws:ecr:us-east-1:12345:repository/my-repo"
                ),
                tag=Fn.import_value("Environment")
            )
        )

app = App()
AwsCdkStack(
    app,
    'my-stack',
    env = Environment(account = os.environ.get('CDK_DEFAULT_ACCOUNT'), region = os.environ.get('CDK_DEFAULT_REGION'))
)
app.synth()

Possible Solution

No response

Additional Information/Context

No response

CDK CLI Version

2.167.0

Framework Version

No response

Node.js Version

v20.17.0

OS

Windows 10

Language

Python

Language Version

No response

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    @aws-cdk/aws-ecrRelated to Amazon Elastic Container RegistrybugThis issue is a bug.effort/mediumMedium work item – several days of effortmanagement/trackingIssues that track a subject or multiple issuesp0potential-regressionMarking this issue as a potential regression to be checked by team member

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions