Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS Cloudformation Get Hosted Zone Name from Hosted Zone ID

When taking a parameter of type AWS::Route53::HostedZone::Id is there a way to get the HostedZone name?

The hosted zone already exists but was not created with Cloudformation so there is no way for me to reference the name from another template.

Using type AWS::Route53::HostedZone::Id allows the user to select from a drop down, but the ID is chosen not the name.

Is there a way to get the name from the ID so that a record set can be created?

Here is the template I am using, notice the Name of the record set entry where we need the name of the hosted zone to create the record set.

AWSTemplateFormatVersion: '2010-09-09'
Description: Route53
Parameters:
  HostedZone:
    Type: AWS::Route53::HostedZone::Id
    Description: The Hosted Zone for the Record Set
  RecordSetName:
    Type: String
    Description: The name of the record set (all lowercase)

Resources:
  Route53:
    Type: AWS::Route53::RecordSet
    Properties:
      HostedZoneId: !Ref HostedZone
      Comment: DNS name
      Name: !Sub ${RecordSetName}.??????
      Type: A
      TTL: '60'
      ResourceRecords:
        - 10.1.1.1
like image 391
jhnclvr Avatar asked Jul 29 '19 15:07

jhnclvr


People also ask

How do I find my AWS hosted zone?

Sign in to the AWS Management Console and open the Route 53 console at https://console.aws.amazon.com/route53/ . In the navigation pane, choose Hosted Zones. On the Hosted Zones page, choose the name of a hosted zone. The console displays the list of records for that hosted zone.

What is AWS hosted zone ID?

A hosted zone is an Amazon Route 53 concept. A hosted zone is analogous to a traditional DNS zone file; it represents a collection of records that can be managed together, belonging to a single parent domain name. All resource record sets within a hosted zone must have the hosted zone's domain name as a suffix.

What is Privated hosted zone?

A private hosted zone is a container that holds information about how you want to route traffic for a domain and its subdomains within one or more Amazon Virtual Private Clouds (Amazon VPCs). To begin, you create a private hosted zone and specify the Amazon VPCs that you want to associate with the hosted zone.


1 Answers

Given the problem you appear to be trying to solve (add an A record for your apex domain) you don't actually need the drop down parameter selector of type AWS::Route53::HostedZone::Id. Instead you can just use your String input and use HostedZoneName instead of HostedZoneId in the AWS::Route53::RecordSet as shown below:

AWSTemplateFormatVersion: '2010-09-09'
Parameters:
  DomainName:
    Type: String
    Description: apex domain name

Resources:
  Route53:
    Type: AWS::Route53::RecordSet
    Properties:
      HostedZoneName: !Sub '${DomainName}.'
      Comment: DNS name
      Name: !Ref DomainName
      Type: A
      TTL: '60'
      ResourceRecords:
        - 10.1.1.1

(note that you need to add the extra period . onto the end of the DomainName for the HostedZoneName).

If you wanted a sub-domain you could do something like:

AWSTemplateFormatVersion: '2010-09-09'
Parameters:
  DomainName:
    Type: String
    Description: apex domain name
  DomainPrefix:
    Type: String
    Description: sub domain prefix

Resources:
  Route53:
    Type: AWS::Route53::RecordSet
    Properties:
      HostedZoneName: !Sub '${DomainName}.'
      Comment: DNS name
      Name: !Sub '${DomainPrefix}.${DomainName}'
      Type: A
      TTL: '60'
      ResourceRecords:
        - 10.1.1.2

With reference to Fn::GetAtt, you would use these when creating cloudformation exports for your resources, not when using the resources as in this question.

You can if you wish create exports containing the apex domain name and hosted zone ids, which is what I prefer to do to keep things tidy. However, exports are region specific, so if you deploy across multiple regions (which might be forced on you if you are using CloudFront and wants APIs deployed to other than us-east-1) you will need some faking up the exports in some of the regions.

like image 87
geoff.weatherall Avatar answered Oct 17 '22 07:10

geoff.weatherall