aws-cdk: (CloudFront): Initial Create Succeeds, Subsequent Updates Fail with Invalid request provided: AWS::CloudFront::PublicKey
The following code will create a proper Cloudfront PublicKey and it is usable to restrict access to private resources.
But, any updates to the stack that update the CloudFront PublicKey will fail with:
Resource handler returned message: "Invalid request provided: AWS::CloudFront::PublicKey" (RequestToken: fd28451c-389a-cdd8-d3f7-2bb76874bfee, HandlerErrorCode: InvalidRequest)
Reproduction Steps
cloudfront_public_key = cloudfront.PublicKey(
self, "cloudfront-public-key",
encoded_key=<generated_public_key>
)
cloudfront_keygroup = cloudfront.KeyGroup(
self, "cloudfront-keygroup",
items=[cloudfront_public_key]
)
What did you expect to happen?
I would expect the PublicKey to update without error.
What actually happened?
Environment
- **CDK CLI Version : ** 1.108.0 (build b23f781)
- Framework Version: 1.108.0 (build b23f781)
- Node.js Version: v12.18.2
- OS : CodeBuild/CodePipeline
- Language (Version): Python 3.7.4
Other
This is š Bug Report
About this issue
- Original URL
- State: open
- Created 5 years ago
- Reactions: 13
- Comments: 41 (19 by maintainers)
Commits related to this issue
- docs(cloudfront): specify that cloudront does not support updating the key (#34676) ### Issue # (if applicable) Related to #15301. ### Reason for this change The error message returned by cloudfor... — committed to aws/aws-cdk by leonmk-aws a year ago
- docs(cloudfront): specify that cloudront does not support updating the key (#34676) ### Issue # (if applicable) Related to #15301. ### Reason for this change The error message returned by cloudfor... — committed to aws/aws-cdk by leonmk-aws a year ago
- docs(cloudfront): add example to update Cloudfront publickey (#34729) ### Issue # (if applicable) Related to #15301. ### Reason for this change Users wanted a more detailed guide on how to update ... — committed to aws/aws-cdk by leonmk-aws a year ago
in public-key.ts
https://github.com/aws/aws-cdk/blob/e220bc8ca9b75bcbb4bb7447703f32737b47fc77/packages/aws-cdk-lib/aws-cloudfront/lib/public-key.ts#L69
The issue is with
callerReference: this.node.addr. This generates a new value on each deployment because:node.addris a unique identifier generated by CDK for each construct instanceWhen CDK tries to update the PublicKey with a new
callerReference, CloudFront rejects the change since this field is immutable. This explains why initial creation works but subsequent updates fail.The fix would be to use a stable callerReference that doesnāt change between deployments.
Iāll bring this up to the team for further review with potential fix.
Just bumped this to p1. Weāll review this issue immediately. Thank you for bringing this up to our attention.
Iām getting absolutely destroyed by this bug, currently.
ā¦what the heck, guys?! This has been open for YEARS.
First run is fine. Subsequent runs error out, miserably.
This is inside an AWS Amplify Custom CDK resourceā¦so a lot of things all break as a result. CI/CD pipeline has come to a grinding halt. Totally.
This is just a question of uniqueness in the name generated for the resources ā both for cloudfront publicKey names, as well as for secrets in SSM.
It turns out, itās not such a hard problem to solve.
WARNING: This means on each run, old keys and secrets get nuked! They donāt get updated or versioned!
Leaving this here for anyone else that lands on the same issue or problem.
I have solved it thusly:
Weāre still investigating this issue. A potential workaround now would be to change the logical ID of the public key, which will force a new one to be created with the correct properties.
Thanks @pahud. Iām on vacation until July 12th. However, the code I linked to above in my CDK wrapper specifically for working with PublicKey should be useful for triage in the meantime. (I would consider that minimal on any OSS repo I manage since the code is isolated) I highlighted the workarounds, and you can see the custom resource used to create the key. Commenting out the workaround will reliably reproduce CDK attempting to update the key.
Brother read the others in the issue. Sheesh.
only coming up on 4 years since this was opened and itās still an issue. just ran into it. come on, folks.
Thanks to @armenr suggestion and @comcalvi for the suggestions.
It did help our case, but it was creating an new edge case i.e every time there is a new deployment, the earlier signed cookies would get invalidated (we are using Cloudfront Signed cookies)
One of the signed cookie name is āCloudFront-Key-Pair-Idā and the since the Public Key resource ID has is changed on subsequent deployments, the earlier key id is deleted making the older cookies invalid!!
So here is the workaround we implemented (with 2 minor manual steps):
How it works:
This method adds 2 manual steps which are only to be done once, i.e during creation and during deletion, so itās definitely worth the trade off for solving our specific problem of cookies invalidation on new deployments!