text: {
    "AWSTemplateFormatVersion": "2010-09-09",
    "Description": "CoreOS on EC2: http://coreos.com/docs/running-coreos/cloud-providers/ec2/",
    "Mappings" : {
        "RegionMap" : {
            "us-east-1" : {
                "AMI" : "ami-4e715d26"
            }
        }
    },
    "Parameters": {
      "InstanceType" : {
        "Description" : "EC2 PV instance type (m3.medium, etc).",
        "Type" : "String",
        "Default" : "m3.medium",
        "ConstraintDescription" : "Must be a valid EC2 PV instance type."
      },
      "ClusterSize": {
        "Default": "3",
        "MinValue": "1",
        "MaxValue": "12",
        "Description": "Number of nodes in cluster (1-12).  3 or larger is recommended.",
        "Type": "Number"
      },
      "DiscoveryURL": {
        "Description": "An unique etcd cluster discovery URL. Grab a new token from https://discovery.etcd.io/new",
        "Type": "String"
      },
      "AdvertisedIPAddress": {
        "Description": "Use 'private' if your etcd cluster is within one region or 'public' if it spans regions or cloud providers.",
        "Default": "private",
        "AllowedValues": ["private", "public"],
        "Type": "String"
      },
      "AllowSSHFrom": {
        "Description": "The net block (CIDR) that SSH is available to.",
        "Default": "0.0.0.0/0",
        "Type": "String"
      },
      "KeyPair" : {
        "Description" : "The name of an EC2 Key Pair to allow SSH access to the instance.",
        "Type" : "String"
      }
    },
    "Resources": {
      "CoreOSSecurityGroup": {
        "Type": "AWS::EC2::SecurityGroup",
        "Properties": {
          "GroupDescription": "CoreOS SecurityGroup",
          "SecurityGroupIngress": [
            {"IpProtocol": "tcp", "FromPort": "22", "ToPort": "22", "CidrIp": {"Ref": "AllowSSHFrom"}}
          ]
        }
      },
      "Ingress4001": {
        "Type": "AWS::EC2::SecurityGroupIngress",
        "Properties": {
          "GroupName": {"Ref": "CoreOSSecurityGroup"}, "IpProtocol": "tcp", "FromPort": "4001", "ToPort": "4001", "SourceSecurityGroupId": {
            "Fn::GetAtt" : [ "CoreOSSecurityGroup", "GroupId" ] 
          }
        }
      },
      "Ingress7001": {
        "Type": "AWS::EC2::SecurityGroupIngress",
        "Properties": {
          "GroupName": {"Ref": "CoreOSSecurityGroup"}, "IpProtocol": "tcp", "FromPort": "7001", "ToPort": "7001", "SourceSecurityGroupId": {
            "Fn::GetAtt" : [ "CoreOSSecurityGroup", "GroupId" ] 
          } 
        }
      },
       
      "Ingress112xx": {
        "Type": "AWS::EC2::SecurityGroupIngress",
        "Properties": {
          "GroupName": {"Ref": "CoreOSSecurityGroup"}, "IpProtocol": "tcp", "FromPort": "11209", "ToPort": "11211", "SourceSecurityGroupId": {
            "Fn::GetAtt" : [ "CoreOSSecurityGroup", "GroupId" ] 
          } 
        }
      },
      "Ingress8091": {
        "Type": "AWS::EC2::SecurityGroupIngress",
        "Properties": {
          "GroupName": {"Ref": "CoreOSSecurityGroup"}, "IpProtocol": "tcp", "FromPort": "8091", "ToPort": "8091", "CidrIp": "0.0.0.0/0"
        }
      },
      "Ingress8092": {
        "Type": "AWS::EC2::SecurityGroupIngress",
        "Properties": {
          "GroupName": {"Ref": "CoreOSSecurityGroup"}, "IpProtocol": "tcp", "FromPort": "8092", "ToPort": "8092", "SourceSecurityGroupId": {
            "Fn::GetAtt" : [ "CoreOSSecurityGroup", "GroupId" ] 
          } 
        }
      },
      "Ingress4369": {
        "Type": "AWS::EC2::SecurityGroupIngress",
        "Properties": {
          "GroupName": {"Ref": "CoreOSSecurityGroup"}, "IpProtocol": "tcp", "FromPort": "4369", "ToPort": "4369", "SourceSecurityGroupId": {
            "Fn::GetAtt" : [ "CoreOSSecurityGroup", "GroupId" ] 
          } 
        }
      },
      "Ingress211xx": {
        "Type": "AWS::EC2::SecurityGroupIngress",
        "Properties": {
          "GroupName": {"Ref": "CoreOSSecurityGroup"}, "IpProtocol": "tcp", "FromPort": "21100", "ToPort": "21199", "SourceSecurityGroupId": {
            "Fn::GetAtt" : [ "CoreOSSecurityGroup", "GroupId" ] 
          } 
        }
      },
       
       
      "Ingress8484": {
        "Type": "AWS::EC2::SecurityGroupIngress",
        "Properties": {
          "GroupName": {"Ref": "CoreOSSecurityGroup"}, "IpProtocol": "tcp", "FromPort": "8484", "ToPort": "8484", "SourceSecurityGroupId": {
            "Fn::GetAtt" : [ "CoreOSSecurityGroup", "GroupId" ] 
          }
        }
      },
      "Ingress8423": {
        "Type": "AWS::EC2::SecurityGroupIngress",
        "Properties": {
          "GroupName": {"Ref": "CoreOSSecurityGroup"}, "IpProtocol": "tcp", "FromPort": "8423", "ToPort": "8423", "SourceSecurityGroupId": {
            "Fn::GetAtt" : [ "CoreOSSecurityGroup", "GroupId" ] 
          }
        }
      },
       
       
      "Ingress4985": {
        "Type": "AWS::EC2::SecurityGroupIngress",
        "Properties": {
          "GroupName": {"Ref": "CoreOSSecurityGroup"}, "IpProtocol": "tcp", "FromPort": "4985", "ToPort": "4985", "SourceSecurityGroupId": {
            "Fn::GetAtt" : [ "CoreOSSecurityGroup", "GroupId" ] 
          }
        }
      },
      "Ingress4984": {
        "Type": "AWS::EC2::SecurityGroupIngress",
        "Properties": {
          "GroupName": {"Ref": "CoreOSSecurityGroup"}, "IpProtocol": "tcp", "FromPort": "4984", "ToPort": "4984", "CidrIp": "0.0.0.0/0"
        }
      },
      "Ingress80": {
        "Type": "AWS::EC2::SecurityGroupIngress",
        "Properties": {
          "GroupName": {"Ref": "CoreOSSecurityGroup"}, "IpProtocol": "tcp", "FromPort": "80", "ToPort": "80", "CidrIp": "0.0.0.0/0"
        }
      },
       
       
      "CoreOSServerAutoScale": {
        "Type": "AWS::AutoScaling::AutoScalingGroup",
        "Properties": {
          "AvailabilityZones": {"Fn::GetAZs": ""},
          "LaunchConfigurationName": {"Ref": "CoreOSServerLaunchConfig"},
          "MinSize": "1",
          "MaxSize": "12",
          "DesiredCapacity": {"Ref": "ClusterSize"},
          "Tags": [
              {"Key": "Name", "Value": { "Ref" : "AWS::StackName" }, "PropagateAtLaunch": true}
          ]
        }
      },
      "CoreOSServerLaunchConfig": {
        "Type": "AWS::AutoScaling::LaunchConfiguration",
        "Properties": {
          "ImageId" : { "Fn::FindInMap" : [ "RegionMap", { "Ref" : "AWS::Region" }, "AMI" ]},
          "InstanceType": {"Ref": "InstanceType"},
          "KeyName": {"Ref": "KeyPair"},
          "SecurityGroups": [{"Ref": "CoreOSSecurityGroup"}],
          "BlockDeviceMappings": [{
              "DeviceName": "/dev/xvda",
              "Ebs" : {"VolumeSize": "20"}
          }],
          "UserData" : { "Fn::Base64":
            { "Fn::Join": [ "", [
              "#cloud-config\n\n",
              "write_files:\n",
              "  - path: /etc/systemd/system/docker.service.d/increase-ulimit.conf\n",
              "    owner: core:core\n",
              "    permissions: 0644\n",
              "    content: |\n",
              "      [Service]\n",
              "      LimitMEMLOCK=infinity\n",
              "  - path: /etc/systemd/system/fleet.socket.d/30-ListenStream.conf\n",
              "    owner: core:core\n",
              "    permissions: 0644\n",
              "    content: |\n",
              "      [Socket]\n",
              "      ListenStream=127.0.0.1:49153\n",
              "  - path: /opt/couchbase/var/.README\n",
              "    owner: core:core\n",
              "    permissions: 0644\n",
              "    content: |\n",
              "      Couchbase /opt/couchbase/var data volume in container mounted here\n",
              "  - path: /var/lib/cbfs/data/.README\n",
              "    owner: core:core\n",
              "    permissions: 0644\n",
              "    content: |\n",
              "      CBFS files are stored here\n",
              "  - path: /opt/bin/etcdctl-get-first\n",
              "    owner: core:core\n",
              "    permissions: 0744\n",
              "    content: |\n",
              "      etcdctl ls $1 | head -n1 | awk -F/ '{print $4}'\n",
              "  - path: /opt/bin/couchbase-server-ip\n",
              "    owner: core:core\n",
              "    permissions: 0744\n",
              "    content: |\n",
              "      MAX_ATTEMPTS=50\n",
              "      SLEEP_SECS=10\n",
              "      num_attempts=0\n",
              "      COUCHBASE_SERVER_IP=$(/opt/bin/etcdctl-get-first /couchbase.com/couchbase-node-state)\n",
              "      while [ -z \"$COUCHBASE_SERVER_IP\" ]; do\n",
              "        sleep $SLEEP_SECS\n",
              "        num_attempts=$((num_attempts+1))\n",
              "        if [[ \"$num_attempts\" -gt \"$MAX_ATTEMPTS\" ]]; then\n",
              "          echo \"Failed to get couchbase ip after $MAX_ATTEMPTS attempts\"\n",
              "          exit 1\n",
              "        fi\n",
              "        COUCHBASE_SERVER_IP=$(/opt/bin/etcdctl-get-first /couchbase.com/couchbase-node-state)\n",
              "      done\n",
              "      echo $COUCHBASE_SERVER_IP\n",
              "coreos:\n",
              "  etcd:\n",
              "    discovery: ", { "Ref": "DiscoveryURL" }, "\n",
              "    addr: $", { "Ref": "AdvertisedIPAddress" }, "_ipv4:4001\n",
              "    peer-addr: $", { "Ref": "AdvertisedIPAddress" }, "_ipv4:7001\n",
              "  units:\n",
              "    - name: etcd.service\n",
              "      command: start\n",
              "    - name: fleet.service\n",
              "      command: start\n",
              "    - name: docker.service\n",
              "      command: restart\n"
              ] ]
            }
          }
        }
      }
    }
  }
  