{
  "AWSTemplateFormatVersion" : "2010-09-09",

  "Description" : "AWS CloudFormation Sample Template WordPress_Single_Instance: WordPress is web software you can use to create a beautiful website or blog. This template installs WordPress with a local MySQL database for storage. It demonstrates using the AWS CloudFormation bootstrap scripts to deploy WordPress. **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" : {

    "KeyName": {
      "Description" : "Name of an existing EC2 KeyPair to enable SSH access to the instances",
      "Type": "AWS::EC2::KeyPair::KeyName",
      "ConstraintDescription" : "must be the name of an existing EC2 KeyPair."
    },

    "InstanceType" : {
      "Description" : "WebServer EC2 instance type",
      "Type" : "String",
      "Default" : "t3.large",
      "AllowedValues" : [  "a1.medium", "a1.large", "a1.xlarge", "a1.2xlarge", "a1.4xlarge", 
    "c5.large", "c5.xlarge", "c5.2xlarge", "c5.4xlarge", "c5.9xlarge", "c5.18xlarge",
    "t3.micro", "t3.small", "t3.medium", "t3.large", "t3.xlarge", "t3.2xlarge",
    "m5.large", "m5.xlarge", "m5.2xlarge", "m5.4xlarge", "m5.12xlarge", "m5.24xlarge",
    "r5.large", "r5.xlarge", "r5.2xlarge", "r5.4xlarge", "r5.12xlarge", "r5.24xlarge",
    "m5a.large", "m5a.xlarge", "m5a.2xlarge", "m5a.4xlarge", "m5a.12xlarge", "m5a.24xlarge",
    "r5a.large", "r5a.xlarge", "r5a.2xlarge", "r5a.4xlarge", "r5a.12xlarge", "r5a.24xlarge" ]

,
      "ConstraintDescription" : "must be a valid EC2 instance type."
    },

    "SSHLocation": {
      "Description": "The IP address range that can be used to SSH to the EC2 instances",
      "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 IP CIDR range of the form x.x.x.x/x."
    },

    "DBName" : {
      "Default": "wordpressdb",
      "Description" : "The WordPress database name",
      "Type": "String",
      "MinLength": "1",
      "MaxLength": "64",
      "AllowedPattern" : "[a-zA-Z][a-zA-Z0-9]*",
      "ConstraintDescription" : "must begin with a letter and contain only alphanumeric characters."
    },

    "DBUser" : {
      "NoEcho": "true",
      "Description" : "The WordPress database admin account username",
      "Type": "String",
      "MinLength": "1",
      "MaxLength": "16",
      "AllowedPattern" : "[a-zA-Z][a-zA-Z0-9]*",
      "ConstraintDescription" : "must begin with a letter and contain only alphanumeric characters."
    },

    "DBPassword" : {
      "NoEcho": "true",
      "Description" : "The WordPress database admin account password",
      "Type": "String",
      "MinLength": "8",
      "MaxLength": "41",
      "AllowedPattern" : "[a-zA-Z0-9]*",
      "ConstraintDescription" : "must contain only alphanumeric characters."
    },

    "DBRootPassword" : {
      "NoEcho": "true",
      "Description" : "MySQL root password",
      "Type": "String",
      "MinLength": "8",
      "MaxLength": "41",
      "AllowedPattern" : "[a-zA-Z0-9]*",
      "ConstraintDescription" : "must contain only alphanumeric characters."
    }
  },

  "Mappings" : {
    "AWSInstanceType2Arch" : {
      "a1.medium"     : { "Arch" : "AL2arm64" },
      "a1.large"      : { "Arch" : "AL2arm64" },
      "a1.xlarge"     : { "Arch" : "AL2arm64" },
      "a1.2xlarge"    : { "Arch" : "AL2arm64" },
      "a1.4xlarge"    : { "Arch" : "AL2arm64" },
      "t3.micro"      : { "Arch" : "AL2x86"  },
      "t3.small"      : { "Arch" : "AL2x86"  },
      "t3.medium"     : { "Arch" : "AL2x86"  },
      "t3.large"      : { "Arch" : "AL2x86"  },
      "t3.xlarge"     : { "Arch" : "AL2x86"  },
      "t3.2xlarge"    : { "Arch" : "AL2x86"  },
      "c5.large"      : { "Arch" : "AL2x86"  },
      "c5.xlarge"     : { "Arch" : "AL2x86"  },
      "c5.2xlarge"    : { "Arch" : "AL2x86"  },
      "c5.4xlarge"    : { "Arch" : "AL2x86"  },
      "c5.9xlarge"    : { "Arch" : "AL2x86"  },
      "c5.18xlarge"   : { "Arch" : "AL2x86"  },
      "m5.large"      : { "Arch" : "AL2x86"  },
      "m5.xlarge"     : { "Arch" : "AL2x86"  },
      "m5.2xlarge"    : { "Arch" : "AL2x86"  },
      "m5.4xlarge"    : { "Arch" : "AL2x86"  },
      "m5.12xlarge"   : { "Arch" : "AL2x86"  },
      "m5.24xlarge"   : { "Arch" : "AL2x86"  },
      "r5.large"      : { "Arch" : "AL2x86"  },
      "r5.xlarge"     : { "Arch" : "AL2x86"  },
      "r5.2xlarge"    : { "Arch" : "AL2x86"  },
      "r5.4xlarge"    : { "Arch" : "AL2x86"  },
      "r5.12xlarge"   : { "Arch" : "AL2x86"  },
      "r5.24xlarge"   : { "Arch" : "AL2x86"  },
      "m5a.large"     : { "Arch" : "AL2x86"  },
      "m5a.xlarge"    : { "Arch" : "AL2x86"  },
      "m5a.2xlarge"   : { "Arch" : "AL2x86"  },
      "m5a.4xlarge"   : { "Arch" : "AL2x86"  },
      "m5a.12xlarge"  : { "Arch" : "AL2x86"  },
      "m5a.24xlarge"  : { "Arch" : "AL2x86"  },
      "r5a.large"     : { "Arch" : "AL2x86"  },
      "r5a.xlarge"    : { "Arch" : "AL2x86"  },
      "r5a.2xlarge"   : { "Arch" : "AL2x86"  },
      "r5a.4xlarge"   : { "Arch" : "AL2x86"  },
      "r5a.12xlarge"  : { "Arch" : "AL2x86"  },
      "r5a.24xlarge"  : { "Arch" : "AL2x86"  }
    },


    "AWSRegionArch2AMI" : {
      "af-south-1"       : {"AL2x86" : "ami-0e0da87fdaf927717", "AL2arm64" : "ami-065d90114e5563b7c"},
      "ap-east-1"        : {"AL2x86" : "ami-05676b11acc4161d5", "AL2arm64" : "ami-01b3c77ba543f2d37"},
      "ap-northeast-1"   : {"AL2x86" : "ami-0e3ceefc9a200a838", "AL2arm64" : "ami-00d52b05e5d12f79a"},
      "ap-northeast-2"   : {"AL2x86" : "ami-085a10255c2ecb4cb", "AL2arm64" : "ami-00830b77b77093c6a"},
      "ap-northeast-3"   : {"AL2x86" : "ami-07795c2951dc11ce7", "AL2arm64" : "ami-0f7ace555021df0e1"},
      "ap-south-1"       : {"AL2x86" : "ami-020562d2cd769c81b", "AL2arm64" : "ami-00d12fc74288c3d02"},
      "ap-south-2"       : {"AL2x86" : "ami-09ec6c49d26d0518f", "AL2arm64" : "ami-0117f0f3a6394d2fb"},
      "ap-southeast-1"   : {"AL2x86" : "ami-044d69b8549e33ff1", "AL2arm64" : "ami-0384c70c348cae8c3"},
      "ap-southeast-2"   : {"AL2x86" : "ami-0e166015f8dee9d71", "AL2arm64" : "ami-02291da86850f876f"},
      "ap-southeast-3"   : {"AL2x86" : "ami-0f8159cc3ab5fffaf", "AL2arm64" : "ami-04efb6fc83d0d0e8c"},
      "ap-southeast-4"   : {"AL2x86" : "ami-04864bb4b01bd8038", "AL2arm64" : "ami-093057da2c3fbb346"},
      "il-central-1"     : {"AL2x86" : "ami-0f552ebe7c7bc7655", "AL2arm64" : "ami-00d86501feea7cdcb"},
      "ca-central-1"     : {"AL2x86" : "ami-09fa72aa5ea159b61", "AL2arm64" : "ami-00a83b65c29532f01"},
      "cn-north-1"       : {"AL2x86" : "ami-05bed6d0f634de167", "AL2arm64" : "ami-0e84f29cb39a69c7a"},
      "cn-northwest-1"   : {"AL2x86" : "ami-0ab2d03a762163a01", "AL2arm64" : "ami-0d3bcc6800743a385"},
      "eu-central-1"     : {"AL2x86" : "ami-0d2820455d08be628", "AL2arm64" : "ami-0f4103bc88cb0f4f9"},
      "eu-north-1"       : {"AL2x86" : "ami-0312b506c46eb9f64", "AL2arm64" : "ami-02dd950f6c0ff67c6"},
      "eu-south-1"       : {"AL2x86" : "ami-08a273089ab10bfda", "AL2arm64" : "ami-0f68372349e5ad993"},
      "eu-west-1"        : {"AL2x86" : "ami-052b48dff01bd3a2d", "AL2arm64" : "ami-0f234a8a091fb8dba"},
      "eu-west-2"        : {"AL2x86" : "ami-0db2efede9f8bb581", "AL2arm64" : "ami-03fd61e716f019dfc"},
      "eu-west-3"        : {"AL2x86" : "ami-05d9ff5247ae93752", "AL2arm64" : "ami-015c31cb07ec39e7d"},
      "me-south-1"       : {"AL2x86" : "ami-0a6e54be87919a76a", "AL2arm64" : "ami-037cf52e27b534dac"},
      "me-central-1"     : {"AL2x86" : "ami-0911d5289b0ceaafa", "AL2arm64" : "ami-0a54881bebc45facc"},
      "eu-south-2"       : {"AL2x86" : "ami-0295ec06d7ecf07a2", "AL2arm64" : "ami-0e7f4e6ca73a34586"},
      "eu-central-2"     : {"AL2x86" : "ami-0a37533ef40d2f340", "AL2arm64" : "ami-04f1f177f49d4d3b5"},
      "sa-east-1"        : {"AL2x86" : "ami-0df03a6401e7a2943", "AL2arm64" : "ami-038cfeaa7767efece"},
      "us-east-1"        : {"AL2x86" : "ami-0d887a308369b6881", "AL2arm64" : "ami-026924e4c3571e2ba"},
      "us-east-2"        : {"AL2x86" : "ami-0e534f3e5321ed9ed", "AL2arm64" : "ami-06ea85b3c51430945"},
      "us-west-1"        : {"AL2x86" : "ami-01cff12e087f492c6", "AL2arm64" : "ami-04618ab487317a0d0"},
      "us-west-2"        : {"AL2x86" : "ami-0044a0897b53acfb6", "AL2arm64" : "ami-0a08b4e3d317bc481"}
    }

  },

  "Resources" : {
    "WebServerSecurityGroup" : {
      "Type" : "AWS::EC2::SecurityGroup",
      "Properties" : {
        "GroupDescription" : "Enable HTTP access via port 80 locked down to the load balancer + SSH access",
        "SecurityGroupIngress" : [
          {"IpProtocol" : "tcp", "FromPort" : "80", "ToPort" : "80", "CidrIp" : "0.0.0.0/0"},
          {"IpProtocol" : "tcp", "FromPort" : "22", "ToPort" : "22", "CidrIp" : { "Ref" : "SSHLocation"}}
        ]
      }
    },

    "WebServer": {
      "Type" : "AWS::EC2::Instance",
      "Metadata" : {
        "AWS::CloudFormation::Init" : {
          "configSets" : {
            "wordpress_install" : ["install_cfn", "setup_extras", "install_wordpress", "configure_wordpress" ]
          },
          "install_cfn" : {
            "files": {
              "/etc/cfn/cfn-hup.conf": {
                "content": { "Fn::Join": [ "", [
                  "[main]\n",
                  "stack=", { "Ref": "AWS::StackId" }, "\n",
                  "region=", { "Ref": "AWS::Region" }, "\n"
                ]]},
                "mode"  : "000400",
                "owner" : "root",
                "group" : "root"
              },
              "/etc/cfn/hooks.d/cfn-auto-reloader.conf": {
                "content": { "Fn::Join": [ "", [
                  "[cfn-auto-reloader-hook]\n",
                  "triggers=post.update\n",
                  "path=Resources.WebServer.Metadata.AWS::CloudFormation::Init\n",
                  "action=/opt/aws/bin/cfn-init -v ",
                          "         --stack ", { "Ref" : "AWS::StackName" },
                          "         --resource WebServer ",
                          "         --configsets wordpress_install ",
                          "         --region ", { "Ref" : "AWS::Region" }, "\n"
                ]]},
                "mode"  : "000400",
                "owner" : "root",
                "group" : "root"
              }
            },
            "services" : {
              "sysvinit" : {
                "cfn-hup" : { "enabled" : "true", "ensureRunning" : "true",
                              "files" : ["/etc/cfn/cfn-hup.conf", "/etc/cfn/hooks.d/cfn-auto-reloader.conf"] }
              }
            }
          },

          "setup_extras" : {
            "commands" : {
              "01_setup_extras" : {
                "command" : "amazon-linux-extras install lamp-mariadb10.2-php7.2"
              }
            }
          },

          "install_wordpress" : {
            "packages" : {
              "yum" : {
                "php"            : [],
                "mariadb-server" : [],
                "httpd"          : []
              }
            },

            "sources" : {
              "/var/www/html" : "http://wordpress.org/latest.tar.gz"
            },
            "files" : {
              "/tmp/setup.mysql" : {
                "content" : { "Fn::Join" : ["", [
                  "CREATE DATABASE ", { "Ref" : "DBName" }, ";\n",
                  "CREATE USER '", { "Ref" : "DBUser" }, "'@'localhost' IDENTIFIED BY '", { "Ref" : "DBPassword" }, "';\n",
                  "GRANT ALL ON ", { "Ref" : "DBName" }, ".* TO '", { "Ref" : "DBUser" }, "'@'localhost';\n",
                  "FLUSH PRIVILEGES;\n"
                ]]},
                "mode"  : "000400",
                "owner" : "root",
                "group" : "root"
              },

              "/tmp/create-wp-config" : {
                "content" : { "Fn::Join" : [ "", [
                  "#!/bin/bash -xe\n",
                  "cp /var/www/html/wordpress/wp-config-sample.php /var/www/html/wordpress/wp-config.php\n",
                  "sed -i \"s/'database_name_here'/'",{ "Ref" : "DBName" }, "'/g\" wp-config.php\n",
                  "sed -i \"s/'username_here'/'",{ "Ref" : "DBUser" }, "'/g\" wp-config.php\n",
                  "sed -i \"s/'password_here'/'",{ "Ref" : "DBPassword" }, "'/g\" wp-config.php\n"
                ]]},
                "mode" : "000500",
                "owner" : "root",
                "group" : "root"
              }
            },
            "services" : {
              "sysvinit" : {
                "httpd"  : { "enabled" : "true", "ensureRunning" : "true" },
                "mariadb" : { "enabled" : "true", "ensureRunning" : "true" }
              }
            }
          },

          "configure_wordpress" : {
            "commands" : {
              "01_set_mysql_root_password" : {
                "command" : { "Fn::Join" : ["", ["mysqladmin -u root password '", { "Ref" : "DBRootPassword" }, "'"]]},
                "test" : { "Fn::Join" : ["", ["$(mysql ", { "Ref" : "DBName" }, " -u root --password='", { "Ref" : "DBRootPassword" }, "' >/dev/null 2>&1 </dev/null); (( $? != 0 ))"]]}
              },
              "02_create_database" : {
                "command" : { "Fn::Join" : ["", ["mysql -u root --password='", { "Ref" : "DBRootPassword" }, "' < /tmp/setup.mysql"]]},
                "test" : { "Fn::Join" : ["", ["$(mysql ", { "Ref" : "DBName" }, " -u root --password='", { "Ref" : "DBRootPassword" }, "' >/dev/null 2>&1 </dev/null); (( $? != 0 ))"]]}
              },
              "03_configure_wordpress" : {
                "command" : "/tmp/create-wp-config",
                "cwd" : "/var/www/html/wordpress"
              }
            }
          }
        }
      },
      "Properties": {
        "ImageId" : { "Fn::FindInMap" : [ "AWSRegionArch2AMI", { "Ref" : "AWS::Region" },
                          { "Fn::FindInMap" : [ "AWSInstanceType2Arch", { "Ref" : "InstanceType" }, "Arch" ] } ] },
        "InstanceType"   : { "Ref" : "InstanceType" },
        "SecurityGroups" : [ {"Ref" : "WebServerSecurityGroup"} ],
        "KeyName"        : { "Ref" : "KeyName" },
        "UserData" : { "Fn::Base64" : { "Fn::Join" : ["", [
                       "#!/bin/bash -xe\n",
                       "yum update -y aws-cfn-bootstrap\n",

                       "/opt/aws/bin/cfn-init -v ",
                       "         --stack ", { "Ref" : "AWS::StackName" },
                       "         --resource WebServer ",
                       "         --configsets wordpress_install ",
                       "         --region ", { "Ref" : "AWS::Region" }, "\n",

                       "/opt/aws/bin/cfn-signal -e $? ",
                       "         --stack ", { "Ref" : "AWS::StackName" },
                       "         --resource WebServer ",
                       "         --region ", { "Ref" : "AWS::Region" }, "\n"
        ]]}}
      },
      "CreationPolicy" : {
        "ResourceSignal" : {
          "Timeout" : "PT15M"
        }
      }
    }
  },

  "Outputs" : {
    "WebsiteURL" : {
      "Value" : { "Fn::Join" : ["", ["http://", { "Fn::GetAtt" : [ "WebServer", "PublicDnsName" ]}, "/wordpress" ]]},
      "Description" : "WordPress Website"
    }
  }
}
