jhnclvr
jhnclvr

Reputation: 9487

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

Upvotes: 13

Views: 8651

Answers (3)

geoff.weatherall
geoff.weatherall

Reputation: 657

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.

Upvotes: 6

Oleg Neumyvakin
Oleg Neumyvakin

Reputation: 10272

Hosted Zone ID is displayed in Route 53 console UI and looks like Z1AVC899B05E2Y

Upvotes: 1

UmairAhmad
UmairAhmad

Reputation: 150

Fn::GetAtt The Fn::GetAtt intrinsic function returns a value for a specified attribute of this type. The following are the available attributes and sample return values.

For more information about using the Fn::GetAtt intrinsic function, see Fn::GetAtt.

NameServers Returns the set of name servers for the specific hosted zone. For example: ns1.example.com.

This attribute is not supported for private hosted zones.

https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-route53-hostedzone.html

Upvotes: 0

Related Questions