{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Description": "Restmail.net stack",
  "Parameters": {
    "KeyName": {
      "Description": "Name of an existing EC2 KeyPair for SSH access.",
      "Type": "AWS::EC2::KeyPair::KeyName",
      "ConstraintDescription": "must be the name of an existing EC2 KeyPair."
    },
    "StackName": {
      "Description": "Name for this Cloudformation stack.",
      "Type": "String"
    },
    "InstanceType": {
      "Description": "EC2 instance type",
      "Type": "String",
      "Default": "t2.small",
      "AllowedValues": [
        "t2.small",
        "t2.medium"
      ],
      "ConstraintDescription": "must be a valid EC2 instance type."
    },
    "HostedZone": {
      "Description": "The DNS name of an existing Amazon Route 53 hosted zone",
      "Type": "String",
      "AllowedPattern": "(?!-)[a-zA-Z0-9-.]{1,63}(?<!-)",
      "ConstraintDescription": "must be a valid DNS zone name."
    },
    "FQDN": {
      "Description": "Fully qualified domain name of this instance.",
      "Type": "String"
    }
  },

  "Mappings": {
    "RegionMap" : {
      "us-east-1"      : { "AMI" : "ami-c58c1dd3" },
      "us-east-2"      : { "AMI" : "ami-4191b524" },
      "us-west-1"      : { "AMI" : "ami-7a85a01a" },
      "us-west-2"      : { "AMI" : "ami-4836a428" },
      "ca-central-1"   : { "AMI" : "ami-0bd66a6f" },
      "eu-west-1"      : { "AMI" : "ami-01ccc867" },
      "eu-west-2"      : { "AMI" : "ami-b6daced2" },
      "eu-central-1"   : { "AMI" : "ami-b968bad6" },
      "ap-southeast-1" : { "AMI" : "ami-fc5ae39f" },
      "ap-southeast-2" : { "AMI" : "ami-162c2575" },
      "ap-northeast-1" : { "AMI" : "ami-923d12f5" },
      "ap-northeast-2" : { "AMI" : "ami-9d15c7f3" },
      "ap-south-1"     : { "AMI" : "ami-52c7b43d" },
      "sa-east-1"      : { "AMI" : "ami-37cfad5b" },
      "cn-north-1"     : { "AMI" : "ami-3fe13752" },
      "us-gov-west-1"  : { "AMI" : "ami-34e76355" }
    }
  },

  "Resources": {
    "EC2Instance": {
      "Type": "AWS::EC2::Instance",
      "Properties": {
        "InstanceType": { "Ref": "InstanceType" },
        "SecurityGroups": [ { "Ref": "InstanceSecurityGroup" } ],
        "KeyName": { "Ref": "KeyName" },
        "Tags": [
          {
            "Key" : "Name",
            "Value" : { "Ref": "StackName" }
          }
        ],
        "ImageId": { "Fn::FindInMap": [ "RegionMap", { "Ref": "AWS::Region" }, "AMI" ] }
      }
    },
    "InstanceSecurityGroup": {
      "Type": "AWS::EC2::SecurityGroup",
      "Properties": {
        "GroupDescription": "Enable access to port 22",
        "SecurityGroupIngress": [
          {
            "IpProtocol": "tcp",
            "FromPort": "22",
            "ToPort": "22",
            "CidrIp": "0.0.0.0/0"
          },
          {
            "IpProtocol": "tcp",
            "FromPort": "25",
            "ToPort": "25",
            "CidrIp": "0.0.0.0/0"
          },
          {
            "IpProtocol": "tcp",
            "FromPort": "80",
            "ToPort": "80",
            "CidrIp": "0.0.0.0/0"
          },
          {
            "IpProtocol": "tcp",
            "FromPort": "443",
            "ToPort": "443",
            "CidrIp": "0.0.0.0/0"
          }
        ]
      }
    },
    "EC2DNSARecord": {
      "Type": "AWS::Route53::RecordSet",
      "Properties": {
        "HostedZoneName": { "Fn::Join" : [ "", [ { "Ref": "HostedZone" }, "." ] ] },
        "Comment": "DNS name pointing to EC2 IP.",
        "Name": { "Ref": "FQDN" },
        "Type": "A",
        "TTL": "60",
        "ResourceRecords": [ { "Fn::GetAtt": [ "EC2Instance", "PublicIp" ] } ]
      }
    },
    "EC2DNSMXRecord": {
      "Type": "AWS::Route53::RecordSet",
      "Properties": {
        "HostedZoneName": { "Fn::Join" : [ "", [ { "Ref": "HostedZone" }, "." ] ] },
        "Comment": "MX record for this hostname.",
        "Name": { "Ref": "FQDN" },
        "Type": "MX",
        "TTL": "60",
        "ResourceRecords" : [ { "Fn::Join" : [ "", [ "10 ", { "Ref": "FQDN" } ] ] } ]
      }
    }
  },

  "Outputs": {
    "PublicDNS": {
      "Description": "Public DNSName",
      "Value": { "Ref": "EC2DNSARecord" }
    },
    "EC2HostName": {
      "Description": "Hostname assigned by EC2",
      "Value": { "Fn::GetAtt": [ "EC2Instance", "PublicDnsName" ] }
    },
    "InstanceId": {
      "Description": "InstanceId",
      "Value": { "Ref": "EC2Instance" }
    }
  }
}
