An Introduction to CloudFormation, AWS’s Infrastructure as Code

AWS CloudFormation template designer graphic for the intended outcome of the upcoming exmaple

Understanding the template

Resources:
MYVPCNAME:
Type: 'AWS::EC2::VPC'
Properties:
CidrBlock: 10.0.0.0/16
Resources:
MYVPCNAME:
Type: 'AWS::EC2::VPC'
Properties:
CidrBlock: 10.0.0.0/16
PublicSubnet:
Type: 'AWS::EC2::Subnet'
Properties:
CidrBlock: 10.0.1.0/24
VpcId: !Ref MYVPCNAME

Template designer

Get Coding!

  • VPC
  • Public Subnet
  • EC2 Instance
  • EC2 Security Group
  • IAM Instance Profile
  • IAM Role
  • Elastic IP
  • Internet Gateway
  • Route Table
Properties:
ImageId: ami-05c424d59413a2876
InstanceType: t2.micro
SubnetId: !Ref PublicSubnet
Properties:
VpcId: !Ref MYVPCNAME
GroupDescription: Development server access over http/ssh
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 22
ToPort: 22
Description: SSH access
CidrIp: 0.0.0.0/0
- IpProtocol: tcp
FromPort: 80
ToPort: 80
Description: HTTP access
CidrIp: 0.0.0.0/0
- IpProtocol: tcp
FromPort: 443
ToPort: 443
Description: HTTPS access
CidrIp: 0.0.0.0/0

IAM Resources

Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service:
- ec2.amazonaws.com
Action:
- 'sts:AssumeRole'
ManagedPolicyArns:
- 'arn:aws:iam::aws:policy/AmazonEC2FullAccess'
- 'arn:aws:iam::aws:policy/AmazonSESFullAccess'
- 'arn:aws:iam::aws:policy/AmazonS3FullAccess'

Route Table and Gateway

PermitAllToIGW:
Type: 'AWS::EC2::Route'
Properties:
RouteTableId: !Ref RouteTable
DestinationCidrBlock: 0.0.0.0/0
GatewayId: !Ref IGW

Parameters

Parameters:
DEVELOPERIP:
Description: 'Ip address for development machine to allow ssh, in CIDR format'
Type: String
SSHKEY:
Description: Key pair for instance SSH access
Type: 'AWS::EC2::KeyPair::KeyName'

Bonus: UserData

UserData:
{"Fn::Base64":
{"Fn::Join":
["",
[
"#/bin/bash \n",
"# one command per array item \n"
]
]
}
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store