{
  "AWSTemplateFormatVersion": "2010-09-09",

  "Description": "PHP Hello World sample application that connects to an Amazon Relational Database Service database instance and displays information about the web server. **WARNING** This template creates one or more Amazon EC2 instances and an Amazon Relational Database Service database instance. You will be billed for the AWS resources used if you create a stack from this template.",

  "Parameters": {
    "KeyName": {
      "Type": "String",
      "Description" : "Name of an existing EC2 KeyPair to enable SSH access to the web server"
    },
    "DatabaseName": {
      "Default": "PHPSample",
      "Type": "String",
      "Description" : "Test database name",
      "AllowedPattern" : "[a-zA-Z][a-zA-Z0-9]*",
      "ConstraintDescription" : "must begin with a letter and contain only alphanumeric characters."
    },
    "DatabaseUser": {
      "Default": "admin",
      "NoEcho": "true",
      "Type": "String",
      "Description" : "Test database admin account name",
      "MinLength": "1",
      "MaxLength": "16",
      "AllowedPattern" : "[a-zA-Z][a-zA-Z0-9]*",
      "ConstraintDescription" : "must begin with a letter and contain only alphanumeric characters."
    },
    "DatabasePassword": {
      "Default": "admin",
      "NoEcho": "true",
      "Type": "String",
      "Description" : "Test database admin account password",
      "MinLength": "1",
      "MaxLength": "41",
      "AllowedPattern" : "[a-zA-Z0-9]*",
      "ConstraintDescription" : "must contain only alphanumeric characters."
    },
    "InstanceType": {
      "Default": "m1.small",
      "Type": "String",
      "Description" : "Type of EC2 instance for web server"
    },
    "OperatorEmail": {
      "Default": "nobody@amazon.com",
      "Description": "Email address to notify if there are any operational issues",
      "Type": "String"
    },
    "WebServerPort": {
      "Default": "8888",
      "Type": "Number",
      "MinValue": "1",
      "MaxValue": "65535",
      "Description" : "TCP/IP port for the web server"
    },
    "DatabasePort": {
      "Default": "3306",
      "Type": "Number",
      "MinValue": "1150",
      "MaxValue": "65535",
      "Description" : "TCP/IP port for the RDS database"
    }
  },
  "Resources": {
    "CPUAlarmHigh": {
      "Properties": {
        "EvaluationPeriods": "1",
        "Statistic": "Average",
        "Threshold": "10",
        "AlarmDescription": "Alarm if CPU too high or metric disappears indicating instance is down",
        "Period": "60",
        "AlarmActions": [
          {
            "Ref": "AlarmTopic"
          }
        ],
        "Namespace": "AWS/EC2",
        "InsufficientDataActions": [
          {
            "Ref": "AlarmTopic"
          }
        ],
        "Dimensions": [
          {
            "Name": "AutoScalingGroupName",
            "Value": {
              "Ref": "WebServerGroup"
            }
          }
        ],
        "ComparisonOperator": "GreaterThanThreshold",
        "MetricName": "CPUUtilization"
      },
      "Type": "AWS::CloudWatch::Alarm"
    },
    "TooManyUnhealthyHostsAlarm": {
      "Properties": {
        "EvaluationPeriods": "1",
        "Statistic": "Average",
        "Threshold": "0",
        "AlarmDescription": "Alarm if there are too many unhealthy hosts.",
        "Period": "60",
        "AlarmActions": [
          {
            "Ref": "AlarmTopic"
          }
        ],
        "Namespace": "AWS/ELB",
        "InsufficientDataActions": [
          {
            "Ref": "AlarmTopic"
          }
        ],
        "Dimensions": [
          {
            "Name": "LoadBalancerName",
            "Value": {
              "Ref": "ElasticLoadBalancer"
            }
          }
        ],
        "ComparisonOperator": "GreaterThanThreshold",
        "MetricName": "UnHealthyHostCount"
      },
      "Type": "AWS::CloudWatch::Alarm"
    },
    "EC2SecurityGroup": {
      "Properties": {
        "SecurityGroupIngress": [
          {
            "FromPort": "22",
            "CidrIp": "0.0.0.0/0",
            "ToPort": "22",
            "IpProtocol": "tcp"
          },
          {
            "FromPort": {
              "Ref": "WebServerPort"
            },
            "CidrIp": "0.0.0.0/0",
            "ToPort": {
              "Ref": "WebServerPort"
            },
            "IpProtocol": "tcp"
          }
        ],
        "GroupDescription": "HTTP and SSH access"
      },
      "Type": "AWS::EC2::SecurityGroup"
    },
    "RequestLatencyAlarmHigh": {
      "Properties": {
        "EvaluationPeriods": "1",
        "Statistic": "Average",
        "Threshold": "1",
        "AlarmDescription": "Alarm if there aren't any requests coming through",
        "Period": "60",
        "AlarmActions": [
          {
            "Ref": "AlarmTopic"
          }
        ],
        "Namespace": "AWS/ELB",
        "InsufficientDataActions": [
          {
            "Ref": "AlarmTopic"
          }
        ],
        "Dimensions": [
          {
            "Name": "LoadBalancerName",
            "Value": {
              "Ref": "ElasticLoadBalancer"
            }
          }
        ],
        "ComparisonOperator": "GreaterThanThreshold",
        "MetricName": "Latency"
      },
      "Type": "AWS::CloudWatch::Alarm"
    },
    "ElasticLoadBalancer": {
      "Properties": {
        "Listeners": [
          {
            "InstancePort": {
              "Ref": "WebServerPort"
            },
            "PolicyNames": [
              "p1"
            ],
            "Protocol": "HTTP",
            "LoadBalancerPort": "80"
          }
        ],
        "HealthCheck": {
          "HealthyThreshold": "2",
          "Timeout": "5",
          "Interval": "10",
          "UnhealthyThreshold": "5",
          "Target": {
            "Fn::Join": [
              "",
              [
                "HTTP:",
                {
                  "Ref": "WebServerPort"
                },
                "/"
              ]
            ]
          }
        },
        "AvailabilityZones": [
          {
            "Fn::Join": [
              "",
              [
                {
                  "Ref": "AWS::Region"
                },
                "a"
              ]
            ]
          }
        ],
        "LBCookieStickinessPolicy": [
          {
            "CookieExpirationPeriod": "30",
            "PolicyName": "p1"
          }
        ]
      },
      "Type": "AWS::ElasticLoadBalancing::LoadBalancer"
    },
    "DBSecurityGroup": {
      "Properties": {
        "DBSecurityGroupIngress": {
          "EC2SecurityGroupName": {
            "Ref": "EC2SecurityGroup"
          }
        },
        "GroupDescription": "database access"
      },
      "Type": "AWS::RDS::DBSecurityGroup"
    },
    "SampleDB": {
      "Properties": {
        "Engine": "MySQL",
        "DBName": {
          "Ref": "DatabaseName"
        },
        "Port": { "Ref": "DatabasePort" },
        "MultiAZ" : { "Fn::FindInMap" : [ "AWSRegionCapabilities", { "Ref" : "AWS::Region" }, "RDSMultiAZ"] },
        "MasterUsername": {
          "Ref": "DatabaseUser"
        },
        "DBInstanceClass": "db.m1.small",
        "DBSecurityGroups": [
          {
            "Ref": "DBSecurityGroup"
          }
        ],
        "AllocatedStorage": "5",
        "MasterUserPassword": {
          "Ref": "DatabasePassword"
        }
      },
      "Type": "AWS::RDS::DBInstance"
    },
    "WebServerGroup": {
      "Properties": {
        "LoadBalancerNames": [
          {
            "Ref": "ElasticLoadBalancer"
          }
        ],
        "MinSize": "1",
        "LaunchConfigurationName": {
          "Ref": "LaunchConfig"
        },
        "AvailabilityZones": [
          {
            "Fn::Join": [
              "",
              [
                {
                  "Ref": "AWS::Region"
                },
                "a"
              ]
            ]
          }
        ],
        "MaxSize": "3"
      },
      "Type": "AWS::AutoScaling::AutoScalingGroup"
    },
    "AlarmTopic": {
      "Properties": {
        "Subscription": [
          {
            "Endpoint": {
              "Ref": "OperatorEmail"
            },
            "Protocol": "email"
          }
        ]
      },
      "Type": "AWS::SNS::Topic"
    },
    "LaunchConfig": {
      "Properties": {
        "SecurityGroups": [
          {
            "Ref": "EC2SecurityGroup"
          }
        ],
        "ImageId": {
          "Fn::FindInMap": [
            "AWSRegionArch2AMI",
            {
              "Ref": "AWS::Region"
            },
            {
              "Fn::FindInMap": [
                "AWSInstanceType2Arch",
                {
                  "Ref": "InstanceType"
                },
                "Arch"
              ]
            }
          ]
        },
        "UserData": {
          "Fn::Base64": {
            "Fn::Join": [
              "",
              [
                "Database=",
                {
                  "Fn::GetAtt": [
                    "SampleDB",
                    "Endpoint.Address"
                  ]
                },
                ":",
                {
                  "Ref": "DatabasePort"
                },
                "&DBUser=",
                {
                  "Ref": "DatabaseUser"
                },
                "&DBPassword=",
                {
                  "Ref": "DatabasePassword"
                },
                "&WSPort=",
                {
                  "Ref": "WebServerPort"
                }
              ]
            ]
          }
        },
        "KeyName": {
          "Ref": "KeyName"
        },
        "InstanceType": {
          "Ref": "InstanceType"
        }
      },
      "Type": "AWS::AutoScaling::LaunchConfiguration"
    }
  },

  "Mappings" : {    "AWSInstanceType2Arch" : {
      "t1.micro"    : { "Arch" : "64" },
      "m1.small"    : { "Arch" : "32" },
      "m1.large"    : { "Arch" : "64" },
      "m1.xlarge"   : { "Arch" : "64" },
      "m2.xlarge"   : { "Arch" : "64" },
      "m2.2xlarge"  : { "Arch" : "64" },
      "m2.4xlarge"  : { "Arch" : "64" },
      "c1.medium"   : { "Arch" : "32" },
      "c1.xlarge"   : { "Arch" : "64" },
      "cc1.4xlarge" : { "Arch" : "64" }
    },
    "AWSRegionArch2AMI" : {
      "us-east-1" : { "32" : "ami-c813e0a1", "64" : "ami-c213e0ab" },
      "us-west-1" : { "32" : "ami-09c7974c", "64" : "ami-0bc7974e" },
      "eu-west-1" : { "32" : "ami-6bc2f61f", "64" : "ami-6fc2f61b" },
      "ap-southeast-1" : { "32" : "ami-70f28c22", "64" : "ami-72f28c20" },
      "ap-northeast-1" : { "32" : "ami-ac03a8ad", "64" : "ami-b003a8b1" }
    },
    "AWSRegionCapabilities" : {
      "us-east-1" :      { "RDSMultiAZ" : "true" },
      "us-west-1" :      { "RDSMultiAZ" : "true" },
      "eu-west-1" :      { "RDSMultiAZ" : "true" },
      "ap-southeast-1" : { "RDSMultiAZ" : "true" },
      "ap-northeast-1" : { "RDSMultiAZ" : "true" }
    }
  },

  "Outputs": {
    "URL": {
      "Value": {
        "Fn::Join": [
          "",
          [
            "http://",
            {
              "Fn::GetAtt": [
                "ElasticLoadBalancer",
                "DNSName"
              ]
            },
            "/"
          ]
        ]
      },
      "Description" : "URL of the website"
    }
  }
}
