Permalink
a3993a6 Aug 15, 2016
Fabrizio Branca Fixed demo
executable file 152 lines (138 sloc) 4.67 KB
{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "Green/Blue experiment",
"Parameters": {
"Subnets": { "Type": "List<AWS::EC2::Subnet::Id>"},
"KeyName": { "Type": "AWS::EC2::KeyPair::KeyName"},
"Vpc": { "Type": "AWS::EC2::VPC::Id"},
"GreenBlueSwitcherArn": { "Type": "String"}
},
"Mappings": {
/* Amazon Linux (HVM64) */
"AWSRegion2AMI": {
"us-west-2": {"AMI": "ami-d0f506b0"},
"eu-west-1": {"AMI": "ami-f9dd458a"}
}
},
"Resources": {
"SecurityGroup": {
"Type": "AWS::EC2::SecurityGroup",
"Properties": {
"VpcId": {"Ref": "Vpc"},
"GroupDescription": "Test",
"SecurityGroupIngress": [
{"IpProtocol": "tcp", "FromPort": "80", "ToPort": "80", "CidrIp": "0.0.0.0/0"}
]
}
},
"LoadBalancer": {
"Type": "AWS::ElasticLoadBalancing::LoadBalancer",
"Properties": {
"Listeners": [{"LoadBalancerPort": 80, "Protocol": "HTTP", "InstancePort": 80, "InstanceProtocol": "HTTP"}],
"CrossZone": true,
"HealthCheck": {
"Target": "TCP:80",
"HealthyThreshold": "2",
"UnhealthyThreshold": "10",
"Interval": "6",
"Timeout": "5"
},
"Subnets": {"Ref": "Subnets"},
"SecurityGroups": [ {"Ref": "SecurityGroup"} ],
"LoadBalancerName": "test-elb"
}
},
"AsgGreen": {
"Type": "AWS::AutoScaling::AutoScalingGroup",
"Properties": {
"LaunchConfigurationName": {"Ref": "LcGreen"},
"MinSize": 1,
"DesiredCapacity": 1,
"MaxSize": 13,
"LoadBalancerNames": [{"Ref": "LoadBalancer"}],
"VPCZoneIdentifier": {"Ref": "Subnets"},
"Tags": [{"Key": "Name", "PropagateAtLaunch": true, "Value": "Green"}]
}
},
"LcGreen": {
"Type": "AWS::AutoScaling::LaunchConfiguration",
"Properties": {
"AssociatePublicIpAddress": true,
"ImageId": {"Fn::FindInMap" : [ "AWSRegion2AMI", {"Ref":"AWS::Region"}, "AMI"]},
"InstanceType": "t2.nano",
"InstanceMonitoring": true,
"SecurityGroups": [{"Ref": "SecurityGroup"}],
"KeyName": {"Ref": "KeyName"},
"UserData": { "Fn::Base64": { "Fn::Join": [ "", [
"#!/usr/bin/env bash\n",
"yum install -y httpd24\n",
"echo 'Green' > /var/www/html/index.html\n",
"sleep 180\n",
"/opt/aws/bin/cfn-signal --exit-code $? '", { "Ref": "WaitHandleGreen" }, "'\n"
]]}}
}
},
"WaitHandleGreen" : { "Type" : "AWS::CloudFormation::WaitConditionHandle" },
"WaitConditionGreen" : {
"Type" : "AWS::CloudFormation::WaitCondition",
"Properties" : {
"Handle": { "Ref" : "WaitHandleGreen" },
"Timeout": "300",
"Count" : 1
}
},
"AsgBlue": {
"Type": "AWS::AutoScaling::AutoScalingGroup",
"Properties": {
"LaunchConfigurationName": {"Ref": "LcBlue"},
"MinSize": 1,
"DesiredCapacity": 1,
"MaxSize": 13,
/* This ASG is not attached to the Load Balancer initially, but will be attached via the Lambda function later */
"VPCZoneIdentifier": {"Ref": "Subnets"},
"Tags": [{"Key": "Name", "PropagateAtLaunch": true, "Value": "Blue"}]
}
},
"LcBlue": {
"Type": "AWS::AutoScaling::LaunchConfiguration",
"Properties": {
"AssociatePublicIpAddress": true,
"ImageId": {"Fn::FindInMap" : [ "AWSRegion2AMI", {"Ref":"AWS::Region"}, "AMI"]},
"InstanceType": "t2.nano",
"InstanceMonitoring": true,
"SecurityGroups": [{"Ref": "SecurityGroup"}],
"KeyName": {"Ref": "KeyName"},
"UserData": { "Fn::Base64": { "Fn::Join": [ "", [
"#!/usr/bin/env bash\n",
"yum install -y httpd24\n",
"echo 'Blue' > /var/www/html/index.html\n",
"/opt/aws/bin/cfn-signal --exit-code $? '", { "Ref": "WaitHandleBlue" }, "'\n"
]]}}
}
},
"WaitHandleBlue" : { "Type" : "AWS::CloudFormation::WaitConditionHandle" },
"WaitConditionBlue" : {
"Type" : "AWS::CloudFormation::WaitCondition",
"Properties" : {
"Handle": { "Ref" : "WaitHandleBlue" },
"Timeout": "300",
"Count" : 1
}
},
"SwitchToBlue": {
"DeletionPolicy": "Retain",
"DependsOn": [ "WaitConditionBlue", "WaitConditionGreen"],
"Type": "Custom::GreenBlueSwitcher",
"Properties": {
"ServiceToken": {"Ref": "GreenBlueSwitcherArn"},
"LoadBalancerName": { "Ref": "LoadBalancer" },
"AutoScalingGroupName": { "Ref": "AsgBlue" }
}
}
},
"Outputs": {
"LoadbalancerDNSName": {
"Value": {"Fn::GetAtt": ["LoadBalancer", "DNSName"]}
}
}
}