{
  "AWSTemplateFormatVersion" : "2010-09-09",

  "Description" : "AWS CloudFormation Sample Template EC2InstanceWithEBSVolumeConditionalIOPs: Example to show how to create an EC2 instance with an optionally attached volume. The instance is EBS Optimized and the volume is a PIOPs volume if the EC2 instance type supports it. **WARNING** This template creates an Amazon EC2 instance. You will be billed for the AWS resources used if you create a stack from this template.",

  "Parameters" : {
    "InstanceType" : {
      "Description" : "WebServer EC2 instance type",
      "Type" : "String",
      "Default" : "m1.small",
      "AllowedValues" : [ "t1.micro","m1.small","m1.medium","m1.large","m1.xlarge","m3.xlarge","m3.2xlarge","m2.xlarge","m2.2xlarge","m2.4xlarge","c1.medium","c1.xlarge","cc1.4xlarge","cc2.8xlarge","cg1.4xlarge","hi1.4xlarge","hs1.8xlarge"],
      "ConstraintDescription" : "must be a valid EC2 instance type."
    },

    "KeyName": {
      "Description" : "Name of an existing EC2 KeyPair to enable SSH access to the web server",
      "Type": "String",
      "MinLength": "1",
      "MaxLength": "255",
      "AllowedPattern" : "[\\x20-\\x7E]*",
      "ConstraintDescription" : "can contain only ASCII characters."
    },

    "SSHFrom": {
      "Description": "Lockdown SSH access to the bastion host (default can be accessed from anywhere)",
      "Type": "String",
      "MinLength": "9",
      "MaxLength": "18",
      "Default": "0.0.0.0/0",
      "AllowedPattern": "(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})/(\\d{1,2})",
      "ConstraintDescription": "must be a valid CIDR range of the form x.x.x.x/x."
    },

    "EBSVolumeDeviceName" : {
      "Description" : "Device name to attach an EBS volume (Default no EBS volume attached)",
      "Type" : "String",
      "Default" : "No Volume"
    },

    "VolumeSize" : {
      "Description" : "Size of the EBS volume if attached",
      "Type" : "Number",
      "Default" : "100",
      "MinValue" : "1",
      "MaxValue" : "1000"
    },

    "IOPs" : {
      "Description" : "Provisioned IOPs for EBS volume if supported by the instance type",
      "Type" : "Number",
      "Default" : "100",
      "MinValue" : "1",
      "MaxValue" : "30000"
    }
  },

  "Mappings" : {
    "InstanceConfig" : {
      "t1.micro"    : { "Arch" : "PV64",  "EBSOptimized" : "false" },

      "m1.small"    : { "Arch" : "PV64",  "EBSOptimized" : "false" },
      "m1.medium"   : { "Arch" : "PV64",  "EBSOptimized" : "false" },
      "m1.large"    : { "Arch" : "PV64",  "EBSOptimized" : "true"  },
      "m1.xlarge"   : { "Arch" : "PV64",  "EBSOptimized" : "true"  },

      "m3.xlarge"   : { "Arch" : "PV64",  "EBSOptimized" : "true"  },
      "m3.2xlarge"  : { "Arch" : "PV64",  "EBSOptimized" : "true"  },

      "m2.xlarge"   : { "Arch" : "PV64",  "EBSOptimized" : "false" },
      "m2.2xlarge"  : { "Arch" : "PV64",  "EBSOptimized" : "true"  },
      "m2.4xlarge"  : { "Arch" : "PV64",  "EBSOptimized" : "true"  },

      "c1.medium"   : { "Arch" : "PV64",  "EBSOptimized" : "false" },
      "c1.xlarge"   : { "Arch" : "PV64",  "EBSOptimized" : "true"  },

      "g2.2xlarge"  : { "Arch" : "GPU64", "EBSOptimized" : "true"  },

      "cc2.8xlarge" : { "Arch" : "PV64",  "EBSOptimized" : "false" },

      "cr1.8xlarge" : { "Arch" : "PV64",  "EBSOptimized" : "false" },

      "cg1.4xlarge" : { "Arch" : "GPU64", "EBSOptimized" : "false" },

      "hi1.4xlarge" : { "Arch" : "PV64",  "EBSOptimized" : "false" },

      "hs1.8xlarge" : { "Arch" : "PV64",  "EBSOptimized" : "false" }
    },

    "AWSRegionArch2AMI" : {
      "us-east-1"      : { "PV64" : "ami-35792c5c", "HVM64" : "ami-69792c00", "GPU64" : "ami-7f792c16" },
      "us-west-2"      : { "PV64" : "ami-d03ea1e0", "HVM64" : "ami-e43ea1d4", "GPU64" : "NOT_YET_SUPPORTED" },
      "us-west-1"      : { "PV64" : "ami-687b4f2d", "HVM64" : "ami-4e7b4f0b", "GPU64" : "NOT_YET_SUPPORTED" },
      "eu-west-1"      : { "PV64" : "ami-149f7863", "HVM64" : "ami-209f7857", "GPU64" : "ami-2c9f785b" },
      "ap-southeast-1" : { "PV64" : "ami-14f2b946", "HVM64" : "ami-6af2b938", "GPU64" : "NOT_YET_SUPPORTED" },
      "ap-southeast-2" : { "PV64" : "ami-a148d59b", "HVM64" : "ami-a948d593", "GPU64" : "NOT_YET_SUPPORTED" },
      "ap-northeast-1" : { "PV64" : "ami-3561fe34", "HVM64" : "ami-0961fe08", "GPU64" : "NOT_YET_SUPPORTED" },
      "sa-east-1"      : { "PV64" : "ami-9f6ec982", "HVM64" : "ami-9d6ec980", "GPU64" : "NOT_YET_SUPPORTED" },
      "us-gov-west-1"  : { "PV64" : "ami-cdef8bee", "HVM64" : "ami-cfef8bec", "GPU64" : "NOT_YET_SUPPORTED" }
    }
  },

  "Conditions" : {
    "IsEBSOptimized" : { "Fn::Equals" : [ { "Fn::FindInMap" : [ "InstanceConfig", { "Ref" : "InstanceType" }, "EBSOptimized" ]}, "true" ] },
    "AttachVolume" : { "Fn::Not" : [{ "Fn::Equals" : [ { "Ref" : "EBSVolumeDeviceName" }, "No Volume" ]}]}
  },

  "Resources" : {
    "EC2Instance" : {
      "Type" : "AWS::EC2::Instance", 
      "Properties" : {
        "ImageId" : { "Fn::FindInMap" : [ "AWSRegionArch2AMI", { "Ref" : "AWS::Region" }, { "Fn::FindInMap" : [ "InstanceConfig", { "Ref" : "InstanceType" }, "Arch" ] } ] },
        "KeyName" : { "Ref" : "KeyName" },
        "InstanceType" : { "Ref" : "InstanceType" },
        "SecurityGroups" : [{ "Ref" : "Ec2SecurityGroup" }],
        "EbsOptimized" : { "Fn::If" : ["IsEBSOptimized", "true", "false" ]}
      }
    },

    "Ec2SecurityGroup" : {
      "Type" : "AWS::EC2::SecurityGroup",
      "Properties" : {
        "GroupDescription" : "HTTP and SSH access",
        "SecurityGroupIngress" : [ {
          "IpProtocol" : "tcp", 
          "FromPort" : "22", "ToPort" : "22", 
          "CidrIp" : { "Ref" : "SSHFrom" }
        } ]
      }
    },

    "EBSVolume": {
      "Type": "AWS::EC2::Volume",
      "Condition" : "AttachVolume",
      "Properties": {
        "Size": { "Ref" : "VolumeSize" },
        "AvailabilityZone": { "Fn::GetAtt": [ "EC2Instance", "AvailabilityZone"] },
        "VolumeType" : { "Fn::If" : ["IsEBSOptimized", "io1", {"Ref" : "AWS::NoValue"}]},
        "Iops" : { "Fn::If" : ["IsEBSOptimized", { "Ref" : "IOPs" }, {"Ref" : "AWS::NoValue"}]}
      }
    },

    "EBSVolumeAttachment" : {
      "Type" : "AWS::EC2::VolumeAttachment",
      "Condition" : "AttachVolume",
      "Properties" : {
        "Device" : { "Ref" : "EBSVolumeDeviceName" },
        "InstanceId" : { "Ref" : "EC2Instance" },
        "VolumeId" : { "Ref" : "EBSVolume" }
      }
    }
  },

  "Outputs" : {
    "Instance" : {
      "Value" : { "Fn::GetAtt" : [ "EC2Instance", "PublicDnsName" ] }, 
      "Description" : "DNS Name of the newly created EC2 instance"
    },
    "Device" : {
      "Condition" : "AttachVolume",
      "Value" : { "Ref" : "EBSVolumeDeviceName" },
      "Description" : "Device name for the attached volume"
    },
    "EBSOptimized" : {
      "Condition" : "AttachVolume",
      "Value" : { "Fn::FindInMap" : [ "InstanceConfig", { "Ref" : "InstanceType" }, "EBSOptimized" ]},
      "Description" : "Is the attached volume a PIOPs volume"
    },
    "IOPs" : {
      "Condition" : "IsEBSOptimized",
      "Value" : { "Ref" : "IOPs" },
      "Description" : "IOPs configured for attached volume"
    }
  }
}
