Skip to content

Instantly share code, notes, and snippets.

@Noppy
Last active March 15, 2017 22:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Noppy/b0dd7a2e8e6ba4e326d461b21b318df6 to your computer and use it in GitHub Desktop.
Save Noppy/b0dd7a2e8e6ba4e326d461b21b318df6 to your computer and use it in GitHub Desktop.
{
"AWSTemplateFormatVersion": "2010-09-09",
"Description" : "This template installs a WordPress deployment using a multi-az Amazon RDS database instance for storage. It demonstrates using the AWS CloudFormation bootstrap scripts to install packages and files at instance launch time. ",
"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."
},
"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": {
"Default": "wpadmin",
"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": "password0",
"Description": "The WordPress database admin account password",
"Type": "String",
"MinLength": "8",
"MaxLength": "41",
"AllowedPattern": "[a-zA-Z0-9]*",
"ConstraintDescription": "must contain only alphanumeric characters."
},
"DBMasterUser": {
"Default": "master",
"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."
},
"DBMasterPassword": {
"Default": "password00",
"Description": "MySQL root password",
"Type": "String",
"MinLength": "8",
"MaxLength": "41",
"AllowedPattern": "[a-zA-Z0-9]*",
"ConstraintDescription": "must contain only alphanumeric characters."
},
"MultiAZDatabase": {
"Default": "false",
"Description" : "Create a multi-AZ MySQL Amazon RDS database instance",
"Type": "String",
"AllowedValues" : [ "true", "false" ],
"ConstraintDescription" : "must be either true or false."
}
},
"Resources": {
"MainVPC": {
"Type": "AWS::EC2::VPC",
"Properties": {
"CidrBlock": "10.0.0.0/16",
"EnableDnsSupport": "true",
"EnableDnsHostnames": "true",
"InstanceTenancy": "default",
"Tags": [
{
"Key" : "Application",
"Value" : { "Ref" : "AWS::StackName"}
}
]
}
},
"InetGW": {
"Type": "AWS::EC2::InternetGateway",
"Properties": {
"Tags" : [
{
"Key" : "Application",
"Value" : { "Ref" : "AWS::StackName"}
}
]
},
"DependsOn": [
"MainVPC"
]
},
"AttachInetGW": {
"Type": "AWS::EC2::VPCGatewayAttachment",
"Properties": {
"InternetGatewayId": {
"Ref": "InetGW"
},
"VpcId": {
"Ref": "MainVPC"
}
},
"DependsOn": [
"MainVPC",
"InetGW"
]
},
"PrivateAsub": {
"Type": "AWS::EC2::Subnet",
"Properties": {
"VpcId": {
"Ref": "MainVPC"
},
"CidrBlock": "10.0.11.0/24",
"AvailabilityZone": {
"Fn::Select": [
"0",
{
"Fn::GetAZs": {
"Ref": "AWS::Region"
}
}
]
},
"Tags": [
{
"Key" : "Application",
"Value" : { "Ref" : "AWS::StackName"}
},
{
"Key" : "Name",
"Value" : "PrivateAsub"
}, {
"Key" : "Network",
"Value" : "Private"
}
]
},
"DependsOn": [
"MainVPC"
]
},
"PrivateBsub": {
"Type": "AWS::EC2::Subnet",
"Properties": {
"VpcId": {
"Ref": "MainVPC"
},
"CidrBlock": "10.0.12.0/24",
"AvailabilityZone": {
"Fn::Select": [
"1",
{
"Fn::GetAZs": {
"Ref": "AWS::Region"
}
}
]
},
"Tags": [
{
"Key" : "Application",
"Value" : { "Ref" : "AWS::StackName"}
},
{
"Key" : "Name",
"Value" : "PrivateBsub"
},
{
"Key" : "Network",
"Value" : "Private"
}
]
},
"DependsOn": [
"MainVPC"
]
},
"PubAsub": {
"Type": "AWS::EC2::Subnet",
"Properties": {
"VpcId": {
"Ref": "MainVPC"
},
"CidrBlock": "10.0.1.0/24",
"AvailabilityZone": {
"Fn::Select": [
"0",
{
"Fn::GetAZs": {
"Ref": "AWS::Region"
}
}
]
},
"Tags": [
{
"Key" : "Application",
"Value" : { "Ref" : "AWS::StackName"}
},
{
"Key" : "Name",
"Value" : "PubAsub"
}, {
"Key" : "Network",
"Value" : "Public"
}
]
},
"DependsOn": [
"MainVPC"
]
},
"PubBsub": {
"Type": "AWS::EC2::Subnet",
"Properties": {
"VpcId": {
"Ref": "MainVPC"
},
"CidrBlock": "10.0.2.0/24",
"AvailabilityZone": {
"Fn::Select": [
"1",
{
"Fn::GetAZs": {
"Ref": "AWS::Region"
}
}
]
},
"Tags": [
{
"Key" : "Application",
"Value" : { "Ref" : "AWS::StackName"}
},
{
"Key" : "Name",
"Value" : "PubBsub"
},
{
"Key" : "Network",
"Value" : "Public"
}
]
},
"DependsOn": [
"MainVPC"
]
},
"InternetRouteTable": {
"Type": "AWS::EC2::RouteTable",
"Properties": {
"VpcId": {
"Ref": "MainVPC"
},
"Tags": [
{
"Key" : "Application",
"Value" : { "Ref" : "AWS::StackName"}
}
]
},
"DependsOn": [
"MainVPC"
]
},
"InternetRote": {
"Type": "AWS::EC2::Route",
"Properties": {
"RouteTableId": {
"Ref": "InternetRouteTable"
},
"DestinationCidrBlock" : "0.0.0.0/0",
"GatewayId": {
"Ref": "InetGW"
}
},
"DependsOn": [
"AttachInetGW"
]
},
"PubSubRouteTableAssociationA" : {
"Type" : "AWS::EC2::SubnetRouteTableAssociation",
"Properties" : {
"SubnetId" : { "Ref" : "PubAsub" },
"RouteTableId" : { "Ref" : "InternetRouteTable" }
}
},
"PubSubRouteTableAssociationB" : {
"Type" : "AWS::EC2::SubnetRouteTableAssociation",
"Properties" : {
"SubnetId" : { "Ref" : "PubBsub" },
"RouteTableId" : { "Ref" : "InternetRouteTable" }
}
},
"ELB" : {
"Type" : "AWS::ElasticLoadBalancing::LoadBalancer",
"Metadata" : {
"Comment1" : "Configure the Load Balancer with a simple health check and cookie-based stickiness",
"Comment2" : "Use install path for healthcheck to avoid redirects - ELB healthcheck does not handle 302 return codes"
},
"Properties" : {
"Subnets" : [
{ "Ref" : "PubAsub" },
{ "Ref" : "PubBsub" }
],
"Instances" : [
{ "Ref" : "WPressWebSrv1" },
{ "Ref" : "WPressWebSrv2" }
],
"LBCookieStickinessPolicy" : [ {
"PolicyName" : "CookieBasedPolicy",
"CookieExpirationPeriod" : "30"
} ],
"Listeners" : [ {
"LoadBalancerPort" : "80",
"InstancePort" : "80",
"Protocol" : "HTTP",
"PolicyNames" : [ "CookieBasedPolicy" ]
} ],
"HealthCheck" : {
"Target" : "HTTP:80/wordpress/wp-admin/install.php",
"HealthyThreshold" : "2",
"UnhealthyThreshold" : "5",
"Interval" : "10",
"Timeout" : "5"
},
"CrossZone" : "false",
"SecurityGroups" : [
{ "Ref" : "WebSecurityGroup" }
]
}
},
"RDSSubnetGroup" : {
"Type" : "AWS::RDS::DBSubnetGroup",
"Properties" : {
"DBSubnetGroupDescription" : "description",
"SubnetIds" : [{ "Ref" : "PrivateAsub" },{ "Ref" : "PrivateBsub" }],
"Tags" : [{ "Key" : "Name", "Value" : "cloudformation_RDS_DBsubnet" }]
}
},
"WebSecurityGroup": {
"Type": "AWS::EC2::SecurityGroup",
"Properties": {
"VpcId": {
"Ref": "MainVPC"
},
"GroupDescription": "Allow access from HTTP and HTTPS traffic",
"SecurityGroupIngress": [
{
"IpProtocol": "tcp",
"FromPort": "80",
"ToPort": "80",
"CidrIp": "0.0.0.0/0"
},
{
"IpProtocol": "tcp",
"FromPort": "443",
"ToPort": "443",
"CidrIp": "0.0.0.0/0"
}
]
},
"DependsOn": [
"MainVPC"
]
},
"SSHSecurityGroup": {
"Type": "AWS::EC2::SecurityGroup",
"Properties": {
"VpcId": {
"Ref": "MainVPC"
},
"GroupDescription": "Allow access from SSH traffic",
"SecurityGroupIngress": [
{
"IpProtocol": "tcp",
"FromPort": "22",
"ToPort": "22",
"CidrIp": "0.0.0.0/0"
}
]
},
"DependsOn": [
"MainVPC"
]
},
"RDSSecurityGroup" : {
"Type" : "AWS::EC2::SecurityGroup",
"Properties" : {
"VpcId" : {
"Ref" : "MainVPC"
},
"GroupDescription" : "Enable RDS access via port 3306",
"SecurityGroupIngress" : [
{ "IpProtocol" : "tcp", "FromPort" : "3306", "ToPort" : "3306", "CidrIp" : "10.0.1.0/24" },
{ "IpProtocol" : "tcp", "FromPort" : "3306", "ToPort" : "3306", "CidrIp" : "10.0.2.0/24" }
],
"Tags" : [
{ "Key" : "Name", "Value" : "cloudformation_vpc_rds_security" }
]
}
},
"WPressWebSrv1" : {
"Type" : "AWS::EC2::Instance",
"Properties" : {
"ImageId" : "ami-56d4ad31",
"InstanceType" : "t2.micro",
"KeyName" : { "Ref" : "KeyName" },
"NetworkInterfaces": [
{
"AssociatePublicIpAddress": "true",
"DeviceIndex": "0",
"SubnetId" : { "Ref" : "PubAsub" },
"GroupSet": [
{ "Ref" : "SSHSecurityGroup" },
{ "Ref" : "WebSecurityGroup" }
]
}
],
"Tags" : [
{
"Key" : "Application",
"Value" : { "Ref" : "AWS::StackName"}
},
{
"Key" : "Name",
"Value" : "WPressWeb1"
}
],
"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 WPressWebSrv1 ",
" --configsets wordpress_install_with_createdb ",
" --region ",
{
"Ref": "AWS::Region"
},
"\n",
"/opt/aws/bin/cfn-signal -e $? ",
" --stack ",
{
"Ref": "AWS::StackName"
},
" --resource WPressWebSrv1 ",
" --region ",
{
"Ref": "AWS::Region"
},
"\n"
]
]
}
}
},
"Metadata": {
"AWS::CloudFormation::Init": {
"configSets": {
"wordpress_install_with_createdb": [
"install_cfn",
"install_wordpress",
"configure_wordpress_dbcreate",
"configure_wordpress_update_config"
],
"wordpress_install" : [
"install_cfn",
"install_wordpress",
"configure_wordpress_update_config"
]
},
"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.WPressWebSrv1.Metadata.AWS::CloudFormation::Init\n",
"action=/opt/aws/bin/cfn-init -v ",
" --stack ",
{
"Ref": "AWS::StackName"
},
" --resource WPressWebSrv1 ",
" --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"
]
}
}
}
},
"install_wordpress": {
"packages": {
"yum": {
"php": [],
"php-mysql": [],
"httpd": [],
"mysql": []
}
},
"sources": {
"/var/www/html": "http://wordpress.org/latest.tar.gz"
},
"files": {
"/tmp/setup.mysql": {
"content": {
"Fn::Join": [
"",
[
"CREATE USER '",
{
"Ref": "DBUser"
},
"'@'%' IDENTIFIED BY '",
{
"Ref": "DBPassword"
},
"';\n",
"GRANT ALL ON ",
{
"Ref": "DBName"
},
".* TO '",
{
"Ref": "DBUser"
},
"'@'%';\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",
"sed -i \"s/'localhost'/'",
{
"Fn::GetAtt" : ["DBInstance", "Endpoint.Address"]
},
"'/g\" wp-config.php\n"
]
]
},
"mode": "000500",
"owner": "root",
"group": "root"
}
},
"services": {
"sysvinit": {
"httpd": {
"enabled": "true",
"ensureRunning": "true"
}
}
}
},
"configure_wordpress_dbcreate": {
"commands": {
"01_create_database": {
"command": {
"Fn::Join": [
"",
[
"mysql -u ",
{
"Ref": "DBMasterUser"
},
" -h ",
{
"Fn::GetAtt" : ["DBInstance", "Endpoint.Address"]
},
" --password='",
{
"Ref": "DBMasterPassword"
},
"' < /tmp/setup.mysql"
]
]
}
}
}
},
"configure_wordpress_update_config" : {
"commands": {
"02_configure_wordpress": {
"command": "/tmp/create-wp-config",
"cwd": "/var/www/html/wordpress"
}
}
}
}
},
"CreationPolicy": {
"ResourceSignal": {
"Timeout": "PT15M"
}
},
"DependsOn" : [ "DBInstance" ]
},
"WPressWebSrv2" : {
"Type" : "AWS::EC2::Instance",
"Properties" : {
"ImageId" : "ami-56d4ad31",
"InstanceType" : "t2.micro",
"KeyName" : { "Ref" : "KeyName" },
"NetworkInterfaces": [
{
"AssociatePublicIpAddress": "true",
"DeviceIndex": "0",
"SubnetId" : { "Ref" : "PubBsub" },
"GroupSet": [
{ "Ref" : "SSHSecurityGroup" },
{ "Ref" : "WebSecurityGroup" }
]
}
],
"Tags" : [
{
"Key" : "Application",
"Value" : { "Ref" : "AWS::StackName"}
},
{
"Key" : "Name",
"Value" : "WPressWeb2"
}
],
"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 WPressWebSrv1 ",
" --configsets wordpress_install ",
" --region ",
{
"Ref": "AWS::Region"
},
"\n",
"/opt/aws/bin/cfn-signal -e $? ",
" --stack ",
{
"Ref": "AWS::StackName"
},
" --resource WPressWebSrv2 ",
" --region ",
{
"Ref": "AWS::Region"
},
"\n"
]
]
}
}
},
"CreationPolicy": {
"ResourceSignal": {
"Timeout": "PT15M"
}
},
"DependsOn" : [ "DBInstance" ]
},
"DBInstance" : {
"Type": "AWS::RDS::DBInstance",
"Properties": {
"DBName" : { "Ref" : "DBName" },
"Engine" : "MySQL",
"DBInstanceClass" : "db.t2.micro",
"Port" : "3306",
"AllocatedStorage" : "5",
"MultiAZ" : { "Ref" : "MultiAZDatabase" },
"DBSubnetGroupName" : { "Ref" : "RDSSubnetGroup" },
"VPCSecurityGroups" : [{ "Ref" : "RDSSecurityGroup" }],
"MasterUsername" : { "Ref" : "DBMasterUser" },
"MasterUserPassword": { "Ref" : "DBMasterPassword" },
"BackupRetentionPeriod" : "0"
}
}
},
"Outputs": {
"ELBURL": {
"Description" : "URL of Internet ELB",
"Value": {
"Fn::Join": [
"",
[
"http://",
{
"Fn::GetAtt": [
"ELB",
"DNSName"
]
},
"/wordpress"
]
]
}
},
"Web1URL" : {
"Description" : "URL of Web#1 Server",
"Value": {
"Fn::Join": [
"",
[
"http://",
{
"Fn::GetAtt": [
"WPressWebSrv1",
"PublicDnsName"
]
},
"/wordpress"
]
]
}
},
"Web2URL" : {
"Description": "WordPress Website",
"Value": {
"Fn::Join": [
"",
[
"http://",
{
"Fn::GetAtt": [
"WPressWebSrv2",
"PublicDnsName"
]
},
"/wordpress"
]
]
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment