Created
June 26, 2020 00:59
-
-
Save richardsonlima/aa7c1b30eafd14377eff37addd96fc3b to your computer and use it in GitHub Desktop.
CloudFormation-WordPress_Single_Instance_With_RDS.template
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"AWSTemplateFormatVersion" : "2010-09-09", | |
"Description" : "AWS CloudFormation Sample Template WordPress_Single_Instance_With_RDS: WordPress is web software you can use to create a website or blog. This template installs a single-instance WordPress deployment that uses an Amazon RDS database instance for storage. It demonstrates using the AWS CloudFormation bootstrap scripts to install packages and files when an instance is launched. **WARNING** This template creates an Amazon EC2 instance and an Amazon RDS 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 to the instances", | |
"Type": "String", | |
"MinLength": "1", | |
"MaxLength": "255", | |
"AllowedPattern" : "[\\x20-\\x7E]*", | |
"ConstraintDescription" : "can contain only ASCII characters." | |
}, | |
"InstanceType" : { | |
"Description" : "WebServer EC2 instance type", | |
"Type" : "String", | |
"Default" : "t1.micro", | |
"AllowedValues" : [ "t1.micro", "t2.micro", "t2.small", "t2.medium", "m3.medium", "m3.large", "m3.xlarge", "m3.2xlarge" ], | |
"ConstraintDescription" : "must be a valid EC2 instance type." | |
}, | |
"DBClass" : { | |
"Default" : "db.t1.micro", | |
"Description" : "Database instance class", | |
"Type" : "String", | |
"AllowedValues" : [ "db.t1.micro", "db.t2.micro", "db.t2.small", "db.t2.medium", "db.m3.medium", "db.m3.large", "db.m3.xlarge", "db.m3.2xlarge" ], | |
"ConstraintDescription" : "must select a valid database instance type." | |
}, | |
"DBName" : { | |
"Default": "wordpress", | |
"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." | |
}, | |
"DBUsername" : { | |
"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]*", | |
"ConstraintDescription" : "must begin with a letter and contain only alphanumeric characters." | |
}, | |
"DBPassword" : { | |
"Default": "password", | |
"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." | |
}, | |
"DBAllocatedStorage" : { | |
"Default": "5", | |
"Description" : "The size of the database (Gb)", | |
"Type": "Number", | |
"MinValue": "5", | |
"MaxValue": "1024", | |
"ConstraintDescription" : "must be between 5 and 1024Gb." | |
}, | |
"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." | |
} | |
}, | |
"Mappings" : { | |
"AWSInstanceType2Arch" : { | |
"t1.micro" : { "Arch" : "PV64" }, | |
"t2.micro" : { "Arch" : "HVM64" }, | |
"t2.small" : { "Arch" : "HVM64" }, | |
"t2.medium" : { "Arch" : "HVM64" }, | |
"m3.medium" : { "Arch" : "PV64" }, | |
"m3.large" : { "Arch" : "PV64" }, | |
"m3.xlarge" : { "Arch" : "PV64" }, | |
"m3.2xlarge" : { "Arch" : "PV64" } | |
}, | |
"AWSRegionArch2AMI" : { | |
"us-east-1" : { "PV64" : "ami-7c807d14", "HVM64" : "ami-76817c1e" }, | |
"us-west-2" : { "PV64" : "ami-1b3b462b", "HVM64" : "ami-d13845e1" }, | |
"us-west-1" : { "PV64" : "ami-a8d3d4ed", "HVM64" : "ami-f0d3d4b5" }, | |
"eu-west-1" : { "PV64" : "ami-672ce210", "HVM64" : "ami-892fe1fe" }, | |
"ap-southeast-1" : { "PV64" : "ami-56b7eb04", "HVM64" : "ami-a6b6eaf4" }, | |
"ap-northeast-1" : { "PV64" : "ami-25dd9324", "HVM64" : "ami-29dc9228" }, | |
"ap-southeast-2" : { "PV64" : "ami-6bf99c51", "HVM64" : "ami-d9fe9be3" }, | |
"sa-east-1" : { "PV64" : "ami-c7e649da", "HVM64" : "ami-c9e649d4" }, | |
"us-gov-west-1" : { "PV64" : "ami-ab4a2d88", "HVM64" : "ami-a54a2d86" }, | |
"cn-north-1" : { "PV64" : "ami-cab82af3", "HVM64" : "ami-ccb82af5" } | |
} | |
}, | |
"Resources" : { | |
"WebServer": { | |
"Type": "AWS::EC2::Instance", | |
"Metadata" : { | |
"AWS::CloudFormation::Init" : { | |
"config" : { | |
"packages" : { | |
"yum" : { | |
"httpd" : [], | |
"php" : [], | |
"php-mysql" : [] | |
} | |
}, | |
"sources" : { | |
"/var/www/html" : "http://wordpress.org/latest.tar.gz" | |
}, | |
"files" : { | |
"/var/www/html/wordpress/wp-config.php" : { | |
"content" : { "Fn::Join" : ["", [ | |
"<?php\n", | |
"define('DB_NAME', '", {"Ref" : "DBName"}, "');\n", | |
"define('DB_USER', '", {"Ref" : "DBUsername"}, "');\n", | |
"define('DB_PASSWORD', '", {"Ref" : "DBPassword" }, "');\n", | |
"define('DB_HOST', '", {"Fn::GetAtt" : ["DBInstance", "Endpoint.Address"]},"');\n", | |
"define('DB_CHARSET', 'utf8');\n", | |
"define('DB_COLLATE', '');\n" | |
]] }, | |
"mode" : "000644", | |
"owner" : "root", | |
"group" : "root" | |
} | |
}, | |
"services" : { | |
"sysvinit" : { | |
"httpd" : { "enabled" : "true", "ensureRunning" : "true" }, | |
"sendmail" : { "enabled" : "false", "ensureRunning" : "false" } | |
} | |
} | |
} | |
} | |
}, | |
"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\n", | |
"yum update -y aws-cfn-bootstrap\n", | |
"/opt/aws/bin/cfn-init -s ", { "Ref" : "AWS::StackId" }, " -r WebServer ", | |
" --region ", { "Ref" : "AWS::Region" }, "\n", | |
"/opt/aws/bin/cfn-signal -e $? '", { "Ref" : "WaitHandle" }, "'\n", | |
"# Setup correct file ownership\n", | |
"chown -R apache:apache /var/www/html/wordpress\n", | |
"# Add keys and salts to the config file\n", | |
"wp_config=/var/www/html/wordpress/wp-config.php\n", | |
"GET https://api.wordpress.org/secret-key/1.1/salt/ >> $wp_config\n", | |
"echo \"define('WPLANG' , '');\" >> $wp_config\n", | |
"echo \"define('WP_DEBUG' , false);\" >> $wp_config\n", | |
"echo \"\\$table_prefix = 'wp_';\" >> $wp_config\n", | |
"echo \"if ( !defined('ABSPATH') )\" >> $wp_config\n", | |
"echo \" define('ABSPATH', dirname(__FILE__) . '/');\" >> $wp_config\n", | |
"echo \"require_once(ABSPATH . 'wp-settings.php');\" >> $wp_config\n" | |
]]}} | |
} | |
}, | |
"WaitHandle" : { | |
"Type" : "AWS::CloudFormation::WaitConditionHandle" | |
}, | |
"WaitCondition" : { | |
"Type" : "AWS::CloudFormation::WaitCondition", | |
"DependsOn" : "WebServer", | |
"Properties" : { | |
"Handle" : {"Ref" : "WaitHandle"}, | |
"Timeout" : "600" | |
} | |
}, | |
"DBInstance" : { | |
"Type": "AWS::RDS::DBInstance", | |
"Properties": { | |
"DBName" : { "Ref" : "DBName" }, | |
"Engine" : "MySQL", | |
"MasterUsername" : { "Ref" : "DBUsername" }, | |
"DBInstanceClass" : { "Ref" : "DBClass" }, | |
"DBSecurityGroups" : [{ "Ref" : "DBSecurityGroup" }], | |
"AllocatedStorage" : { "Ref" : "DBAllocatedStorage" }, | |
"MasterUserPassword": { "Ref" : "DBPassword" } | |
} | |
}, | |
"DBSecurityGroup": { | |
"Type": "AWS::RDS::DBSecurityGroup", | |
"Properties": { | |
"DBSecurityGroupIngress": { "EC2SecurityGroupName": { "Ref": "WebServerSecurityGroup"} }, | |
"GroupDescription" : "Frontend Access" | |
} | |
}, | |
"WebServerSecurityGroup" : { | |
"Type" : "AWS::EC2::SecurityGroup", | |
"Properties" : { | |
"GroupDescription" : "Enable HTTP access via port 80 and SSH access", | |
"SecurityGroupIngress" : [ | |
{"IpProtocol" : "tcp", "FromPort" : "80", "ToPort" : "80", "CidrIp" : "0.0.0.0/0"}, | |
{"IpProtocol" : "tcp", "FromPort" : "22", "ToPort" : "22", "CidrIp" : { "Ref" : "SSHLocation"}} | |
] | |
} | |
} | |
}, | |
"Outputs" : { | |
"WebsiteURL" : { | |
"Value" : { "Fn::Join" : ["", ["http://", { "Fn::GetAtt" : [ "WebServer", "PublicDnsName" ]}, "/wordpress"]] }, | |
"Description" : "WordPress Website" | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment