Skip to content

Instantly share code, notes, and snippets.

@pgreene
Created January 31, 2013 18:19
Show Gist options
  • Save pgreene/4684983 to your computer and use it in GitHub Desktop.
Save pgreene/4684983 to your computer and use it in GitHub Desktop.
nts.cfn.php
<?php
include_once "../cfn-generator.php";
/**
* The template generation often does not have to be complete. The create_template()
* function applies inpwrd-specific policy optimizations, which may be influenced by
* the NS: key prefix hints. For example:
*
* == Launch Configuration ==
* - ImageID is latest Amazon Linux AMI. Defaults to EBS backed. Picks S3-backed if
NS:EBS = false.
* - Dev and QA are spot unless specified otherwise (NS:ISSpot = false)
* - If spot, bid price is set to the on-demand cost.
* - If no IAM Profile is set, the generic environment one is selected.
* - User data is created from NS:Name (project name for finding bootstrap file on S3).
* - Set the keyname to environment.
*
* == Auto Scaling Groups ===
* - If there is a load balancer or we are launching on-demand, AZs A+B are selected. Otherwise
* all AZs are selected to increase spot availability.
*
* == Load Balancers ==
* - Default to AZs A+B.
* == Security Groups ===
* - Description is automatically created as a summary of ports opened.
* - Automatically adds bouncebox-global SOURCE to any SG created.
* - NS:DefaultSecurityPolicy is replaced by a standard config for a specific port.
*/
if (count($_SERVER['argv'])!=2) {
die("Usage:\nphp ".$_SERVER['argv'][0]." {dev,qa,prd} > stack.json\ncfn-update-stack [stack-name] --template-file stack.json\n");
}
$environment = preg_replace("/[0-9]/", "", $_SERVER['argv'][1]);
print create_template($environment, array(
'Description' => "nts stack",
'Resources' => array(
'ntstaggerASG' => array(
'Type' => 'AWS::AutoScaling::AutoScalingGroup',
'Properties' => array(
'LaunchConfigurationName' => array('Ref' => 'ntstaggerLC'),
'MaxSize' => ($environment=='prd' ? 15 : 1),
'MinSize' => ($environment=='prd' ? 2 : 1),
'LoadBalancerNames' => array(
array('Ref' => 'ntstaggerELB')
),
)
),
'ntstaggerLC' => array(
'Type' => 'AWS::AutoScaling::LaunchConfiguration',
'Properties' => array(
'InstanceType' => 'c1.xlarge',
'SecurityGroups' => array(
array('Ref' => 'ntstaggerSG'),
"ntsdb-$environment"
),
'NS:Name' => 'ntstagger',
)
),
'ntstaggerELB' => array(
'Type' => 'AWS::ElasticLoadBalancing::LoadBalancer',
"Properties" => array(
'Listeners' => array(
array(
"LoadBalancerPort" => "8080",
"InstancePort" => "8080",
"Protocol" => "HTTP",
)
),
"HealthCheck" => array(
"Target" => "HTTP:8080/new-nts/health-check",
"HealthyThreshold" => "5",
"UnhealthyThreshold"=> "2",
"Interval" => "10",
"Timeout" => "8"
)
)
),
'ntstaggerSG' => array(
"Type" => 'AWS::EC2::SecurityGroup',
"Properties" => array(
'SecurityGroupIngress' => array(
array(
"IpProtocol" => "tcp",
"FromPort" => "8080",
"ToPort" => "8080",
"SourceSecurityGroupName" => "amazon-elb-sg",
"SourceSecurityGroupOwnerId" => "amazon-elb"
)
)
)
),
/* Copied Auto Scaling rules below from previous nts.prd1.json file */
"ntstaggerScaleUpQty1" => array(
"Properties" => array(
"AdjustmentType" => "ChangeInCapacity",
"AutoScalingGroupName" => array(
"Ref" => "ntstaggerASG"
),
"ScalingAdjustment" => "1"
),
"Type" => "AWS::AutoScaling::ScalingPolicy"
),
"ntstaggerScaleUpQty2" => array(
"Properties" => array(
"AdjustmentType" => "ChangeInCapacity",
"AutoScalingGroupName" => array(
"Ref" => "ntstaggerASG"
),
"ScalingAdjustment" => "2"
),
"Type" => "AWS::AutoScaling::ScalingPolicy"
),
"ntstaggerScaleDownQty1" => array(
"Properties" => array(
"AdjustmentType" => "ChangeInCapacity",
"AutoScalingGroupName" => array(
"Ref" => "ntstaggerASG"
),
"ScalingAdjustment" => "-1"
),
"Type" => "AWS::AutoScaling::ScalingPolicy"
),
/* Not clear on the syntax or what to use for the AlarmActions json array...
comparing with targeting.cfn.php however for alarm actions variables are used */
"ntstaggerNumberOfIncomingMessagesHigh" => array(
"Properties" => array(
"AlarmActions" => array(
"Ref" => "ntstaggerScaleUpQty1"
),
"AlarmDescription" => "ntstaggerIncomingMessagesHigh",
"ComparisonOperator" => "GreaterThanThreshold",
"Dimensions" => array(
"Name" => "Totals",
"Value" => "NS/PRD/NTS"
),
"EvaluationPeriods" => "1",
"MetricName" => "ApproximateNumberOfMessagesBothQueues",
"Namespace" => "NS/PRD/NTS",
"Period" => "900",
"Statistic" => "Average",
"Threshold" => "1000"
),
"Type" => "AWS::CloudWatch::Alarm"
),
"ntstaggerNumberOfIncomingMessagesVeryHigh" => array(
"Properties" => array(
"AlarmActions" => array(
"Ref" => "WarningTopic"
), (
/* There's an issue with the line below. Not sure why. */
array("Ref" => "ntstaggerScaleUpQty2")
),
"AlarmDescription" => "ntstaggerIncomingMessagesVeryHigh",
"ComparisonOperator" => "GreaterThanThreshold",
"Dimensions" => array(
"Name" => "Totals",
"Value" => "NS/PRD/NTS"
),
"EvaluationPeriods" => "1",
"MetricName" => "ApproximateNumberOfMessagesBothQueues",
"Namespace" => "NS/PRD/NTS",
"Period" => "900",
"Statistic" => "Average",
"Threshold" => "10000"
),
"Type" => "AWS::CloudWatch::Alarm"
),
"ntstaggerNumberOfIncomingMessagesCritical" => array(
"Properties" => array(
"AlarmActions" => array(
"Ref" => "CriticalTopic"
),
"AlarmDescription" => "ntstaggerIncomingMessagesCritical",
"ComparisonOperator" => "GreaterThanThreshold",
"Dimensions" => array(
"Name" => "Totals",
"Value" => "NS/PRD/NTS"
),
"EvaluationPeriods" => "1",
"MetricName" => "ApproximateNumberOfMessagesBothQueues",
"Namespace" => "NS/PRD/NTS",
"Period" => "900",
"Statistic" => "Average",
"Threshold" => "100000"
),
"Type" => "AWS::CloudWatch::Alarm"
),
"ntstaggerNumberOfIncomingMessagesOK" => array(
"Properties" => array(
"AlarmActions" => array(
"Ref" => "ntstaggerScaleDownQty1"
),
"AlarmDescription" => "ntstaggerIncomingMessagesOK",
"ComparisonOperator" => "LessThanThreshold",
"Dimensions" => array(
"Name" => "Totals",
"Value" => "NS/PRD/NTS"
),
"EvaluationPeriods" => "1",
"MetricName" => "ApproximateNumberOfMessagesBothQueues",
"Namespace" => "NS/PRD/NTS",
"Period" => "900",
"Statistic" => "Average",
"Threshold" => "1000"
),
"Type" => "AWS::CloudWatch::Alarm"
),
"ntstaggerNumberOfIncomingMessagesZero" => array(
"Properties" => array(
"AlarmActions" => array(
"Ref" => "CriticalTopic"
),
"AlarmDescription" => "ntstaggerIncomingMessagesEmpty",
"ComparisonOperator" => "LessThanThreshold",
"Dimensions" => array(
"Name" => "Totals",
"Value" => "NS/PRD/NTS"
),
"EvaluationPeriods" => "1",
"MetricName" => "ApproximateNumberOfMessagesBothQueues",
"Namespace" => "NS/PRD/NTS",
"Period" => "900",
"Statistic" => "Maximum",
"Threshold" => "0"
),
"Type" => "AWS::CloudWatch::Alarm"
),
"Outputs" => array(
"ntstaggerURL" => array(
"Description" => "URL for the FrontEnd NTS service",
"Value" => array(
"Fn::GetAtt" => array("ntstaggerELB", "DNSName")
)
)
)
)
));
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment