13

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
jhnclvr
  • 9,137
  • 5
  • 50
  • 55
  • You'll almost certainly need to use a custom resource which can look up that info and return you a name for that id. – 404 Jul 30 '19 at 12:01

3 Answers3

6

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.

  • Thank you for the hint about the trailing "." on the HostedZoneName! This isn't made obvious anywhere in the docs. – Schrockwell Oct 21 '20 at 12:43
  • 1
    I'd describe this as a useful workaround (which is of course a perfectly valid thing to answer with - hence a +1 from me). Being able to require the template user to select an actual hosted zone within their account is quite valuable for communicating how to use the template correctly. Unfortunately, AWS::Route53::HostedZone::Id is the only Route53 parameter type supported. Seems like a gap. – nickform Dec 23 '20 at 14:49
1

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

Oleg Neumyvakin
  • 9,706
  • 3
  • 58
  • 62
0

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

UmairAhmad
  • 150
  • 3
  • 14