aws-cdk: route53.HostedZone.fromLookup returned error
I have a hosted zone on route53, and use the following CDK code (typescript) to look up that zone using domainName, but got error while running cdk synth, cdk diff and cdk deploy. After changing to use route53.HostedZone.fromHostedZoneAttributes(), it works.
Reproduction Steps
const zone = route53.HostedZone.fromLookup(this, 'Zone', { domainName: props.domainName });
Error Log
Cannot retrieve value from context provider hosted-zone since account/region are not specified at the stack level. Either configure "env" with explicit account and region when you define your stack, or use the environment variables "CDK_DEFAULT_ACCOUNT" and "CDK_DEFAULT_REGION" to inherit environment information from the CLI (not recommended for production stacks)
Subprocess exited with error 1
Environment
- CLI Version : 1.19.0 (build 5597bbe)
- Framework Version:1.19.0
- OS :macOS Mojave version 10.14.6
- **Language :typescript **
Other
This is š Bug Report
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 22
- Comments: 21 (10 by maintainers)
cdk 2.29.0I think the reason synth succeeds while deploy fails when using HostedZoneFromLookup is that the cdk optimistically assumes a successful lookup.
Hereās the synth output for domainHostedZone.HostedZoneId() (using Golang): DUMMY
That looks like a placeholder value in case of unsuccessful lookups.
In fact, after having fought this for a couple of hours, I think the answer to āwhy does the zone lookup sometimes failā in my case is eventual consistency. After deleting and recreating the stack/zone in question a few times, I went for a walk, retried and the zone was now āfoundā.
I have not looked into the JS or Golang implementations yet, but I suspect many may be falling victims to the CDK trying to fallback to a dummy zone instead of failing outright.
Hi, I found what is the problem:
Is related to my alias creatio. Before I was using:
I changed it to:
And now its working o0
Thanks guys
@BryanPan342 I think the primary issue is confusion on when to use which API and the pre-requisites for each (local vs. requiring environment configuration). To reduce friction and avoid this common point of confusion, we need to shore up the
READMEAdditionally, thereās also
fromHostedZoneId(...)andfromHostedZoneName(...)which will return anIHostedZonewith just the ZoneId or the ZoneName field present. This is also done without having make an API call so it can be done entirely locally.fromHostedZoneAttributes()returns anIHostedZonewith both thehostedZoneIdand thezoneNamefinally, thereās
fromLookup()which populates context by making a service call to AWS through a context provider and stores it incdk.context.json. If this is performed once and saved, subsequent calls will be cached and will not require making a service call.If the entry does not exist in
cdk.context.json, then it requires an environment to be configured and takes the attributes and performs an API call to determine which exact HostedZone is deployed and updates its behavior accordingly.If
hostedZoneNameServersproperty from anIHostedZoneis desired,fromLookup()is the onlyfromXxXmethod that will support it.@diogoap -
fromLookupuses a context provider where a service call is made toRoute53to retrieve thehostedZoneIdgiven a domain name.fromHostedZoneAttributeson the other hand requires that thehostedZoneIdbe provided and therefore does not need that lookup.@optinirr can you help me with a repro of your issue (or trace if you can run cdk commands with the
-vflag.Hereās what Iām doing (
v1.41.0) Stack 1:Stack 2 where I will look up the created hosted zone:
Itās created by providing the environment. i.e.
When I run
cdk synth, the context is not there, it gets looked up and populated intocdk.context.jsonwhich looks like thisAdditionally, the context lookup is not performed if the
cdk.context.jsonis already populated. Context lookup is only done when the information is not available during synthesis.But why adding the region and account required only when using
route53.HostedZone.fromLookup?Cannot it be inferred like all the other commands?
@shivlaks sorry for late reply. Yep, Iāve tried to set env with no luck. Time was flying for that specific project so I went with: