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

(aws-cloudfront): CloudFront IFunction interface is structurally compatible with Lambda's IFunction, but not compatible in AWS #16885

Open
moltar opened this issue Oct 9, 2021 · 10 comments
Labels
@aws-cdk/aws-cloudfront Related to Amazon CloudFront bug This issue is a bug. effort/small Small work item – less than a day of effort p2

Comments

@moltar
Copy link
Contributor

moltar commented Oct 9, 2021

What is the problem?

I am able to assign a Lambda function to CloudFront function (matching TypeScript interfaces), but when the solution is deployed, it produces an error, as Lambda functions cannot be used as CloudFront functions.

Reproduction Steps

new cloudfront.Distribution(this, "CloudFrontDistribution", {
  defaultBehavior: {
    origin: new S3Origin(bucket),
    functionAssociations: [
      {
        // this does not result in error
        function: new NodejsFunction(this, "Fn", {
          entry: 'cf.js',
        }),
        eventType: cloudfront.FunctionEventType.VIEWER_REQUEST
      }
    ]
  },
})

What did you expect to happen?

Interfaces to be incompatible and get an error in the IDE and at build time.

What actually happened?

No error was produced in the IDE or at build time.

Error happened during the deployment:

Invalid request provided: 2 validation errors detected: Value 'arn:aws:lambda:ap-northeast-1:123456:function:Fn220341F76-xxx' at 'distributionConfig.cacheBehaviors.items.1.member.functionAssociations.items.1.member.functionARN' failed to satisfy constraint: Member must satisfy regular expression pattern: arn:aws:cloudfront::[0-9]{12}:function/[a-zA-Z 0-9-_]{1,64}$; Value 'arn:aws:lambda:ap-northeast-1:123456:function:Fn220341F76-h4X9p0uP7M5E' at 'distributionConfig.cacheBehaviors.items.1.member.functionAssociations.items.1.m ember.functionARN' failed to satisfy constraint: Member must have length less than or equal to 108 (Service: CloudFront, Status Code: 400, Request ID: 5fcc4d15-74b0-4a55-9845-8631a395fdb2, Extended Request ID: null)"

CDK CLI Version

1.126.0 (build f004e1a)

Framework Version

1.126.0

Node.js Version

v14.17.5

OS

macOS

Language

Typescript

Language Version

TypeScript 4.4.3

Other information

No response

@moltar moltar added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Oct 9, 2021
@github-actions github-actions bot added the @aws-cdk/aws-cloudfront Related to Amazon CloudFront label Oct 9, 2021
@peterwoodworth
Copy link
Contributor

Thanks for submitting this issue @moltar,

Seems to me like this is a consequence of the lambda IFunction having all of the properties that cloudfront's IFunction has. I'm not sure there's anything we can do about this, and it's more a consequence of how the language works. @njlynch is this accurate?

@peterwoodworth peterwoodworth removed the needs-triage This issue or PR still needs to be triaged. label Oct 13, 2021
@moltar
Copy link
Contributor Author

moltar commented Oct 14, 2021

Seems to me like this is a consequence of the lambda IFunction having all of the properties that cloudfront's IFunction has.

That is correct.

I'm not sure there's anything we can do about this, and it's more a consequence of how the language works. @njlynch is this accurate?

There is a way to do this, it's a bit hack-ish, using nominal typing.

@njlynch njlynch added effort/small Small work item – less than a day of effort p2 labels Oct 15, 2021
@njlynch njlynch removed their assignment Oct 15, 2021
@njlynch
Copy link
Contributor

njlynch commented Oct 15, 2021

Yeah, just about the only way to fix this would be to add a conflicting parameter between the two (e.g., an extra prop to CloudFront's IFunction) just for the sake of avoiding this. This might naturally happen in the future, as new capabilities are added to CloudFront Functions.

@moltar
Copy link
Contributor Author

moltar commented Oct 15, 2021

Probably too late, but at least aming the interface differently would be good.

Maybe ICloudFrontFunction, as it is only applicable to CF anyways.

@github-actions
Copy link

This issue has not received any attention in 1 year. If you want to keep this issue open, please leave a comment below and auto-close will be canceled.

@github-actions github-actions bot added the closing-soon This issue will automatically close in 4 days unless further comments are made. label Oct 15, 2022
@moltar
Copy link
Contributor Author

moltar commented Oct 15, 2022

Not stale.

GitHub stale bot considered harmful

@github-actions github-actions bot removed the closing-soon This issue will automatically close in 4 days unless further comments are made. label Oct 15, 2022
@ashishdhingra
Copy link
Contributor

@moltar Good afternoon. As mentioned in the #16885 (comment), this is a consequence on how TypeScript works. Are you able to see the same issue in other languages (such as .NET, Java or Python)? I'm unsure if it is feasible to raise the error during build time.

@ashishdhingra ashishdhingra added the response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. label Jun 5, 2024
@laurelmay
Copy link
Contributor

I don't think it'd be unreasonable to bring functionRuntime from FunctionAttributes in as a property of cloudfront.IFunction -- that (at least nullably) has to be defined for both imported and created functions. That attribute does not exist for lambda.IFunction. That structural difference should appease languages that allow structural equivalence for types.

@github-actions github-actions bot removed the response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. label Jun 6, 2024
@ashishdhingra
Copy link
Contributor

I don't think it'd be unreasonable to bring functionRuntime from FunctionAttributes in as a property of cloudfront.IFunction -- that (at least nullably) has to be defined for both imported and created functions. That attribute does not exist for lambda.IFunction. That structural difference should appease languages that allow structural equivalence for types.

@kylelaker FunctionProps does have the Runtime attribute.

@ashishdhingra ashishdhingra added the response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. label Jun 6, 2024
@laurelmay
Copy link
Contributor

laurelmay commented Jun 6, 2024

@kylelaker FunctionProps does have the Runtime attribute.

Sure, but that's not exposed in lambda.IFunction (at least not with the name I proposed here) 🤷‍♀️ It's an option that's currently available to solve the issue. This issue is about the IFunction interfaces.

I didn't look into whether FunctionProps has the same problems but I thought those were already incompatible.

@github-actions github-actions bot removed the response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. label Jun 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-cloudfront Related to Amazon CloudFront bug This issue is a bug. effort/small Small work item – less than a day of effort p2
Projects
None yet
Development

No branches or pull requests

5 participants