queirozsc
8/30/2017 - 7:01 PM

Using Nested Stacks w/ AWS CloudFormation https://www.aaronmedacco.com/blog/post/2017/04/22/using-nested-stacks-w-aws-cloudformation

{
    "AWSTemplateFormatVersion": "2010-09-09",
    "Description": "Generate internet-facing load balancer.",
    "Parameters": {
        "Domain": {
            "Type": "String",
            "Description": "Domain serviced by load balancer."
        },
        "VPC": {
            "Type": "AWS::EC2::VPC::Id",
            "Description": "VPC for the load balancer."
        },
        "PublicSubnet1": {
            "Type": "AWS::EC2::Subnet::Id",
            "Description": "First public subnet."
        },
        "PublicSubnet2": {
            "Type": "AWS::EC2::Subnet::Id",
            "Description": "Second public subnet."
        }
    },
    "Resources": {
        "HostedZone": {
            "Type": "AWS::Route53::HostedZone",
            "Properties": {
                "Name": { "Ref": "Domain" }
            }
        },
        "HostedZoneRecords": {
            "Type": "AWS::Route53::RecordSetGroup",
            "Properties": {
                "HostedZoneId": { "Ref": "HostedZone" },
                "RecordSets": [{
                    "Name": { "Ref": "Domain" },
                    "Type": "A",
                    "AliasTarget": {
                        "DNSName": { "Fn::GetAtt": [ "LoadBalancerStack", "Outputs.LoadBalancerDNS" ]},
                        "HostedZoneId": { "Fn::GetAtt": [ "LoadBalancerStack", "Outputs.LoadBalancerHostedZoneID" ]}
                    }
                }]
            }
        },
        "LoadBalancerStack": {
            "Type": "AWS::CloudFormation::Stack",
            "Properties": {
                "Parameters": {
                    "VPC": { "Ref": "VPC" },
                    "PublicSubnet1": { "Ref": "PublicSubnet1" },
                    "PublicSubnet2": { "Ref": "PublicSubnet2" }
                },
                "TemplateURL": "https://s3.amazonaws.com/cf-templates-1bc7bmahm5ald-us-east-1/loadbalancer.json"
            }
        }
    }
}
{
    "AWSTemplateFormatVersion": "2010-09-09",
    "Description": "Simple Load Balancer",
    "Parameters": {
        "VPC": {
            "Type": "AWS::EC2::VPC::Id",
            "Description": "VPC for the load balancer."
        },
        "PublicSubnet1": {
            "Type": "AWS::EC2::Subnet::Id",
            "Description": "First public subnet."
        },
        "PublicSubnet2": {
            "Type": "AWS::EC2::Subnet::Id",
            "Description": "Second public subnet."
        }
    },
    "Resources": {
        "ElasticLoadBalancer": {
            "Type": "AWS::ElasticLoadBalancingV2::LoadBalancer",
            "Properties" : {
                "Name": "Load-Balancer",
                "Scheme": "internet-facing",
                "Subnets": [ {"Ref": "PublicSubnet1"}, {"Ref": "PublicSubnet2"} ],
                "Tags": [ { "Key": "Name", "Value": "CloudFormation Load Balancer" } ]
            }
        }
    },
    "Outputs": {
        "LoadBalancerDNS": {
            "Description": "Public DNS For Load Balancer",
            "Value": { "Fn::GetAtt": [ "ElasticLoadBalancer", "DNSName" ] }
        },
        "LoadBalancerHostedZoneID": {
            "Description": "Canonical Hosted Zone ID of load balancer.",
            "Value": { "Fn::GetAtt": [ "ElasticLoadBalancer", "CanonicalHostedZoneID" ] } 
        }
    }
}