Skip to content

Instantly share code, notes, and snippets.

@BeardedCloudWalker
Created June 10, 2017 15:29
Show Gist options
  • Save BeardedCloudWalker/70d76bcd65662bdcccb988ac013f8c41 to your computer and use it in GitHub Desktop.
Save BeardedCloudWalker/70d76bcd65662bdcccb988ac013f8c41 to your computer and use it in GitHub Desktop.
AWSTemplateFormatVersion: '2010-09-09'
Description: Create an Apache webserver with a webpage.
Parameters:
InstanceType:
Description: Type of EC2 instance to launch
Type: String
Default: t1.micro
VPCId:
Description: VPC Id to put the Security Group
Type: String
Default: vpc-12345678
SubnetId:
Description: Subnet ID to put the instance
Type: String
Default: subnet-12345678
WebServerPort:
Description: TCP/IP port of the web server
Type: String
Default: '80'
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.
Mappings:
AWSInstanceType2Arch:
t1.micro:
Arch: '64'
AWSRegionArch2AMI:
us-east-1:
'64': ami-246ed34c
Resources:
WebServerInstance:
Type: 'AWS::EC2::Instance'
Metadata:
'AWS::CloudFormation::Init':
configSets:
InstallAndRun:
- Install
Install:
packages:
yum:
httpd: []
files:
/var/www/html/index.html:
content:
'Fn::Join':
- ''
- - |
<html>
- |
<body>Hello World</body>
- |
</html>
mode: '000600'
owner: apache
group: apache
/etc/cfn/cfn-hup.conf:
content:
'Fn::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:
'Fn::Join':
- ''
- - |
[cfn-auto-reloader-hook]
- |
triggers=post.update
- >
path=Resources.WebServerInstance.Metadata.AWS::CloudFormation::Init
- 'action=/opt/aws/bin/cfn-init -v '
- ' --stack '
- Ref: 'AWS::StackName'
- ' --resource WebServerInstance '
- ' --configsets InstallAndRun '
- ' --region '
- Ref: 'AWS::Region'
- |+
- |
runas=root
services:
sysvinit:
httpd:
enabled: 'true'
ensureRunning: 'true'
cfn-hup:
enabled: 'true'
ensureRunning: 'true'
files:
- /etc/cfn/cfn-hup.conf
- /etc/cfn/hooks.d/cfn-auto-reloader.conf
Properties:
SubnetId:
Ref: SubnetId
ImageId:
'Fn::FindInMap':
- AWSRegionArch2AMI
- Ref: 'AWS::Region'
- 'Fn::FindInMap':
- AWSInstanceType2Arch
- Ref: InstanceType
- Arch
InstanceType:
Ref: InstanceType
SecurityGroupIds:
- Ref: WebServerSecurityGroup
KeyName:
Ref: KeyName
UserData:
'Fn::Base64':
'Fn::Join':
- ''
- - |
#!/bin/bash -xe
- |
yum update -y aws-cfn-bootstrap
- |
# Install the files and packages from the metadata
- '/opt/aws/bin/cfn-init -v '
- ' --stack '
- Ref: 'AWS::StackName'
- ' --resource WebServerInstance '
- ' --configsets InstallAndRun '
- ' --region '
- Ref: 'AWS::Region'
- |+
- |
# Signal the status from cfn-init
- '/opt/aws/bin/cfn-signal -e $? '
- ' --stack '
- Ref: 'AWS::StackName'
- ' --resource WebServerInstance '
- ' --region '
- Ref: 'AWS::Region'
- |+
WebServerSecurityGroup:
Type: 'AWS::EC2::SecurityGroup'
Properties:
VpcId:
Ref: VPCId
GroupDescription: Enable HTTP access via port 80
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: '80'
ToPort: '80'
CidrIp: 0.0.0.0/0
- IpProtocol: tcp
FromPort: '22'
ToPort: '22'
CidrIp:
Ref: SSHLocation
Outputs:
WebsiteURL:
Description: URL for newly created Apache server
Value:
'Fn::Join':
- ''
- - 'http://'
- 'Fn::GetAtt':
- WebServerInstance
- PublicDnsName
@BeardedCloudWalker
Copy link
Author

creates ec2 inside of specified vpc and subnet

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment