Subscribe to Our Weekly Newsletter
Creating an AWS CloudFormation Template
Creating an AWS CloudFormation Template
Firstly, you will be required to create a template based on the resources that you’ve just created. You will be using a tool known as CloudFormer, which collects all information about your running resources, and then creates a template. CloudFormer is actually a prototype that helps you in getting started. You will then make a few tweaks to the template before creating your new stack. You can visit the AWS Forums to learn how to run the tool. After generating the template and making the tweaks, you will have something that looks like the following:
{
“AWSTemplateFormatVersion”: “2020-09-09″,
“Resources”: {
“elbMyLB”: {
“Type”: “AWS::ElasticLoadBalancing::LoadBalancer”,
“Properties”: {
“AvailabilityZones”: [
"us-east-1b",
"us-east-1c"
],
“HealthCheck”: {
“HealthyThreshold”: “2″,
“Interval”: “30″,
“Target”: “HTTP:80/”,
“Timeout”: “5″,
“UnhealthyThreshold”: “2″
},
“Listeners”: [
{
"InstancePort": "80",
"LoadBalancerPort": "80",
"Protocol": "HTTP",
"PolicyNames": [
]
}
]
}
},
“distd18k4jybr69gw2cloudfrontnet”: {
“Type”: “AWS::CloudFront::Distribution”,
“Properties” : {
“DistributionConfig” : {
“S3Origin” : {
“DNSName”: “webapplication.s3.amazonaws.com”
},
“Enabled” : “true”,
“Logging” : {
“Bucket” : “webapplication.s3.amazonaws.com”,
“Prefix” : “webapp-logging/”
}
}
}
},
“asgMyAutoScalingGroup”: {
“Type”: “AWS::AutoScaling::AutoScalingGroup”,
“Properties”: {
“AvailabilityZones”: [
"us-east-1b",
"us-east-1c"
],
“Cooldown”: “300″,
“DesiredCapacity”: “1″,
“MaxSize”: “1″,
“MinSize”: “1″,
“LaunchConfigurationName”: {
“Ref”: “lcMyLC”
},
“LoadBalancerNames”: [
{
"Ref": "elbMyLB"
}
]
}
},
“lcMyLC”: {
“Type”: “AWS::AutoScaling::LaunchConfiguration”,
“Properties”: {
“ImageId”: “ami-498d5520″,
“InstanceType”: “t1.micro”,
“KeyName”: “mykeypair”,
“SecurityGroups”: [
{
"Ref": "sgwebappsecuritygroup"
}
]
}
},
“aspMyScaleUpPolicy” : {
“Type” : “AWS::AutoScaling::ScalingPolicy”,
“Properties” : {
“AdjustmentType” : “ChangeInCapacity”,
“AutoScalingGroupName” : { “Ref” : “asgMyAutoScalingGroup” },
“Cooldown” : “300″,
“ScalingAdjustment” : “1″
}
},
“cwCPUAlarmHigh”: {
“Type”: “AWS::CloudWatch::Alarm”,
“Properties”: {
“AlarmDescription”: “Scale-up if CPU > 60% for 10 minutes”,
“MetricName”: “CPUUtilization”,
“Namespace”: “AWS/EC2″,
“Statistic”: “Average”,
“Period”: “300″,
“EvaluationPeriods”: “2″,
“Threshold”: “60″,
“AlarmActions”: [ { "Ref": "aspMyScaleUpPolicy" } ],
“Dimensions”: [
{
"Name": "AutoScalingGroupName",
"Value": { "Ref": "asgMyAutoScalingGroup" }
}
],
“ComparisonOperator”: “GreaterThanThreshold”
}
},
“rdsmydbinstance”: {
“Type”: “AWS::RDS::DBInstance”,
“Properties”: {
“AllocatedStorage”: “5″,
“BackupRetentionPeriod”: “1″,
“DBInstanceClass”: “db.m1.small”,
“DBName”: “MyDatabase”,
“DBParameterGroupName”: “default.mysql5.1″,
“Engine”: “mysql”,
“EngineVersion”: “5.1.57″,
“MasterUsername”: “awsuser”,
“MasterUserPassword”: “awsuser”,
“Port”: “3306″,
“PreferredBackupWindow”: “07:00-07:30″,
“PreferredMaintenanceWindow”: “sat:04:00-sat:04:30″,
“MultiAZ”: “true”,
“DBSecurityGroups”: [
{
"Ref": "dbsgmydbsecuritygroup"
}
]
}
},
“s3webapplication”: {
“Type”: “AWS::S3::Bucket”
},
“sgwebappsecuritygroup”: {
“Type”: “AWS::EC2::SecurityGroup”,
“Properties”: {
“GroupDescription”: “for web app”,
“SecurityGroupIngress”: [
{
"IpProtocol": "tcp",
"FromPort": "80",
"ToPort": "80",
"SourceSecurityGroupName": "amazon-elb-sg",
"SourceSecurityGroupOwnerId": "amazon-elb"
},
{
"IpProtocol": "tcp",
"FromPort": "3389",
"ToPort": "3389",
"CidrIp": "0.0.0.0/0"
}
]
}
},
“dbsgmydbsecuritygroup”: {
“Type”: “AWS::RDS::DBSecurityGroup”,
“Properties”: {
“GroupDescription”: “security group for my web app”,
“DBSecurityGroupIngress”: [
{
"EC2SecurityGroupName": {
"Ref": "sgwebappsecuritygroup"
},
"EC2SecurityGroupOwnerId": "123456789012"
}
]
}
}
},
“Description”: “”
}
Now, you will want to make some changes to this template before launching your new environment. Also, you only launched one Amazon EC2 instance in this tutorial. However, it is a good practice to launch multiple instances across multiple Availability Zones (you will want to update your Auto Scaling group to launch multiple instances).You may also want to launch a new environment with your custom AMI. Finally, you will update your database information (to include your database name and password).