{
  "AWSTemplateFormatVersion" : "2010-09-09",
  "Description" : "ArcGIS CloudFormation Template: Provisions an Elastic Load Balancer for ArcGIS Server. **WARNING** You will be billed by AWS for the AWS resources if you create a stack from this template.",
  "Parameters" : {
    "VPCId" : {
      "Description" : "VPC ID",
      "Type" : "AWS::EC2::VPC::Id" 
    },
    "Subnet1" : {
      "Description" : "Subnet 1",
      "Type": "AWS::EC2::Subnet::Id"
    },
    "Subnet2" : {
      "Description" : "Subnet 2",
      "Type": "AWS::EC2::Subnet::Id"
    }
  },
  "Resources" : {
    "ELB" : {
      "Type" : "AWS::ElasticLoadBalancingV2::LoadBalancer",
      "Properties" : {
        "Type" : "network",
        "Subnets" : [ {"Ref" : "Subnet1"}, {"Ref" : "Subnet2"} ],
        "Scheme" : "internet-facing"
      }
    },
    "AGSHttpListener" : {
      "Type" : "AWS::ElasticLoadBalancingV2::Listener",
      "Properties" : {
        "DefaultActions" : [ {"TargetGroupArn" : {"Ref" : "AGSHttpTarget"}, "Type" : "forward"} ],
        "LoadBalancerArn" : {"Ref" : "ELB"},
        "Port" : 80,
        "Protocol" : "TCP"
      }
    },
    "AGSHttpsListener" : {
      "Type" : "AWS::ElasticLoadBalancingV2::Listener",
      "Properties" : {
        "DefaultActions" : [ {"TargetGroupArn" : {"Ref" : "AGSHttpsTarget"}, "Type" : "forward"} ],
        "LoadBalancerArn" : {"Ref" : "ELB"},
        "Port" : 443,
        "Protocol" : "TCP"
      }
    },
    "GEEServicesHttpListener" : {
      "Type" : "AWS::ElasticLoadBalancingV2::Listener",
      "Properties" : {
        "DefaultActions" : [ {"TargetGroupArn" : {"Ref" : "GEEServicesHttpTarget"}, "Type" : "forward"} ],
        "LoadBalancerArn" : {"Ref" : "ELB"},
        "Port" : 6180,
        "Protocol" : "TCP"
      }
    },
    "GEEServicesHttpsListener" : {
      "Type" : "AWS::ElasticLoadBalancingV2::Listener",
      "Properties" : {
        "DefaultActions" : [ {"TargetGroupArn" : {"Ref" : "GEEServicesHttpsTarget"}, "Type" : "forward"} ],
        "LoadBalancerArn" : {"Ref" : "ELB"},
        "Port" : 6143,
        "Protocol" : "TCP"
      }
    },
    "AGSHttpTarget" : {
      "Type" : "AWS::ElasticLoadBalancingV2::TargetGroup",
      "Properties" : {
        "HealthCheckIntervalSeconds" : 10,
        "HealthCheckPath" : "/arcgis/rest/info/healthcheck",
        "HealthCheckPort" : 6080,
        "HealthCheckProtocol" : "HTTP",
        "HealthyThresholdCount" : 3,
        "UnhealthyThresholdCount" : 3,
        "Port" : 6080,
        "Protocol" : "TCP",
        "TargetType" : "instance",
        "VpcId" : {"Ref" : "VPCId"}
      }
    },
    "AGSHttpsTarget" : {
      "Type" : "AWS::ElasticLoadBalancingV2::TargetGroup",
      "Properties" : {
        "HealthCheckIntervalSeconds" : 10,
        "HealthCheckPath" : "/arcgis/rest/info/healthcheck",
        "HealthCheckPort" : 6443,
        "HealthCheckProtocol" : "HTTPS",
        "HealthyThresholdCount" : 3,
        "UnhealthyThresholdCount" : 3,
        "Port" : 6443,
        "Protocol" : "TCP",
        "TargetType" : "instance",
        "VpcId" : {"Ref" : "VPCId"}
      }
    },
    "GEEServicesHttpTarget" : {
      "Type" : "AWS::ElasticLoadBalancingV2::TargetGroup",
      "Properties" : {
        "Port" : 6180,
        "Protocol" : "TCP",
        "TargetType" : "instance",
        "VpcId" : {"Ref" : "VPCId"}
      }
    },
    "GEEServicesHttpsTarget" : {
      "Type" : "AWS::ElasticLoadBalancingV2::TargetGroup",
      "Properties" : {
        "Port" : 6143,
        "Protocol" : "TCP",
        "TargetType" : "instance",
        "VpcId" : {"Ref" : "VPCId"}
      }
    }
  },
  "Outputs" : {
    "ELBName" : {
      "Description" : "Elastic Load Balancer name",
      "Value" :  {"Fn::GetAtt" : ["ELB", "LoadBalancerName"]}
    },
    "DNSName" : {
      "Description" : "Elastic Load Balancer DNS name",
      "Value" :  {"Fn::GetAtt" : ["ELB", "DNSName"]}
    }
  }
}
