Skip to content

Instantly share code, notes, and snippets.

@karlospn
Created February 10, 2019 23:14
Show Gist options
  • Save karlospn/e005b2cd6065c4f7a0e79214e29940b1 to your computer and use it in GitHub Desktop.
Save karlospn/e005b2cd6065c4f7a0e79214e29940b1 to your computer and use it in GitHub Desktop.
Simple cloudformation example
Parameters:
ec2Name:
Description: ec2 name
Type: String
availZone:
Description: availability zone
Type: AWS::EC2::AvailabilityZone::Name
myKeyPair:
Description: Amazon EC2 Key Pair
Type: "AWS::EC2::KeyPair::KeyName"
Outputs:
ServerDnsName:
Description: dns name
Value: !GetAtt MyEc2Instance.PublicDnsName
IpPublic:
Description: public ip
Value: !GetAtt MyEc2Instance.PublicIp
Resources:
MyEc2Instance:
Type: AWS::EC2::Instance
Metadata:
AWS::CloudFormation::Init:
config:
packages:
yum:
httpd: []
php: []
files:
/var/www/html/index.php:
content: !Sub |
<?php print "Hello world!"; ?>
services:
sysvinit:
httpd:
enabled: true
ensureRunning: true
Properties:
AvailabilityZone: !Ref availZone
InstanceType: t2.micro
ImageId: ami-0fad7378adf284ce0
KeyName: !Ref myKeyPair
NetworkInterfaces:
- AssociatePublicIpAddress: "true"
DeviceIndex: "0"
GroupSet:
- !Ref MySecGroup
SubnetId: !Ref MySubnet
Tags:
- Key: Name
Value: !Ref ec2Name
UserData:
'Fn::Base64':
!Sub |
#!/bin/bash -xe
# Ensure AWS CFN Bootstrap is the latest
yum install -y aws-cfn-bootstrap
# Install the files and packages from the metadata
/opt/aws/bin/cfn-init -v --stack ${AWS::StackName} --resource EC2Instance --region ${AWS::Region}
MyEBSVolume:
Type: AWS::EC2::Volume
Properties:
VolumeType: gp2
AvailabilityZone: !Ref availZone
Size: 20
MyInternetGateway:
Type: AWS::EC2::InternetGateway
MyVPCGatewayAttachment:
Type: AWS::EC2::VPCGatewayAttachment
Properties:
InternetGatewayId: !Ref MyInternetGateway
VpcId: !Ref myVPC
MyRouteTable:
Type: AWS::EC2::RouteTable
Properties:
VpcId: !Ref myVPC
MySubnetRouteTableAssociation:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
SubnetId: !Ref MySubnet
RouteTableId: !Ref MyRouteTable
MyMountPoint:
Type: AWS::EC2::VolumeAttachment
Properties:
InstanceId: !Ref MyEc2Instance
VolumeId: !Ref MyEBSVolume
Device: /dev/sdh
myRoute:
Type: AWS::EC2::Route
DependsOn:
- MyInternetGateway
- MyVPCGatewayAttachment
Properties:
RouteTableId: !Ref MyRouteTable
DestinationCidrBlock: 0.0.0.0/0
GatewayId: !Ref MyInternetGateway
myVPC:
Type: AWS::EC2::VPC
Properties:
CidrBlock: 10.0.0.0/16
EnableDnsSupport: true
EnableDnsHostnames: true
MySecGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Allow ssh
VpcId: !Ref myVPC
SecurityGroupIngress:
- IpProtocol : tcp
FromPort : 80
ToPort : 80
CidrIp : 0.0.0.0/0
- IpProtocol : tcp
FromPort : 22
ToPort : 22
CidrIp : 0.0.0.0/0
MySubnet:
Type: AWS::EC2::Subnet
Properties:
AvailabilityZone: !Ref availZone
VpcId: !Ref myVPC
CidrBlock: 10.0.2.0/24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment