Created
April 27, 2020 07:47
-
-
Save Jeffen/dfb10975829e99a308e966d7fa4dfe49 to your computer and use it in GitHub Desktop.
AWS Cloud formation cfn-init example
This file contains 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 for CFN Init | |
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. | |
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. | |
Resources: | |
WebServerSecurityGroup: | |
Type: AWS::EC2::SecurityGroup | |
Properties: | |
GroupDescription: Enable HTTP access via port 80 and SSH access via port 22 | |
SecurityGroupIngress: | |
- IpProtocol: tcp | |
FromPort: '80' | |
ToPort: '80' | |
CidrIp: 0.0.0.0/0 | |
- IpProtocol: tcp | |
FromPort: '22' | |
ToPort: '22' | |
CidrIp: !Ref SSHLocation | |
WebServerHost: | |
Type: AWS::EC2::Instance | |
Metadata: | |
Comment: Install a simple PHP application | |
AWS::CloudFormation::Init: | |
config: | |
packages: | |
yum: | |
httpd: [] | |
php: [] | |
groups: | |
apache: {} | |
users: | |
"apache": | |
groups: | |
- "apache" | |
sources: | |
"/home/ec2-user/aws-cli": "https://github.com/aws/aws-cli/tarball/master" | |
files: | |
"/tmp/cwlogs/apacheaccess.conf": | |
content: !Sub | | |
[general] | |
state_file= /var/awslogs/agent-state | |
[/var/log/httpd/access_log] | |
file = /var/log/httpd/access_log | |
log_group_name = ${AWS::StackName} | |
log_stream_name = {instance_id}/apache.log | |
datetime_format = %d/%b/%Y:%H:%M:%S | |
mode: '000400' | |
owner: apache | |
group: apache | |
"/var/www/html/index.php": | |
content: !Sub | | |
<?php | |
echo '<h1>AWS CloudFormation sample PHP application for ${AWS::StackName}</h1>'; | |
?> | |
mode: '000644' | |
owner: apache | |
group: apache | |
"/etc/cfn/cfn-hup.conf": | |
content: !Sub | | |
[main] | |
stack=${AWS::StackId} | |
region=${AWS::Region} | |
mode: "000400" | |
owner: "root" | |
group: "root" | |
"/etc/cfn/hooks.d/cfn-auto-reloader.conf": | |
content: !Sub | | |
[cfn-auto-reloader-hook] | |
triggers=post.update | |
path=Resources.WebServerHost.Metadata.AWS::CloudFormation::Init | |
action=/opt/aws/bin/cfn-init -v --stack ${AWS::StackName} --resource WebServerHost --region ${AWS::Region} | |
mode: "000400" | |
owner: "root" | |
group: "root" | |
commands: | |
test: | |
command: "echo \"$MAGIC\" > test.txt" | |
env: | |
MAGIC: "I come from the environment!" | |
cwd: "~" | |
services: | |
sysvinit: | |
httpd: | |
enabled: 'true' | |
ensureRunning: 'true' | |
sendmail: | |
enabled: 'false' | |
ensureRunning: 'false' | |
CreationPolicy: | |
ResourceSignal: | |
Timeout: PT5M | |
Properties: | |
ImageId: ami-a4c7edb2 | |
KeyName: | |
Ref: KeyName | |
InstanceType: t2.micro | |
SecurityGroups: | |
- Ref: WebServerSecurityGroup | |
UserData: | |
"Fn::Base64": | |
!Sub | | |
#!/bin/bash -xe | |
# Get the latest CloudFormation package | |
yum update -y aws-cfn-bootstrap | |
# Start cfn-init | |
/opt/aws/bin/cfn-init -s ${AWS::StackId} -r WebServerHost --region ${AWS::Region} || error_exit 'Failed to run cfn-init' | |
# Start up the cfn-hup daemon to listen for changes to the EC2 instance metadata | |
/opt/aws/bin/cfn-hup || error_exit 'Failed to start cfn-hup' | |
# All done so signal success | |
/opt/aws/bin/cfn-signal -e $? --stack ${AWS::StackId} --resource WebServerHost --region ${AWS::Region} | |
Outputs: | |
InstanceId: | |
Description: The instance ID of the web server | |
Value: | |
Ref: WebServerHost | |
WebsiteURL: | |
Value: | |
!Sub 'http://${WebServerHost.PublicDnsName}' | |
Description: URL for newly created LAMP stack | |
PublicIP: | |
Description: Public IP address of the web server | |
Value: | |
!GetAtt WebServerHost.PublicIp |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment