How do I add an array of RecordSets into Cloud Formation using troposphere?

1.5k Views Asked by At

I'm using the python module troposphere to create my cloud formation template. Most of it is complete but I seem to be confused about how to create my DNS entries for the load balancer with the RecordSets method/function. The output for this section is supposed to look like:

"devdevopsdemoELBDNSARecord0": {
    "Type": "AWS::Route53::RecordSetGroup",
    "Properties": {
        "HostedZoneName": "FOO.net.",
        "Comment": "Alias targeted to devdevopsdemoELB ELB.",
        "RecordSets": [
            {
             "Name": "devopsdemo.dev.FOO.net.",
             "Type": "A",
             "AliasTarget": {
             "HostedZoneId": {
                 "Fn::GetAtt": [
                    "devdevopsdemoELB",
                    "CanonicalHostedZoneNameID"
                 ]
             },
             "DNSName": {
                 "Fn::GetAtt": [
                 "devdevopsdemoELB",
                 "CanonicalHostedZoneName"
                 ]
             }
         }
     },
     {
         "Name": "devopsdemo-dev.FOO.net.",
         "Type": "A",
         "AliasTarget": {
            "HostedZoneId": {
            "Fn::GetAtt": [
               "devdevopsdemoELB",
               "CanonicalHostedZoneNameID"
             ]
         },
         "DNSName": {
             "Fn::GetAtt": [
             "devdevopsdemoELB",
             "CanonicalHostedZoneName"
             ]
         }
     }
 }

I've started with:

hostedzone = "FOO.net"
myRecordSet = RecordSetType("devdevopsdemoELBDNSARecord0")
myRecordSet.HostedZoneName=Join("", hostedzone, "."])
myRecordSet.Comment="Alias targeted to devdevopsdemoELB ELB."

But then I'm not clear on how the RecordSets values should be entered.

I supposed I could just use the straight

myRecordSet.RecordSets = 

And just put the json into place, but that seems a bit like a misuse of the purpose of using troposphere in the first place.

Update: Putting in the json results in this error

AttributeError: AWS::Route53::RecordSet object does not support attribute RecordSets

myRecordSet.RecordSets = [
                         {
                          "Name": "devopsdemo.dev.FOO.net.",
                          "Type": "A",
                          "AliasTarget": {
                              "HostedZoneId": {
                                  "Fn::GetAtt": [
                                      "devdevopsdemoELB",
                                      "CanonicalHostedZoneNameID"
                                  ]
                              },
                              "DNSName": {
                                  "Fn::GetAtt": [
                                      "devdevopsdemoELB",
                                      "CanonicalHostedZoneName"
                                      ]
                                  }
                              }
                         },
                         {
                          "Name": "devopsdemo-dev.FOO.net.",
                          "Type": "A",
                          "AliasTarget": {
                                 "HostedZoneId": {
                                  "Fn::GetAtt": [
                                      "devdevopsdemoELB",
                                      "CanonicalHostedZoneNameID"
                                      ]
                                  },
                                 "DNSName": {
                                     "Fn::GetAtt": [
                                         "devdevopsdemoELB",
                                         "CanonicalHostedZoneName"
                                         ]
                                     }
                                 }
                          }
                         ]

Then I tried this:

myDNSRecord = t.add_resource(RecordSetGroup(
        "devdevopsdemoELBDNSARecord0",
        HostedZoneName=Join("", ["inpwrd.net", "."]),
        Comment="DNS Entry to point to the ELB for devopsdemo",
        RecordSets=[
            RecordSet(
                Name="devopsdemo.dev.inpwrd.net.",
                Type="A",
                AliasTarget=[HostedZoneId(GetAtt("devdevopsdemoELB", "CanonicalHostedZoneNameID")),
                             DNSName(GetAtt("devdevopsdemoELB", "CanonicalHostedZoneName"))],
                ),
            RecordSet(
                Name="devopsdemo-dev.inpwrd.net.",
                Type="A",
                AliasTarget=[HostedZoneId(GetAtt("devdevopsdemoELB", "CanonicalHostedZoneNameID")),
                             DNSName(GetAtt("devdevopsdemoELB", "CanonicalHostedZoneName"))],
                ),
            ],
    )
)

HostedZoneId isn't found

1

There are 1 best solutions below

0
On BEST ANSWER

AliasTarget takes an AliasTarget helper class so you want to do it this way:

from troposphere import GetAtt, Join, Template
from troposphere.route53 import AliasTarget, RecordSetType, RecordSetGroup, RecordSet

t = Template()

myDNSRecord = t.add_resource(RecordSetGroup(
        "devdevopsdemoELBDNSARecord0",
        HostedZoneName=Join("", ["example.net", "."]),
        Comment="DNS Entry to point to the ELB for devopsdemo",
        RecordSets=[
            RecordSet(
                Name="devopsdemo.dev.example.net.",
                Type="A",
                AliasTarget=AliasTarget(
                    GetAtt("devdevopsdemoELB", "CanonicalHostedZoneNameID"),
                    GetAtt("devdevopsdemoELB", "CanonicalHostedZoneName"),
                ),
            ),
            RecordSet(
                Name="devopsdemo-dev.example.net.",
                Type="A",
                AliasTarget=AliasTarget(
                    GetAtt("devdevopsdemoELB", "CanonicalHostedZoneNameID"),
                    GetAtt("devdevopsdemoELB", "CanonicalHostedZoneName"),
                ),
            ),
        ],
    )
)

print t.to_json()