{
  "AWSTemplateFormatVersion": "2010-09-09",
    "Parameters": {
      "VpcCidr": {
        "Description": "The VPC Network Range",
        "Type": "String",
        "MinLength": "9",
        "MaxLength": "18",
        "Default": "10.0.0.0/16",
        "AllowedPattern": "(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})/(\\d{1,2})",
        "ConstraintDescription": "must be a valid IP CIDR range of the form x.x.x.x/x."
      },
      "SubnetCidr": {
        "Description": "The Subnet Network Range",
        "Type": "String",
        "MinLength": "9",
        "MaxLength": "18",
        "Default": "10.0.0.0/24",
        "AllowedPattern": "(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})/(\\d{1,2})",
        "ConstraintDescription": "must be a valid IP CIDR range of the form x.x.x.x/x."
      },
      "SubnetAvailabilityZone": {
        "Description": "Availability Zone",
        "Type": "String",
        "Default": "auto",
        "ConstraintDescription": "Must be a valid AZ for the region or 'auto'."
      },
      {{partial 'parameter-name-tag'}}
    },
    "Conditions": {
      "SubnetAutoSelection": {"Fn::Equals": [{"Ref": "SubnetAvailabilityZone"}, "auto"]}
    },
    "Resources": {
      "Vpc": {
        "Type": "AWS::CloudFormation::Stack",
        "Properties": {
          "Parameters": {
            "VpcCidr": {
              "Ref": "VpcCidr"
            },
            "CreateVpcSecurityGroup": "true",
            "CreateInternetGateway": "true",
            "OpenVpcCommunication": "true",
            "NameTag": {
              "Ref": "NameTag"
            }
          },
          "TemplateURL": "{{templateS3Url 'vpc.template.json'}}"
        }
      },
      "Subnet": {
        "Type": "AWS::CloudFormation::Stack",
        "Properties": {
          "Parameters": {
            "SubnetCidr": {
              "Ref": "SubnetCidr"
            },
            "AvailabilityZone": {
              "Fn::If": [
                "SubnetAutoSelection",
              {"Fn::Select": ["0",{"Fn::GetAZs" : "" }]},
              {"Ref": "SubnetAvailabilityZone"}
              ]
            },
            "VpcId": { "Fn::GetAtt": [ "Vpc", "Outputs.VpcId" ] },
            "RouteTableId": {
              "Fn::GetAtt": [
                "Vpc",
              "Outputs.RouteTableId"
                ]
            },
            "NameTag": { "Ref": "NameTag" }
          },
          "TemplateURL": "{{templateS3Url 'subnet.template.json'}}"
        },
        "DependsOn": "Vpc"
      }
    },
    "Outputs": {
      "SubnetId" : {
        "Value" : {"Fn::GetAtt": ["Subnet","Outputs.SubnetId"]},
        "Description" : "Subnet"
      },
      "VpcId" : {
        "Value" : {"Fn::GetAtt": ["Vpc","Outputs.VpcId"]},
        "Description" : "VPC ID"
      },
      "SubnetAZ": {
        "Value": {
          "Fn::If": [
            "SubnetAutoSelection",
            {"Fn::Select": ["0",{"Fn::GetAZs" : "" }]},
            {"Ref": "SubnetAvailabilityZone"}
          ]
        }
      }
    }
}
