{
  "AWSTemplateFormatVersion": "2010-09-09",

  "Description" : "WordPress is web software you can use to create a beautiful website or blog. This template creates a scalable WordPress installation using an Auto Scaling group behind an Elastic Load Balancer along with an Amazon Relational Database Service database instance to store the content. **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": {
      "Description" : "Name of an existing EC2 KeyPair to enable SSH access into the WordPress web server",
      "Type": "String"
    },
    "WordPressDBName": {
      "Default": "wordpress",
      "Description" : "The WordPress database name",
      "Type": "String",
      "MinLength": "1",
      "MaxLength": "64",
      "AllowedPattern" : "[^\\x00\\\\/.]*[^\\x00\\\\/. ]"
    },
    "WordPressUser": {
      "Default": "admin",
      "NoEcho": "true",
      "Description" : "The WordPress database admin account username",
      "Type": "String",
      "MinLength": "1",
      "MaxLength": "16",
      "AllowedPattern" : "[a-zA-Z][a-zA-Z0-9]*"
    },
    "WordPressPwd": {
      "Default": "admin",
      "NoEcho": "true",
      "Description" : "The WordPress database admin account password",
      "Type": "String",
      "MinLength": "1",
      "MaxLength": "41",
      "AllowedPattern" : "[a-zA-Z0-9]*"
    },
    "GroupSize": {
      "Default": "1",
      "Description" : "The initial number of EC2 instances for the WordPress web server",
      "Type": "Number",
      "MinValue": "0"
    },
    "InstanceType": {
      "Default": "m1.small",
      "Description" : "The type of EC2 instances used for the WordPress web server",
      "Type": "String",
      "AllowedPattern" : "[a-zA-Z0-9\\.]+"
    },
    "OperatorEmail": {
      "Default": "nobody@amazon.com",
      "Description": "Email address to notify if there are any operational issues",
      "Type": "String"
    },
    "WordPressDBPort": {
      "Default": "3306",
      "Description" : "TCP/IP port for the WordPress database",
      "Type": "Number",
      "MinValue": "1150",
      "MaxValue": "65535"
    },
    "WebServerPort": {
      "Default": "8888",
      "Description" : "TCP/IP port for the WordPress web server",
      "Type": "Number",
      "MinValue": "1",
      "MaxValue": "65535"
    }
  },
  "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"
                },
                "/wp-admin/install.php"
              ]
            ]
          }
        },
        "AvailabilityZones": {
          "Fn::GetAZs": {
            "Ref": "AWS::Region"
          }
        },
        "LBCookieStickinessPolicy": [
          {
            "CookieExpirationPeriod": "30",
            "PolicyName": "p1"
          }
        ]
      },
      "Type": "AWS::ElasticLoadBalancing::LoadBalancer"
    },
    "DBSecurityGroup": {
      "Properties": {
        "DBSecurityGroupIngress": {
          "EC2SecurityGroupName": {
            "Ref": "EC2SecurityGroup"
          }
        },
        "GroupDescription": "database access"
      },
      "Type": "AWS::RDS::DBSecurityGroup"
    },
    "WordPressDB": {
      "Properties": {
        "Engine": "MySQL",
        "DBName": {
          "Ref": "WordPressDBName"
        },
        "Port": { "Ref": "WordPressDBPort" },
        "MultiAZ" : { "Fn::FindInMap" : [ "AWSRegionCapabilities", { "Ref" : "AWS::Region" }, "RDSMultiAZ"] },
        "MasterUsername": {
          "Ref": "WordPressUser"
        },
        "DBInstanceClass": "db.m1.small",
        "DBSecurityGroups": [
          {
            "Ref": "DBSecurityGroup"
          }
        ],
        "AllocatedStorage": "5",
        "MasterUserPassword": {
          "Ref": "WordPressPwd"
        }
      },
      "Type": "AWS::RDS::DBInstance"
    },
    "WebServerGroup": {
      "Properties": {
        "LoadBalancerNames": [
          {
            "Ref": "ElasticLoadBalancer"
          }
        ],
        "LaunchConfigurationName": {
          "Ref": "LaunchConfig"
        },
        "AvailabilityZones": {
          "Fn::GetAZs": {
            "Ref": "AWS::Region"
          }
        },
        "MinSize": "0",
        "MaxSize": "3",
        "DesiredCapacity" : "1",
        "NotificationConfiguration" : {
           "TopicARN" : { "Ref" : "AlarmTopic" },
           "NotificationTypes" : [ "autoscaling:EC2_INSTANCE_LAUNCH",
                                   "autoscaling:EC2_INSTANCE_LAUNCH_ERROR",
                                   "autoscaling:EC2_INSTANCE_TERMINATE", 
                                   "autoscaling:EC2_INSTANCE_TERMINATE_ERROR"]
        }          
      },
      "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": [
              ":",
              [
                {
                  "Ref": "WordPressDBName"
                },
                {
                  "Ref": "WordPressUser"
                },
                {
                  "Ref": "WordPressPwd"
                },
                {
                  "Ref": "WordPressDBPort"
                },
                {
                  "Fn::GetAtt": [
                    "WordPressDB",
                    "Endpoint.Address"
                  ]
                },
                {
                  "Ref": "WebServerPort"
                },
                {
                  "Fn::GetAtt": [
                    "ElasticLoadBalancer",
                    "DNSName"
                  ]
                }
              ]
            ]
          }
        },
        "KeyName": {
          "Ref": "KeyName"
        },
        "InstanceType": {
          "Ref": "InstanceType"
        }
      },
      "Type": "AWS::AutoScaling::LaunchConfiguration"
    }
  },
  "Description": "WordPress is web software you can use to create a beautiful website or blog.",
  "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-f417e49d", "64" : "ami-f617e49f" },
      "us-west-1" : { "32" : "ami-bdc797f8", "64" : "ami-bfc797fa" },
      "eu-west-1" : { "32" : "ami-a1c2f6d5", "64" : "ami-a3c2f6d7" },
      "ap-southeast-1" : { "32" : "ami-2cf28c7e", "64" : "ami-2ef28c7c" },
      "ap-northeast-1" : { "32" : "ami-cc03a8cd", "64" : "ami-d203a8d3" }
    },
    "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": {
    "InstallURL": {
      "Value": {
        "Fn::Join": [
          "",
          [
            "http://",
            {
              "Fn::GetAtt": [
                "ElasticLoadBalancer",
                "DNSName"
              ]
            },
            "/wp-admin/install.php"
          ]
        ]
      },
      "Description" : "Installation URL of the WordPress website"
    },
    "WebsiteURL": {
      "Value": {
        "Fn::Join": [
          "",
          [
            "http://",
            {
              "Fn::GetAtt": [
                "ElasticLoadBalancer",
                "DNSName" ]
            }
          ]
        ]
      }
    }
  }
}
