
{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Description": "Flatcar Linux on EC2: https://kinvolk.io/docs/flatcar-container-linux/latest/installing/cloud/aws-ec2/",
  "Mappings" : {
      "RegionMap" : {
         "eu-central-1" : {
             "AMI" : "ami-0175ff2e90869b010"
         },
         "ap-northeast-1" : {
             "AMI" : "ami-0a83332afdee87626"
         },
         "ap-northeast-2" : {
             "AMI" : "ami-08cb873dbde1e551a"
         },
         "af-south-1" : {
             "AMI" : "ami-02ace936051e68d7b"
         },
         "ca-central-1" : {
             "AMI" : "ami-06f8f862099c391e3"
         },
         "ap-south-1" : {
             "AMI" : "ami-04157cdd1ed9216bb"
         },
         "sa-east-1" : {
             "AMI" : "ami-06f9af7a4a9833e5a"
         },
         "ap-southeast-1" : {
             "AMI" : "ami-029589b26d1fc2fa0"
         },
         "ap-southeast-2" : {
             "AMI" : "ami-0a706f7140fbf0e29"
         },
         "ap-southeast-3" : {
             "AMI" : "ami-011966d4c7642e869"
         },
         "us-east-1" : {
             "AMI" : "ami-0749a12240ca137bf"
         },
         "us-east-2" : {
             "AMI" : "ami-06e9d85833e9f32ab"
         },
         "us-west-2" : {
             "AMI" : "ami-0007a09e5c891d51a"
         },
         "us-west-1" : {
             "AMI" : "ami-08595d699ad706fe9"
         },
         "eu-west-1" : {
             "AMI" : "ami-00a9918479a5be094"
         },
         "eu-west-2" : {
             "AMI" : "ami-0bb30a9a0ca93299e"
         },
         "eu-west-3" : {
             "AMI" : "ami-046052b765cb72de5"
         },
         "eu-north-1" : {
             "AMI" : "ami-0b37b9297fc421a84"
         },
         "eu-south-1" : {
             "AMI" : "ami-0aebb29ab68c7797b"
         },
         "ap-east-1" : {
             "AMI" : "ami-05d78574bd68c1620"
         },
         "me-south-1" : {
             "AMI" : null
         }
      }
  },
  "Parameters": {
    "InstanceType" : {
      "Description" : "EC2 HVM instance type (m3.medium, etc).",
      "Type" : "String",
      "Default" : "m3.medium",
      "ConstraintDescription" : "Must be a valid EC2 HVM instance type."
    },
    "ClusterSize": {
      "Default": "3",
      "MinValue": "3",
      "MaxValue": "12",
      "Description": "Number of nodes in cluster (3-12).",
      "Type": "Number"
    },
    "DiscoveryURL": {
      "Description": "An unique etcd cluster discovery URL. Grab a new token from https://discovery.etcd.io/new?size=<your cluster size>",
      "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": {
    "FlatcarSecurityGroup": {
      "Type": "AWS::EC2::SecurityGroup",
      "Properties": {
        "GroupDescription": "Flatcar Linux SecurityGroup",
        "SecurityGroupIngress": [
          {"IpProtocol": "tcp", "FromPort": "22", "ToPort": "22", "CidrIp": {"Ref": "AllowSSHFrom"}}
        ]
      }
    },
    "Ingress4001": {
      "Type": "AWS::EC2::SecurityGroupIngress",
      "Properties": {
        "GroupName": {"Ref": "FlatcarSecurityGroup"}, "IpProtocol": "tcp", "FromPort": "4001", "ToPort": "4001", "SourceSecurityGroupId": {
          "Fn::GetAtt" : [ "FlatcarSecurityGroup", "GroupId" ]
        }
      }
    },
    "Ingress2379": {
      "Type": "AWS::EC2::SecurityGroupIngress",
      "Properties": {
        "GroupName": {"Ref": "FlatcarSecurityGroup"}, "IpProtocol": "tcp", "FromPort": "2379", "ToPort": "2379", "SourceSecurityGroupId": {
          "Fn::GetAtt" : [ "FlatcarSecurityGroup", "GroupId" ]
        }
      }
    },
    "Ingress2380": {
      "Type": "AWS::EC2::SecurityGroupIngress",
      "Properties": {
        "GroupName": {"Ref": "FlatcarSecurityGroup"}, "IpProtocol": "tcp", "FromPort": "2380", "ToPort": "2380", "SourceSecurityGroupId": {
          "Fn::GetAtt" : [ "FlatcarSecurityGroup", "GroupId" ]
        }
      }
    },
    "FlatcarServerAutoScale": {
      "Type": "AWS::AutoScaling::AutoScalingGroup",
      "Properties": {
        "AvailabilityZones": {"Fn::GetAZs": ""},
        "LaunchConfigurationName": {"Ref": "FlatcarServerLaunchConfig"},
        "MinSize": "3",
        "MaxSize": "12",
        "DesiredCapacity": {"Ref": "ClusterSize"},
        "Tags": [
            {"Key": "Name", "Value": { "Ref" : "AWS::StackName" }, "PropagateAtLaunch": true}
        ]
      }
    },
    "FlatcarServerLaunchConfig": {
      "Type": "AWS::AutoScaling::LaunchConfiguration",
      "Properties": {
        "ImageId" : { "Fn::FindInMap" : [ "RegionMap", { "Ref" : "AWS::Region" }, "AMI" ]},
        "InstanceType": {"Ref": "InstanceType"},
        "KeyName": {"Ref": "KeyPair"},
        "SecurityGroups": [{"Ref": "FlatcarSecurityGroup"}],
        "UserData" : { "Fn::Base64":
          { "Fn::Join": [ "", [
            "#cloud-config\n\n",
            "coreos:\n",
            "  etcd2:\n",
            "    discovery: ", { "Ref": "DiscoveryURL" }, "\n",
            "    advertise-client-urls: http://$", { "Ref": "AdvertisedIPAddress" }, "_ipv4:2379\n",
            "    initial-advertise-peer-urls: http://$", { "Ref": "AdvertisedIPAddress" }, "_ipv4:2380\n",
            "    listen-client-urls: http://0.0.0.0:2379,http://0.0.0.0:4001\n",
            "    listen-peer-urls: http://$", { "Ref": "AdvertisedIPAddress" }, "_ipv4:2380\n",
            "  units:\n",
            "    - name: etcd2.service\n",
            "      command: start\n",
            "    - name: fleet.service\n",
            "      command: start\n"
            ] ]
          }
        }
      }
    }
  }
}

