Created
August 23, 2012 07:31
-
-
Save kachina/3433846 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"AWSTemplateFormatVersion": "2010-09-09", | |
"Description": "This is a template for the AWS CloudFormation in order to build a CloudDesignPattern No17. Copyright 2012 Serverworks Co, Ltd. All Rights Reserved. ", | |
"Parameters" : { | |
"AMI" : { | |
"Type" : "String", | |
"Description" : "AMI ID", | |
"Default" : "ami-2819aa29", | |
"ConstraintDescription" : "must be a valid AMI ID." | |
}, | |
"KeyPair" : { | |
"Type" : "String", | |
"Description" : "Key Pair", | |
"Default" : "foo", | |
"ConstraintDescription" : "must be a valid Key Pair." | |
}, | |
"InstanceType" : { | |
"Type" : "String", | |
"Description" : "Instance type (t1.micro, m1.small, c1.medium, m1.medium, m1.large, m1.xlarge, m2.xlarge, m2.2xlarge, m2.4xlarge, c1.xlarge)", | |
"Default" : "t1.micro", | |
"AllowedValues" : [ "t1.micro","m1.small","c1.medium","m1.medium","m1.large","m1.xlarge","m2.xlarge","m2.2xlarge","m2.4xlarge","c1.xlarge" ], | |
"ConstraintDescription" : "must be a valid Amazon EC2 instance type." | |
}, | |
"PeakDateTime" : { | |
"Type" : "String", | |
"Description" : "Peak UTC date time (yyyy-mm-ddThh:mi:ssZ)", | |
"Default" : "2012-08-28T18:55:00Z", | |
"ConstraintDescription" : "must be a valid Peak date time." | |
}, | |
"PeakTimeInstance" : { | |
"Type" : "String", | |
"Description" : "Number of peak time instance", | |
"Default" : "10", | |
"ConstraintDescription" : "must be a valid instances of peak time." | |
}, | |
"AWSAccessKeyId" : { | |
"Type" : "String", | |
"Description" : "input the aws access key id.", | |
"ConstraintDescription" : "must be a valid AWS Access Key ID." | |
}, | |
"AWSSecretKey" : { | |
"Type" : "String", | |
"Description" : "input the aws secret key was Converted to \"\\\\/\" a \"/\".", | |
"ConstraintDescription" : "must be a valid AWS Secret Key." | |
} | |
}, | |
"Resources": { | |
"LoadBalancer": { | |
"Type": "AWS::ElasticLoadBalancing::LoadBalancer", | |
"Properties": { | |
"AvailabilityZones": [ | |
"ap-northeast-1a", | |
"ap-northeast-1b" | |
], | |
"HealthCheck": { | |
"HealthyThreshold": "10", | |
"Interval": "30", | |
"Target": "HTTP:80/index.html", | |
"Timeout": "5", | |
"UnhealthyThreshold": "2" | |
}, | |
"Listeners": [ | |
{ | |
"InstancePort": "80", | |
"LoadBalancerPort": "80", | |
"Protocol": "HTTP", | |
"PolicyNames": [ | |
] | |
} | |
] | |
} | |
}, | |
"AutoScalingGroup": { | |
"Type": "AWS::AutoScaling::AutoScalingGroup", | |
"Properties": { | |
"AvailabilityZones": [ | |
"ap-northeast-1a", | |
"ap-northeast-1b" | |
], | |
"Cooldown": "300", | |
"DesiredCapacity": "2", | |
"MinSize": "2", | |
"MaxSize": "20", | |
"LaunchConfigurationName": { "Ref": "LaunchConfiguration" }, | |
"LoadBalancerNames": [ { "Ref": "LoadBalancer" } ] | |
} | |
}, | |
"LaunchConfiguration": { | |
"Type": "AWS::AutoScaling::LaunchConfiguration", | |
"Properties": { | |
"KeyName": { "Ref" : "KeyPair" }, | |
"ImageId": { "Ref" : "AMI" }, | |
"InstanceType": { "Ref": "InstanceType" }, | |
"SecurityGroups": [ | |
{ "Ref" : "SecurityGroup" } | |
] | |
} | |
}, | |
"CPUAlarmHigh": { | |
"Type": "AWS::CloudWatch::Alarm", | |
"Properties": { | |
"AlarmDescription": "Scale-up if CPU > 80% for 5 minutes", | |
"MetricName": "CPUUtilization", | |
"Namespace": "AWS/EC2", | |
"Statistic": "Average", | |
"Period": "300", | |
"EvaluationPeriods": "2", | |
"Threshold": "90", | |
"AlarmActions": [ { "Ref": "WebServerScaleUpPolicy" } ], | |
"Dimensions": [ | |
{ | |
"Name": "AutoScalingGroupName", | |
"Value": { "Ref": "AutoScalingGroup" } | |
} | |
], | |
"ComparisonOperator": "GreaterThanThreshold" | |
} | |
}, | |
"WebServerScaleUpPolicy" : { | |
"Type" : "AWS::AutoScaling::ScalingPolicy", | |
"Properties" : { | |
"AdjustmentType" : "ChangeInCapacity", | |
"AutoScalingGroupName" : { "Ref" : "AutoScalingGroup" }, | |
"Cooldown" : "60", | |
"ScalingAdjustment" : "1" | |
} | |
}, | |
"CPUAlarmLow": { | |
"Type": "AWS::CloudWatch::Alarm", | |
"Properties": { | |
"AlarmDescription": "Scale-down if CPU < 60% for 5 minutes", | |
"MetricName": "CPUUtilization", | |
"Namespace": "AWS/EC2", | |
"Statistic": "Average", | |
"Period": "300", | |
"EvaluationPeriods": "2", | |
"Threshold": "70", | |
"AlarmActions": [ { "Ref": "WebServerScaleDownPolicy" } ], | |
"Dimensions": [ | |
{ | |
"Name": "AutoScalingGroupName", | |
"Value": { "Ref": "AutoScalingGroup" } | |
} | |
], | |
"ComparisonOperator": "LessThanThreshold" | |
} | |
}, | |
"WebServerScaleDownPolicy" : { | |
"Type" : "AWS::AutoScaling::ScalingPolicy", | |
"Properties" : { | |
"AdjustmentType" : "ChangeInCapacity", | |
"AutoScalingGroupName" : { "Ref" : "AutoScalingGroup" }, | |
"Cooldown" : "60", | |
"ScalingAdjustment" : "-1" | |
} | |
}, | |
"ManagementInstance": { | |
"Type": "AWS::EC2::Instance", | |
"Properties": { | |
"AvailabilityZone": "ap-northeast-1a", | |
"DisableApiTermination": "FALSE", | |
"ImageId": { "Ref" : "AMI" }, | |
"InstanceType": { "Ref" : "InstanceType" }, | |
"KernelId": "aki-44992845", | |
"KeyName": { "Ref" : "KeyPair" }, | |
"Monitoring": "false", | |
"SecurityGroups": [ | |
{ "Ref" : "SecurityGroup" } | |
], | |
"Tags": [ | |
{ "Key": "Name", "Value": "ManagementInstance" } | |
], | |
"UserData" : { "Fn::Base64" : { "Fn::Join" : [ "", [ | |
"#!/bin/bash\n", | |
"export JAVA_HOME=/usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0.x86_64/jre\n", | |
"export AWS_AUTO_SCALING_HOME=/opt/aws/apitools/as\n", | |
"export PATH=$PATH:${AWS_AUTO_SCALING_HOME}/bin\n", | |
"cp /opt/aws/credential-file-path.template /opt/aws/credential-file-path\n", | |
"sed -i \"s/^AWSAccessKeyId.*/AWSAccessKeyId=", { "Ref" : "AWSAccessKeyId" }, "/g\" /opt/aws/credential-file-path\n", | |
"sed -i \"s/^AWSSecretKey.*/AWSSecretKey=", { "Ref" : "AWSSecretKey" }, "/g\" /opt/aws/credential-file-path\n", | |
"as-put-scheduled-update-group-action peak_action -g ", { "Ref": "AutoScalingGroup" }, " --region ap-northeast-1 --aws-credential-file /opt/aws/credential-file-path --name \"scale out\" --time \"", { "Ref": "PeakDateTime" }, "\" -min-size ", { "Ref": "PeakTimeInstance" }, " -max-size 20\n", | |
"shutdown -h now\n" | |
] ] } } | |
} | |
}, | |
"SecurityGroup": { | |
"Type": "AWS::EC2::SecurityGroup", | |
"Properties": { | |
"GroupDescription": "Security", | |
"SecurityGroupIngress": [ | |
{ | |
"IpProtocol": "tcp", | |
"FromPort": "22", | |
"ToPort": "22", | |
"CidrIp": "0.0.0.0/0" | |
}, | |
{ | |
"IpProtocol": "tcp", | |
"FromPort": "80", | |
"ToPort": "80", | |
"CidrIp": "0.0.0.0/0" | |
} | |
] | |
} | |
} | |
}, | |
"Outputs" : { | |
"ManagementInstancePublicDNS" : { | |
"Value" : { "Fn::GetAtt" : [ "ManagementInstance", "PublicDnsName" ] }, | |
"Description" : "" | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment