Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
1 task
chrislciaba opened this issue Oct 23, 2024 · 3 comments · Fixed by #32053
Assignees
Labels
@aws-cdk/aws-lambda Related to AWS Lambda bug This issue is a bug. effort/small Small work item – less than a day of effort p1

Comments

@chrislciaba
Copy link

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

@chrislciaba chrislciaba added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Oct 23, 2024
@github-actions github-actions bot added the @aws-cdk/aws-lambda Related to AWS Lambda label Oct 23, 2024
@pahud pahud self-assigned this Oct 23, 2024
@pahud
Copy link
Contributor

pahud commented Oct 23, 2024

I think it's because repositoryUriForTagOrDigest() is not handling unresolved tokens well.

imageUri: this.repository.repositoryUriForTagOrDigest(this.props?.tagOrDigest ?? this.props?.tag ?? 'latest'),

Specifically this func

public repositoryUriForTagOrDigest(tagOrDigest?: string): string {
if (tagOrDigest?.startsWith('sha256:')) {
return this.repositoryUriForDigest(tagOrDigest);
} else {
return this.repositoryUriForTag(tagOrDigest);
}
}

Making this a p1 and we probably need a PR to better handle unresolved tokens.

@pahud pahud added p1 effort/small Small work item – less than a day of effort labels Oct 23, 2024
@pahud pahud removed their assignment Oct 23, 2024
@pahud pahud removed the needs-triage This issue or PR still needs to be triaged. label Oct 23, 2024
@moelasmar moelasmar self-assigned this Oct 24, 2024
@mergify mergify bot closed this as completed in #32053 Nov 8, 2024
@mergify mergify bot closed this as completed in 5648199 Nov 8, 2024
Copy link

github-actions bot commented Nov 8, 2024

Comments on closed issues and PRs are hard for our team to see.
If you need help, please open a new issue that references this one.

1 similar comment
Copy link

github-actions bot commented Nov 8, 2024

Comments on closed issues and PRs are hard for our team to see.
If you need help, please open a new issue that references this one.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 8, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 8, 2024
Leo10Gama pushed a commit to Leo10Gama/aws-cdk that referenced this issue Nov 13, 2024
…aws#32053)

### Issue # (if applicable)

Closes aws#31860.

### Reason for this change
Currently customers can pass one property `tagOrDigest` and if the customers pass a CFN parameter, CDK could not know if it is a tag or digest, and so the generated URI is not correct.

Now the same parameter can supports Tokens, and it will generate a CFN condition to check if the value of this token is digest or tag, and then update the uri based on the condition output.


### Description of changes

Check if the input is a Token, and so instead of determining if its value is a tag or digest in synth time, we create a CFN condition to do this check in CFN, and then determine how to build the repo uri.

### Description of how you validated changes

Added unit test cases, and integration test cases with assertions.

### Checklist
- [X] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md)

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
@moelasmar moelasmar reopened this Nov 22, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
@aws-cdk/aws-lambda Related to AWS Lambda bug This issue is a bug. effort/small Small work item – less than a day of effort p1
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants