Skip to content

lambda.DockerImageCode.fromEcr: imageTagOrDigest field cannot recognize digests supplied as CfnParameter #31860

@chrislciaba

Description

@chrislciaba

Describe the bug

Since imageTagOrDigest supplied in the props for lambda.DockerImageCode.fromEcr can contain either an image tag or digest, if the value isn't explicitly known (in my example it's a CfnParameter) it defaults to it being a tag. There appears to be no workaround for this at the moment

Regression Issue

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

Last Known Working CDK Version

No response

Expected Behavior

The following cloudformation code should be produced

"Code": {
     "ImageUri": {
      "Fn::Join": [
       "",
       [
        {
         "Fn::Select": [
          4,
          {
           "Fn::Split": [
            ":",
            {
             "Ref": "EcrRepoArn"
            }
           ]
          }
         ]
        },
        ".dkr.ecr.",
        {
         "Fn::Select": [
          3,
          {
           "Fn::Split": [
            ":",
            {
             "Ref": "EcrRepoArn"
            }
           ]
          }
         ]
        },
        ".",
        {
         "Ref": "AWS::URLSuffix"
        },
        "/",
        {
         "Ref": "EcrRepoName"
        },
        "@",
        {
         "Ref": "ImageDigest"
        }
       ]
      ]
     }
    },

Current Behavior

The following code is produced. Notice the : splitting the image tag

"Code": {
     "ImageUri": {
      "Fn::Join": [
       "",
       [
        {
         "Fn::Select": [
          4,
          {
           "Fn::Split": [
            ":",
            {
             "Ref": "EcrRepoArn"
            }
           ]
          }
         ]
        },
        ".dkr.ecr.",
        {
         "Fn::Select": [
          3,
          {
           "Fn::Split": [
            ":",
            {
             "Ref": "EcrRepoArn"
            }
           ]
          }
         ]
        },
        ".",
        {
         "Ref": "AWS::URLSuffix"
        },
        "/",
        {
         "Ref": "EcrRepoName"
        },
        ":",
        {
         "Ref": "ImageDigest"
        }
       ]
      ]
     }
    },

Reproduction Steps

The following code produces an ECR URI with a : separating the image digest from the URL instead of an @. This causes a deployment failure in cloudformation due to a validation error on the lambda side

const imageDigest = new cdk.CfnParameter(this, 'ImageDigest', {
      type: 'String',
      description: 'The image digest',
      default: 'sha256:...',
    });

    const lambdaFunction = new lambda.DockerImageFunction(this, `${lambdaPrefix}`, {
      code: lambda.DockerImageCode.fromEcr(
        ecr.Repository.fromRepositoryAttributes(this, 'RepoCrossAccount', {
          repositoryArn: ecrRepoArn.valueAsString,
          repositoryName: ecrRepoName.valueAsString,
        }), {
          tagOrDigest:  imageDigest.valueAsString,
        }
      ),
    });

Possible Solution

Partition the imageTagOrDigest field into two separate fields or add a type tag

Additional Information/Context

No response

CDK CLI Version

2.162.1

Framework Version

No response

Node.js Version

v22.4.1

OS

MacOS 14.6.1

Language

TypeScript

Language Version

No response

Other information

No response

Metadata

Metadata

Assignees

Labels

@aws-cdk/aws-lambdaRelated to AWS LambdabugThis issue is a bug.effort/smallSmall work item – less than a day of effortp1

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions