Skip to content

Instantly share code, notes, and snippets.

@doapp-ryanp
Created January 15, 2015 17:01
Show Gist options
  • Select an option

  • Save doapp-ryanp/a2710ebdc774ddcd1472 to your computer and use it in GitHub Desktop.

Select an option

Save doapp-ryanp/a2710ebdc774ddcd1472 to your computer and use it in GitHub Desktop.
AWS CloudFormation for CodeDeploy
{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "Webservices",
"Parameters": {
"aaPrefix": {
"Type": "String",
"Default": "prod"
},
"ProjectName": {
"Type": "String",
"Default": "my-product",
"AllowedValues": [
"my-product"
]
},
"InstanceType": {
"Type": "String",
"Default": "c3.large"
},
"AMI": {
"Type": "String",
"Default": "ami-5a82f432"
},
"KeyName": {
"Type": "String",
"Default": "myco"
},
"DesiredCapacity": {
"Type": "String",
"Default": "1"
},
"MinSize": {
"Type": "String",
"Default": "1"
},
"MaxSize": {
"Type": "String",
"Default": "1"
},
"NotificationEmail": {
"Type": "String",
"Default": "[email protected]"
},
"CpuUpThreshold": {
"Type": "String",
"Default": "70"
},
"CPUPeriods": {
"Type": "String",
"Default": "3"
},
"CpuDownThreshold": {
"Type": "String",
"Default": "35"
},
"NetworkUpThreshold": {
"Type": "String",
"Default": "40000000"
},
"NetworkDownThreshold": {
"Type": "String",
"Default": "25000000"
},
"ScaleUp": {
"Type": "String",
"Default": "3"
},
"ScaleDown": {
"Type": "String",
"Default": "-1"
},
"HostedZoneName": {
"Type": "String",
"Default": "mycos.com"
}
},
"Resources": {
"Role": {
"Type": "AWS::IAM::Role",
"Properties": {
"AssumeRolePolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": [
"ec2.amazonaws.com"
]
},
"Action": [
"sts:AssumeRole"
]
}
]
},
"Path": "/",
"Policies": [
{
"PolicyName": "CodeDeploy",
"PolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"s3:Get*",
"s3:List*"
],
"Resource": [
"arn:aws:s3:::*",
"arn:aws:s3:::aws-codedeploy-us-east-1/*",
"arn:aws:s3:::aws-codedeploy-us-west-2/*"
],
"Effect": "Allow"
}
]
}
},
{
"PolicyName": "CloudWatch",
"PolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"cloudwatch:*",
"sns:*",
"autoscaling:Describe*"
],
"Effect": "Allow",
"Resource": "*"
}
]
}
},
{
"PolicyName": "Logs",
"PolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"logs:*",
"s3:GetObject"
],
"Resource": [
"arn:aws:logs:us-east-1:*:*",
"arn:aws:s3:::*"
]
}
]
}
},
{
"PolicyName": "S3ReadOnly",
"PolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"s3:Get*",
"s3:List*"
],
"Resource": [
"arn:aws:s3:::appbuilds.mobilelocalnews.com*"
]
}
]
}
}
]
}
},
"Profile": {
"Type": "AWS::IAM::InstanceProfile",
"Properties": {
"Path": "/",
"Roles": [
{
"Ref": "Role"
}
]
}
},
"ELB": {
"Type": "AWS::ElasticLoadBalancing::LoadBalancer",
"Properties": {
"AvailabilityZones": [
"us-east-1a",
"us-east-1b",
"us-east-1e"
],
"HealthCheck": {
"HealthyThreshold": "2",
"Interval": "30",
"Target": "HTTP:80/",
"Timeout": "5",
"UnhealthyThreshold": "10"
},
"Listeners": [
{
"InstancePort": "80",
"LoadBalancerPort": "80",
"Protocol": "HTTP",
"PolicyNames": []
}
],
"ConnectionDrainingPolicy": {
"Enabled": true,
"Timeout": 5
},
"CrossZone": true
}
},
"SecurityGroup": {
"Type": "AWS::EC2::SecurityGroup",
"Properties": {
"GroupDescription": {
"Fn::Join": [
" ",
[
{
"Ref": "aaPrefix"
},
{
"Ref": "ProjectName"
}
]
]
},
"SecurityGroupIngress": [
{
"IpProtocol": "tcp",
"FromPort": "0",
"ToPort": "65535",
"CidrIp": "0.0.0.0/0"
},
{
"IpProtocol": "tcp",
"FromPort": "80",
"ToPort": "80",
"SourceSecurityGroupName": "amazon-elb-sg",
"SourceSecurityGroupOwnerId": "amazon-elb"
}
]
}
},
"ScaleUpPolicy": {
"Type": "AWS::AutoScaling::ScalingPolicy",
"Properties": {
"AdjustmentType": "ChangeInCapacity",
"AutoScalingGroupName": {
"Ref": "AutoscaleGroup"
},
"Cooldown": "360",
"ScalingAdjustment": {
"Ref": "ScaleUp"
}
}
},
"CPUAlarmHigh": {
"Type": "AWS::CloudWatch::Alarm",
"Properties": {
"EvaluationPeriods": {
"Ref": "CPUPeriods"
},
"Statistic": "Average",
"Threshold": {
"Ref": "CpuUpThreshold"
},
"AlarmDescription": "Alarm if CPU too high or metric disappears indicating instance is down",
"Period": "60",
"AlarmActions": [
{
"Ref": "ScaleUpPolicy"
}
],
"Namespace": "AWS/EC2",
"Dimensions": [
{
"Name": "AutoScalingGroupName",
"Value": {
"Ref": "AutoscaleGroup"
}
}
],
"ComparisonOperator": "GreaterThanThreshold",
"MetricName": "CPUUtilization"
}
},
"ScaleDownPolicy": {
"Type": "AWS::AutoScaling::ScalingPolicy",
"Properties": {
"AdjustmentType": "ChangeInCapacity",
"AutoScalingGroupName": {
"Ref": "AutoscaleGroup"
},
"Cooldown": "360",
"ScalingAdjustment": {
"Ref": "ScaleDown"
}
}
},
"CPUAlarmLow": {
"Type": "AWS::CloudWatch::Alarm",
"Properties": {
"EvaluationPeriods": "6",
"Statistic": "Average",
"Threshold": {
"Ref": "CpuDownThreshold"
},
"AlarmDescription": "Alarm if CPU too low",
"Period": "300",
"AlarmActions": [
{
"Ref": "ScaleDownPolicy"
}
],
"Namespace": "AWS/EC2",
"Dimensions": [
{
"Name": "AutoScalingGroupName",
"Value": {
"Ref": "AutoscaleGroup"
}
}
],
"ComparisonOperator": "LessThanThreshold",
"MetricName": "CPUUtilization"
}
},
"NetworkInAlarmLow": {
"Type": "AWS::CloudWatch::Alarm",
"Properties": {
"EvaluationPeriods": "2",
"Statistic": "Average",
"Threshold": {
"Ref": "NetworkDownThreshold"
},
"AlarmDescription": "Alarm if Network in too low",
"Period": "60",
"AlarmActions": [
{
"Ref": "ScaleDownPolicy"
}
],
"Namespace": "AWS/EC2",
"Dimensions": [
{
"Name": "AutoScalingGroupName",
"Value": {
"Ref": "AutoscaleGroup"
}
}
],
"ComparisonOperator": "LessThanThreshold",
"MetricName": "NetworkIn"
}
},
"NetworkInHighLow": {
"Type": "AWS::CloudWatch::Alarm",
"Properties": {
"EvaluationPeriods": "1",
"Statistic": "Average",
"Threshold": {
"Ref": "NetworkUpThreshold"
},
"AlarmDescription": "Alarm if Network in too high",
"Period": "60",
"AlarmActions": [
{
"Ref": "ScaleUpPolicy"
}
],
"Namespace": "AWS/EC2",
"Dimensions": [
{
"Name": "AutoScalingGroupName",
"Value": {
"Ref": "AutoscaleGroup"
}
}
],
"ComparisonOperator": "GreaterThanThreshold",
"MetricName": "NetworkIn"
}
},
"AutoscaleGroup": {
"Type": "AWS::AutoScaling::AutoScalingGroup",
"Properties": {
"HealthCheckType": "ELB",
"HealthCheckGracePeriod": "450",
"AvailabilityZones": [
"us-east-1a",
"us-east-1b",
"us-east-1e"
],
"Cooldown": "300",
"DesiredCapacity": {
"Ref": "DesiredCapacity"
},
"MaxSize": {
"Ref": "MaxSize"
},
"MinSize": {
"Ref": "MinSize"
},
"LoadBalancerNames": [
{
"Ref": "ELB"
}
],
"LaunchConfigurationName": {
"Ref": "LaunchConfig"
},
"Tags": [
{
"Key": "Name",
"Value": {
"Fn::Join": [
"-",
[
{
"Ref": "aaPrefix"
},
{
"Ref": "ProjectName"
}
]
]
},
"PropagateAtLaunch": true
}
]
}
},
"LaunchConfig": {
"Type": "AWS::AutoScaling::LaunchConfiguration",
"Properties": {
"ImageId": {
"Ref": "AMI"
},
"InstanceType": {
"Ref": "InstanceType"
},
"KeyName": {
"Ref": "KeyName"
},
"IamInstanceProfile": {
"Ref": "Profile"
},
"SecurityGroups": [
{
"Ref": "SecurityGroup"
}
],
"UserData" : {
"Fn::Base64" : {
"Fn::Join" : [
"",
[
"#!/bin/bash\n",
"\n",
"update-rc.d codedeploy-agent enable\n",
"service codedeploy-agent start\n"
]
]
}
}
}
},
"SNSTopicForAlarmsMinor": {
"Type": "AWS::SNS::Topic",
"Properties": {
"Subscription": [
{
"Endpoint": {
"Ref": "NotificationEmail"
},
"Protocol": "email"
}
],
"DisplayName": {
"Fn::Join": [
"-",
[
{
"Ref": "aaPrefix"
},
{
"Ref": "ProjectName"
},
"minor"
]
]
},
"TopicName": {
"Fn::Join": [
"-",
[
{
"Ref": "aaPrefix"
},
{
"Ref": "ProjectName"
},
"minor"
]
]
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment