aws / aws-cdk

The AWS Cloud Development Kit is a framework for defining cloud infrastructure in code
https://aws.amazon.com/cdk
Apache License 2.0
11.59k stars 3.89k forks source link

Route53: HostedZone.fromHostedZoneId() throws an error for cdk synth #3663

Closed realharry closed 5 years ago

realharry commented 5 years ago

Example code:

const domainZone = route53.HostedZone.fromHostedZoneId(this, 'domain-zone', hostedZoneId);

cdk synth throws an error:

HostedZone.fromHostedZoneId doesn't support "zoneName"

fromHostedZoneId() should return a valid IHosted­Zone object.

HostedZone is needed, for instance, for LoadBalancedFargateServiceProps (for the domainZone property).

rhboyd commented 5 years ago

duplicate of https://github.com/aws/aws-cdk/issues/3558

using from_hostedzone_attributes() is the preferred method for fetching the hostedzone information

shivlaks commented 5 years ago

hey @realharry ,

As @rhboyd mentioned, this seems to be a duplicate. Please reopen if the behavior you're observing is unique and the direction provided in the related issue doesn't work for you.

rhboyd commented 5 years ago

@realharry for Hosted Zones there are 3 methods that accomplish very similar, but slightly different goals.

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.

The third method is fromAttributes(...) that will take the attributes and perform an API call to determine which exact HostedZone is deployed and updates its behavior accordingly.

The first two methods create use different Classes of IHostedZone that throw an error if you use the wrong parameter (e.g. calling getName() when you used fromHostedZoneId())

mimozell commented 3 years ago

Is there any way to get the HostedZone if I only have the ID? I was using fromHostedZoneId and got the same error, but then I switched to using fromAttributes and now I need to provide the name in addition to the ID :(

esteban-uo commented 3 years ago

@rhboyd fromHostedZoneName seems to be got deprecated and seems to be fromHostedZoneName is doing an actual API call, how are you dealing with it? thanks!

kevinjamescasey commented 2 years ago

I just fell into the same trap two years after this problem was first reported. It would be nice if the docs were updated so everybody didn't have to get the error, search for it on google, and read a bunch of threads to find the explanation of this wacky function.

adrian-skybaker commented 2 years ago

using from_hostedzone_attributes() is the preferred method for fetching the hostedzone information

  1. Why is zone name required in addition to zone id? It's not required on the underlying resource (which is how I've worked around), ie:

    CfnRecordSet.Builder.create(this, "PublicRoute53Alias")
    .hostedZoneId(Fn.importValue("..."))
  2. How is it possible to know that from_hostedzone_attributes() is preferred? Short of hitting this error, pasting it into google, and spending time reviewing these tickets:

It feels something is wrong with the API, that documentation alone can only mitigate, yet all of these tickets are closed.

esteban-uo commented 2 years ago

@adrian-skybaker domainZone is already optional for ApplicationLoadBalancedServiceBase at least in master. If you need to use an older version, you could create a class and mock it and will also work. Hopefully works for someone else:


class MockZone extends cdk.Resource implements IHostedZone { 
  public readonly hostedZoneId = ''
  public readonly zoneName = ''
  public get hostedZoneArn(): string { return '' }
}

new aws_ecs_patterns.ApplicationLoadBalancedFargateService(this,
  'FargateService',
  {
    certificate: cert,
    domainZone: new MockZone(this, 'mockzone'),
    ....
  }
)
peterfranzen commented 2 years ago

I just ran into this error as well-- I am trying to use route53.HostedZone.from_hosted_zone_id() and it fails with the following error when a ZoneID is provided: HostedZone.fromHostedZoneId doesn't support "zoneName"

v-karbovnichy commented 2 years ago

Please reopen this and fix the docs

peterwoodworth commented 2 years ago

Responded and reopened here https://github.com/aws/aws-cdk/issues/8406