Issue when using "route53.HostedZone.fromHostedZoneId" Method
Reproduction Steps
When trying to import an HostedZone with :
route53.HostedZone.fromHostedZoneId(this, 'MyHostedZone', 'ID123456789')
Error Log
I've that error when launching the Stack :
HostedZone.fromHostedZoneId doesn't support "zoneName"
Environment
- CLI Version : CDK 1.42.1 (build a4797b4)
- Framework Version: 6.14.5
- Node.js Version: v12.15.0
- OS : Linux
- Language (Version): TypeScript
Other
So, I've imported that way :
route53.HostedZone.fromHostedZoneAttributes(this, 'R53Domain', {hostedZoneId: 'MyID123456789', zoneName: 'Toto'})
What's wrong with fromHostedZoneId Method ?
This is :bug: Bug Report
What you're currently doing is fine. I do believe we can better document this as it has come up before as a point of confusion for users.
fromHostedZoneId(...) and fromHostedZoneName(...) will create a mock object with just the ZoneId or the ZoneName field present. These are useful if you need to supply a HostedZone object but you know that the resource that is accepting the HostedZone only needs access to one of those variables. This is done without having make an API call so it can be done entirely locally.
fromHostedZoneAttributes will actually make a call to AWS to retrieve information and populate cdk.context.json.
It takes the attributes and performs an API call to determine which exact HostedZone is deployed and updates its behavior accordingly.
Understood, thank you for the explanations ! Indeed, it can be detailed in the README and the error log maybe.
Definitely, I've gotten used to use fromXxXAttributes that gives better (real/exact) results.
About cdk.context.json, do you know any reasons why the file can't be populated anymore ?
About cdk.context.json, do you know any reasons why the file can't be populated anymore ?
can you expand on that? I need a little more information to dig into that. some questions that pop into my mind:
- was it being populated before but is not now?
- context lookups won't happen if the information it would look up already exists in
cdk.context.json. are you observing behaviour different from that?
Sorry, it's not our purpose but yes, it was polpulated before and not now. I can say that the last time was the first time, when creating VPC resources ;D I only have that on it :
{
"@aws-cdk/core:enableStackNameDuplicates": "true",
"aws-cdk:enableDiffNoFail": "true",
"availability-zones:account=123456789:region=eu-west-1": [
"eu-west-1a",
"eu-west-1b",
"eu-west-1c"
],
"availability-zones:account=123456789:region=eu-west-1": [
"eu-west-1a",
"eu-west-1b",
"eu-west-1c"
],
"availability-zones:account=123456789:region=eu-west-1": [
"eu-west-1a",
"eu-west-1b",
"eu-west-1c"
],
"vpc-provider:account=123456789:filter.tag:Name=vpc-dev:region=eu-west-1:returnAsymmetricSubnets=true": {
"vpcId": "vpc-123456789",
"vpcCidrBlock": "1.2.3.4/23",
[...]
vpc-provider:account 3 times (for my 3 accounts); that's all. Maybe I've to make a clear on it.
But, don't really nead that file, what is his main use ?
@Cloudrage sorry for the late reply i missed your question here. The primary purpose of the runtime context is to cache information from your AWS account (such as availability zones). This avoids unexpected changes to your deployments.
In your case, the context is populated so that the availability zones that are available and valid for the regions you are deploying are cached. If the cdk.context.json already exists, that lookup to populate the context will not be performed. We recommend that the project's context files be placed under version control along with the rest of your application for consistency when synthesizing and deploying templates.
you can learn more about runtime context here.
I'm resolving this issue as I don't believe further action is required.
Feel free to re-open / ask questions if anything has been left unanswered!
Thanks @shivlaks for your time & explanations !
What if you want to create an ARecord on an existing HostedZone on another account because its the DNS so the hosted zone is not created in any of your stacks? from_hosted_zone_attributes Error is "Template error: Mapping named 'AWSCloudFrontPartitionHostedZoneIdMap' is not present in the 'Mappings' section of template." This is required before I can create a custom domain for my Cognito User Pool, and although it can be done before hand, much neater to have the CDK create this ARecord for any changes to the sub domain name used for login.
@kgunny , I've the same use case and I've to import the HostedZone from the other account : fromHostedZoneAttributes with hostedZoneId & ZoneName props.
These one are stored in a JSON var file to be retrieved by the Stack with a tryGetContext. These 2 values are imutable from an HostedZone.
It's not a perfect way, I don't like that too (globally because CDK can't rertieve natively resources between accounts); but it's the most simpliest way I've found. See : aws/aws-cdk-rfcs#226
Thanks @Cloudrage will experiment and see how I go.
@kgunny Hello, I just hit your error :
Error [ValidationError]: Template error: Mapping named 'AWSCloudFrontPartitionHostedZoneIdMap' is not present in the 'Mappings' section of template.
I'm trying to create a record for a cloudfront distribution. Everything is in the same region except the certificate (it's required to be in us-east-1 by cloudfront).
Also I created the certificate and the hosted zone outside of CDK. I retrieve there infos using ssm.
Did you solve your issue? I tried to solve it the last 2 hours with nice people on cdk's Slack without finding a solution.
Here is my stacks involved and the error log : https://gist.github.com/jgoux/9f26a27137a511e278f8fe01e01f5e3f#file-cloudfront-ts
** EDIT ** :
Problem solved! 🎊 The issue was that I can't create a record alias for a Cloudfront distribution from another stack, they have to be in the same stack. The missing "Mappings" section was actually created in the CloudfrontStack template, so the Route53Stack template couldn't find it in its own template. ✅
Sorry no we are still not able to use certificates across accounts/regions, even with latest CDK and bootstrapping. And the certificate still needs to be hosted in us-east-1. So as a result certificate management, custom domains for API GW, and route 53 targets have to be set up manually. Perhaps someone from AWS could shed some light on possible future plans to help resolve this issue so CDK can be used to roll out this very important component of our infrastructures.
@jgoux Hi, I'm facing the same issue as yours during CDK version migration (from 1.45.0 to 1.110.0)
Error [ValidationError]: Template error: Mapping named 'AWSCloudFrontPartitionHostedZoneIdMap' is not present in the 'Mappings' section of template.
I also resolved it by moving the alias record from route53 stack to cloudfront stack (thanks!) However, it will cause some downtime for my web app because the A record is destroyed & re-created in another stack. Do you happen to know any way around the downtime issue? Thanks in advance!
@danny-xx Why is your record destroyed and re-created? If it's the exact same I guess CDK should diff it. 🤔 In my case I have two stacks, a "stable" and a "dynamic" one. My stable contains everything that will never change between deployments (VPC, RDS, domain), and the dynamic one is for everything else that could change based on our application code (Fargate, Cloudfront, subdomain, lambdas...).
@jgoux Thanks for the quick reply!
In my case, I have the current Alias Record in Route53Stack and I'll have to move it to AssetsStack, where the CloudFront Distribution belongs to.
To move it across different stacks, the record would be destroyed in the current route53 stack and then created in the assets stack. It causes a downtime for around 10 minutes
The Alias Record also doesn't support TTL time, which would help me shorten/eliminate the downtime 😕
I'm resolving this issue as I don't believe further action is required.
Feel free to re-open / ask questions if anything has been left unanswered!
I've encountered this issue just now and I believe this should be reopened @shivlaks
Why would the following call (I'm using Python CDK v1.114.0):
route53.HostedZone.from_hosted_zone_id(self, "MyHostedZone", hosted_zone_id="MyHostedZoneId")
Result in:
Error: HostedZone.fromHostedZoneId doesn't support "zoneName"
This doesn't make sense... I didn't pass the function a zone name and in fact the function doesn't even accept a zone name... This is either poor API design, or a bug - calling this expected behavior seems like a stretch to me.
I agree this is an issue. If I pass the zoneId (which I did) I should not be getting an error for the name?
I agree with other here, this is a bug, either in the documentation or implementation..
Reading the documentation for from_hosted_zone_id it clearly states:
Use when hosted zone ID is known. Hosted zone name becomes unavailable through this query.
As such, I would expect it to work, instead of complaining about an argument which I did not pass, and which it does not accept.
You will get an error for zoneName when using fromHostedZoneId because you have tried to pass your imported hosted zone to somewhere which will try to reference your hosted zone's zoneName property. This will lead to error, because the hosted zone that has been created has no zoneName property. This is why this is considered expected behavior.
Seeing as this is causing lots of confusion, we can improve the error message.
I'm also working on improving the documentation related to how importing resources in CDK works in general, which should also help to reduce confusion surrounding these types of errors.
Sidenote - I've seen this in both related threads
fromHostedZoneAttributes will actually make a call to AWS to retrieve information and populate cdk.context.json
This is incorrect. only fromLookup will actually make a call to AWS to retrieve information.
Thanks for taking some action on this @peterwoodworth , it's really appreciated. Are you also aware of the conversation on #3558 ? There's further discussion there from a number of community members around the expectation that fromHostedZoneId will do a genuine lookup (tl;dr: it's a valid use-case, and we shouldn't intentionally have a broken implementation of an interface)