Skip to content

Instantly share code, notes, and snippets.

@nicerobot
Forked from fernandohonig/wordpress.template
Last active June 13, 2017 04:03
Show Gist options
  • Save nicerobot/2cd7b529f307398fb44a638f015d1876 to your computer and use it in GitHub Desktop.
Save nicerobot/2cd7b529f307398fb44a638f015d1876 to your computer and use it in GitHub Desktop.
Parameters:
VPCID:
Type: 'AWS::EC2::VPC::Id'
Description: Select Virtual Private Cloud ID
ELBSubnet:
Type: 'List<AWS::EC2::Subnet::Id>'
Description: Subnets for Elastic Loadbalancer
PrivateSubnets:
Type: 'List<AWS::EC2::Subnet::Id>'
Description: Subnets for RDS and EC2
InstanceType:
Description: WebServer EC2 instance type
Type: String
Default: t2.micro
AllowedValues:
- t2.micro
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.
DBClass:
Description: Database instance class
Type: String
Default: db.t2.micro
AllowedValues:
- db.t2.micro
ConstraintDescription: must select a valid database instance type.
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.
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.
WebServerCapacity:
Default: '1'
Description: The initial number of WebServer instances
Type: Number
MinValue: '1'
MaxValue: '5'
ConstraintDescription: must be between 1 and 5 EC2 instances.
DBAllocatedStorage:
Default: '5'
Description: The size of the database (Gb)
Type: Number
MinValue: '5'
MaxValue: '1024'
ConstraintDescription: must be between 5 and 1024Gb.
Mappings:
AWSInstanceType2Arch:
t2.micro:
Arch: HVM64
AWSRegionArch2AMI:
us-east-1:
HVM64: ami-f5f41398
us-west-2:
HVM64: ami-d0f506b0
us-west-1:
HVM64: ami-6e84fa0e
eu-west-1:
HVM64: ami-b0ac25c3
Resources:
ElasticLoadBalancer:
Type: 'AWS::ElasticLoadBalancing::LoadBalancer'
Properties:
Subnets: !Ref ELBSubnet
CrossZone: 'true'
SecurityGroups:
- !Ref ELBSecurityGroup
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'
InstanceSecurityGroup:
Type: 'AWS::EC2::SecurityGroup'
Properties:
GroupDescription: Enable inbound network access access to EC2 instance.
VpcId: !Ref VPCID
SecurityGroupIngress:
- ToPort: '80'
IpProtocol: tcp
SourceSecurityGroupId: !Ref ELBSecurityGroup
FromPort: '80'
Tags:
- Key: Name
Value: !Join
- ''
- - !Ref 'AWS::StackName'
- EC2SecurityGrp
ELBSecurityGroup:
Type: 'AWS::EC2::SecurityGroup'
Properties:
GroupDescription: Enable inbound network access to load balancer.
VpcId: !Ref VPCID
SecurityGroupIngress:
- ToPort: '80'
IpProtocol: tcp
FromPort: '80'
CidrIp: 0.0.0.0/0
Tags:
- Key: Name
Value: !Join
- ''
- - !Ref 'AWS::StackName'
- AppGroupName
WebServerGroup:
Type: 'AWS::AutoScaling::AutoScalingGroup'
Properties:
VPCZoneIdentifier: !Ref PrivateSubnets
LaunchConfigurationName: !Ref LaunchConfig
MinSize: '1'
MaxSize: '5'
DesiredCapacity: !Ref WebServerCapacity
LoadBalancerNames:
- !Ref ElasticLoadBalancer
CreationPolicy:
ResourceSignal:
Timeout: PT15M
UpdatePolicy:
AutoScalingRollingUpdate:
MinInstancesInService: '1'
MaxBatchSize: '1'
PauseTime: PT15M
WaitOnResourceSignals: 'true'
LaunchConfig:
Type: 'AWS::AutoScaling::LaunchConfiguration'
Metadata:
'AWS::CloudFormation::Init':
configSets:
wordpress_install:
- install_cfn
- install_wordpress
install_cfn:
files:
/etc/cfn/cfn-hup.conf:
content: !Join
- ''
- - |
[main]
- stack=
- !Ref 'AWS::StackId'
- |+
- region=
- !Ref 'AWS::Region'
- |+
mode: '000400'
owner: root
group: root
/etc/cfn/hooks.d/cfn-auto-reloader.conf:
content: !Join
- ''
- - |
[cfn-auto-reloader-hook]
- |
triggers=post.update
- >
path=Resources.LaunchConfig.Metadata.AWS::CloudFormation::Init
- 'action=/opt/aws/bin/cfn-init -v '
- ' --stack '
- !Ref 'AWS::StackName'
- ' --resource LaunchConfig '
- ' --configsets wordpress_install '
- ' --region '
- !Ref 'AWS::Region'
- |+
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: []
mysql: []
httpd: []
sources:
/var/www/html: 'http://wordpress.org/latest.tar.gz'
files:
/tmp/create-wp-config:
content: !Join
- ''
- - |
#!/bin/bash
- >
cp /var/www/html/wordpress/wp-config-sample.php
/var/www/html/wordpress/wp-config.php
- sed -i "s/'database_name_here'/'
- !Ref DBName
- |
'/g" wp-config.php
- sed -i "s/'username_here'/'
- !Ref DBUser
- |
'/g" wp-config.php
- sed -i "s/'password_here'/'
- !Ref DBPassword
- |
'/g" wp-config.php
- sed -i "s/'localhost'/'
- !GetAtt
- DBInstance
- Endpoint.Address
- |
'/g" wp-config.php
mode: '000500'
owner: root
group: root
commands:
01_configure_wordpress:
command: /tmp/create-wp-config
cwd: /var/www/html/wordpress
services:
sysvinit:
httpd:
enabled: 'true'
ensureRunning: 'true'
Properties:
ImageId: !FindInMap
- AWSRegionArch2AMI
- !Ref 'AWS::Region'
- !FindInMap
- AWSInstanceType2Arch
- !Ref InstanceType
- Arch
InstanceType: !Ref InstanceType
SecurityGroups:
- !Ref InstanceSecurityGroup
UserData: !Base64
'Fn::Join':
- ''
- - |
#!/bin/bash -xe
- |
yum update -y aws-cfn-bootstrap
- '/opt/aws/bin/cfn-init -v '
- ' --stack '
- !Ref 'AWS::StackName'
- ' --resource LaunchConfig '
- ' --configsets wordpress_install '
- ' --region '
- !Ref 'AWS::Region'
- |+
- '/opt/aws/bin/cfn-signal -e $? '
- ' --stack '
- !Ref 'AWS::StackName'
- ' --resource WebServerGroup '
- ' --region '
- !Ref 'AWS::Region'
- |+
DBEC2SecurityGroup:
Type: 'AWS::EC2::SecurityGroup'
Properties:
VpcId: !Ref VPCID
GroupDescription: Open database for access
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: '3306'
ToPort: '3306'
SourceSecurityGroupId: !Ref InstanceSecurityGroup
DBSubnetGroup:
Type: 'AWS::RDS::DBSubnetGroup'
Properties:
DBSubnetGroupDescription: RDS DB Subnet
SubnetIds: !Ref PrivateSubnets
DBInstance:
Type: 'AWS::RDS::DBInstance'
Properties:
DBName: !Ref DBName
DBSubnetGroupName: !Ref DBSubnetGroup
Engine: MySQL
MultiAZ: !Ref MultiAZDatabase
MasterUsername: !Ref DBUser
MasterUserPassword: !Ref DBPassword
DBInstanceClass: !Ref DBClass
AllocatedStorage: !Ref DBAllocatedStorage
VPCSecurityGroups:
- !Ref DBEC2SecurityGroup
Outputs:
WebsiteURL:
Value: !Join
- ''
- - 'http://'
- !GetAtt
- ElasticLoadBalancer
- DNSName
- /wordpress
Description: WordPress Website
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment