3

我有一個Elastic Load Balancer已經配置了端口和SSL證書等,Route 53設置爲將我的網站流量路由到它。創建autoscaling網絡服務器組添加到現有elb

我想知道是否有一個示例cloudFormation模板創建一個ec2實例的自動縮放組,其中每個實例都添加到現有的負載均衡器中並從中移除。

我在網上查了一些例子 - 下面的例子似乎幾乎是我所需要的,但是它的問題(以及所有其他似乎使用這種變體的問題)是它假設你想創建一個新的負載平衡器。我不。

https://s3.amazonaws.com/cloudformation-templates-us-east-1/AutoScalingMultiAZWithNotifications.template

是否有可能,做我的建議?有人有一個例子嗎?

我的CloudFormation腳本如下所示(我刪除了實際的服務器軟件包配置部分)。這成功創建了一個新實例,但它不會添加到負載平衡器「load4」。我可以手動將主機添加到負載平衡器,但這顯然會失敗。

{ 
"AWSTemplateFormatVersion" : "2010-09-09", 

"Description" : "Create an Auto-scaling group that will attach to existing load balancer and inhereit existing security groups.", 

"Parameters" : { 
"KeyName" : { 
    "Description" : "mykeyname", 
    "Type" : "String" 
}, 

"InstanceType" : { 
    "Type" : "String", 
    "Default" : "m1.small", 
    "AllowedValues" : [ "m1.small", "m1.medium", "m1.large", "m1.xlarge", "m2.xlarge", "m2.2xlarge", "m2.4xlarge", "c1.xlarge", "cc1.4xlarge" ], 
    "Description" : "EC2 instance type (e.g. m1.large, m1.xlarge, m2.xlarge)" 
}, 
"SpotPrice": { 
    "Description": "Spot price for application AutoScaling Group", 
    "Type": "Number", 
    "MinValue" : ".03" 
}, 
"MinInstances" : { 
    "Description" : "The minimum number of Workers", 
    "Type" : "Number", 
    "MinValue" : "0", 
    "Default" : "0", 
    "ConstraintDescription" : "Enter a number >=0" 
}, 

"MaxInstances" : { 
    "Description" : "The maximum number of Workers", 
    "Type" : "Number", 
    "MinValue" : "1", 
    "Default" : "4", 
    "ConstraintDescription" : "Enter a number >1" 
}, 

"OperatorEmail": { 
    "Description": "Email address to notify if there are any scaling operations", 
    "Type": "String" 
} 
}, 

"Mappings" : { 
"AWSInstanceType2Arch" : { 
    "t1.micro" : { "Arch" : "64" }, 
    "m1.small" : { "Arch" : "64" }, 
    "m1.medium" : { "Arch" : "64" }, 
    "m1.large" : { "Arch" : "64" }, 
    "m1.xlarge" : { "Arch" : "64" }, 
    "m2.xlarge" : { "Arch" : "64" }, 
    "m2.2xlarge" : { "Arch" : "64" }, 
    "m2.4xlarge" : { "Arch" : "64" }, 
    "m3.xlarge" : { "Arch" : "64" }, 
    "m3.2xlarge" : { "Arch" : "64" }, 
    "c1.medium" : { "Arch" : "64" }, 
    "c1.xlarge" : { "Arch" : "64" }, 
    "cc1.4xlarge" : { "Arch" : "64HVM" }, 
    "cc2.8xlarge" : { "Arch" : "64HVM" }, 
    "cg1.4xlarge" : { "Arch" : "64HVM" } 
}, 

"AWSRegionArch2AMI" : { 
    "us-east-1"  : { "32" : "ami-31814f58", "64" : "ami-1b814f72", "64HVM" : "ami-0da96764" }, 
    "us-west-2"  : { "32" : "ami-38fe7308", "64" : "ami-30fe7300", "64HVM" : "NOT_YET_SUPPORTED" }, 
    "us-west-1"  : { "32" : "ami-11d68a54", "64" : "ami-1bd68a5e", "64HVM" : "NOT_YET_SUPPORTED" }, 
    "eu-west-1"  : { "32" : "ami-973b06e3", "64" : "ami-953b06e1", "64HVM" : "NOT_YET_SUPPORTED" }, 
    "ap-southeast-1" : { "32" : "ami-b4b0cae6", "64" : "ami-beb0caec", "64HVM" : "NOT_YET_SUPPORTED" }, 
    "ap-southeast-2" : { "32" : "ami-b3990e89", "64" : "ami-bd990e87", "64HVM" : "NOT_YET_SUPPORTED" }, 
    "ap-northeast-1" : { "32" : "ami-0644f007", "64" : "ami-0a44f00b", "64HVM" : "NOT_YET_SUPPORTED" }, 
    "sa-east-1"  : { "32" : "ami-3e3be423", "64" : "ami-3c3be421", "64HVM" : "NOT_YET_SUPPORTED" } 
} 
}, 

"Resources" : { 
"NotificationTopic": { 
    "Type": "AWS::SNS::Topic", 
    "Properties": { 
    "Subscription": [ { 
     "Endpoint": { "Ref": "OperatorEmail" }, 
     "Protocol": "email" } ] 
    } 
}, 

"WebServerGroup" : { 
    "Type" : "AWS::AutoScaling::AutoScalingGroup", 
    "Properties" : { 
    "AvailabilityZones" : { "Fn::GetAZs" : ""}, 
    "LaunchConfigurationName" : { "Ref" : "LaunchConfig" }, 
    "MinSize" : "0", 
    "MaxSize" : "4", 
    "LoadBalancerNames" : [ "load4" ],    
    "NotificationConfiguration" : { 
     "TopicARN" : { "Ref" : "NotificationTopic" }, 
     "NotificationTypes" : [ "autoscaling:EC2_INSTANCE_LAUNCH","autoscaling:EC2_INSTANCE_LAUNCH_ERROR","autoscaling:EC2_INSTANCE_TERMINATE", "autoscaling:EC2_INSTANCE_TERMINATE_ERROR"] 
    } 
    } 
}, 

"CfnUser" : { 
    "Type" : "AWS::IAM::User", 
    "Properties" : { 
     "Path": "/", 
     "Policies": [ { 
      "PolicyName": "root", 
      "PolicyDocument": { "Statement": [ { 
       "Effect":"Allow", 
       "Action":"cloudformation:DescribeStackResource", 
       "Resource":"*" 
      } ] } 
     } ] 
    } 
}, 

"HostKeys" : { 
    "Type" : "AWS::IAM::AccessKey", 
    "Properties" : { 
     "UserName" : { "Ref" : "CfnUser" } 
    } 
}, 

"LaunchConfig" : { 
    "Type" : "AWS::AutoScaling::LaunchConfiguration", 
    "Metadata" : { 
    "Comment" : "Create a single webserver", 
    "AWS::CloudFormation::Init" : { 
     "config" : { 
     "packages" : { 
      "yum" : { 

      } 
     }, 
     "files" : { 

     } 
     } 
    } 
    }, 
    "Properties" : { 
    "KeyName" : { "Ref" : "KeyName" }, 
    "SpotPrice" : { "Ref" : "SpotPrice" }, 
    "ImageId" : { "Fn::FindInMap" : [ "AWSRegionArch2AMI", { "Ref" : "AWS::Region" }, 
             { "Fn::FindInMap" : [ "AWSInstanceType2Arch", {  "Ref" : "InstanceType" }, 
             "Arch" ] } ] }, 
    "SecurityGroups" : [ "webserver" ], 
    "InstanceType" : { "Ref" : "InstanceType" }, 
    "UserData"  : { "Fn::Base64" : { "Fn::Join" : ["", [ 
     "#!/bin/bash\n", 
     "yum update -y aws-cfn-bootstrap\n", 
     "# Install the Worker application\n", 
     "/opt/aws/bin/cfn-init ", 
     "   --stack ", { "Ref" : "AWS::StackId" }, 
     "   --resource LaunchConfig ", 
     "   --configset ALL", 
     "   --region ", { "Ref" : "AWS::Region" }, "\n" 
    ]]}}   
    } 
}, 

"WorkerGroup" : { 
    "Type" : "AWS::AutoScaling::AutoScalingGroup", 
    "Properties" : { 
    "AvailabilityZones" : { "Fn::GetAZs" : ""}, 
    "LaunchConfigurationName" : { "Ref" : "LaunchConfig" }, 
    "MinSize" : { "Ref" : "MinInstances" }, 
    "MaxSize" : { "Ref" : "MaxInstances" } 
    } 
}, 


"WebServerScaleUpPolicy" : { 
    "Type" : "AWS::AutoScaling::ScalingPolicy", 
    "Properties" : { 
    "AdjustmentType" : "ChangeInCapacity", 
    "AutoScalingGroupName" : { "Ref" : "WorkerGroup" }, 
    "Cooldown" : "60", 
    "ScalingAdjustment" : "1" 
    } 
}, 
"WebServerScaleDownPolicy" : { 
    "Type" : "AWS::AutoScaling::ScalingPolicy", 
    "Properties" : { 
    "AdjustmentType" : "ChangeInCapacity", 
    "AutoScalingGroupName" : { "Ref" : "WorkerGroup" }, 
    "Cooldown" : "60", 
    "ScalingAdjustment" : "-1" 
    } 
}, ... 




    "WorkerThreadHigh": { 
    "Type": "AWS::CloudWatch::Alarm", 
    "Properties": { 
     "AlarmDescription": "Scale-up if Worker Thread Vs. Idle Percent > 80% for 10min", 
     "MetricName": "PctActiveWorkers", 
     "Namespace": "EC2", 
     "Statistic": "Average", 
     "Period": "300", 
     "EvaluationPeriods": "2", 
     "Threshold": "80", 
     "AlarmActions": [ { "Ref": "WebServerScaleUpPolicy" } ], 
     "Dimensions": [ 
     { 
      "Name": "AutoScalingGroupName", 
      "Value": { "Ref": "WebServerGroup" } 
     } 
     ], 
     "ComparisonOperator": "GreaterThanThreshold" 
    } 
    }, 
    "WorkerThreadLow": { 
    "Type": "AWS::CloudWatch::Alarm", 
    "Properties": { 
     "AlarmDescription": "Scale-down if CPU < 50% for 10 minutes", 
     "MetricName": "PctActiveWorkers", 
     "Namespace": "EC2", 
     "Statistic": "Average", 
     "Period": "300", 
     "EvaluationPeriods": "2", 
     "Threshold": "50", 
     "AlarmActions": [ { "Ref": "WebServerScaleDownPolicy" } ], 
     "Dimensions": [ 
     { 
      "Name": "AutoScalingGroupName", 
      "Value": { "Ref": "WebServerGroup" } 
     } 
     ], 
     "ComparisonOperator": "LessThanThreshold" 
    } 
    } 
} 

} 

回答

1

你不能將這兩者分開。我已經通過亞馬遜支持驗證了一個不相關的用例。它很爛。

這是我們的討論。 https://forums.aws.amazon.com/thread.jspa?messageID=362467&#362467亞馬遜最終沒有回覆他們的說法,我們不支持。

UPDATE我在下面的回答不再正確。亞馬遜增加了這個功能。查看其他討論。

+0

jeez,如果它確實真的吸吮!爲了防止別人可能有衝突的答案或其他漂亮的解決方法,我會在開放之前留出一段時間。謝謝。 – Ross

+0

我想現在我能想到的最佳解決方法是使用外部監視機制(腳本)來輪詢cloudwatch指標或一組指標,並添加/刪除instacnes以取代自動縮放的cloudformation ...不完全理想! – Ross

+0

這不再是事實。您可以使用LoadBalancerNames屬性(我剛剛驗證過)將實例添加到現有ELB中 - 請參閱@steffenopel。 – Rilindo

3

參數LoadBalancerNames只是表示與此自動縮放組關聯的負載平衡器列表。您參考樣本AWS CloudFormation模板(以及所有其他的例子我所知道的)有這個配置給LoadBalancer資源的結果如下:

"LoadBalancerNames": [ 
    { 
     "Ref": "ElasticLoadBalancer" 
    } 
], 

Ref函數的結果是部分定義

當這種資源的邏輯ID提供給參考內在 函數,它返回資源名稱:回在LoadBalancer底部的值。例如, mystack-myelb-1WQN7BJGDB5YQ。

這簡直是負載平衡器名稱如圖中AWS Management Console,因此你可以通過直接供給它的名字,例如使用CloudFormation之外創建任何Elastic Load Balancer

"LoadBalancerNames": [ "existing-load-balancer-1" ], 
+0

你做到了嗎?我真的很想知道這是否有效。 –

+0

我已經成功地使用手動創建的負載均衡器來啓動引用的模板,但尚未在生產環境中使用它,因此不知道這種方法是否存在任何隱藏問題(但從概念角度來看,沒有任何)。 –

+0

@JohnHinnegan - 我會試試這個,讓你知道並接受它,如果它的工作。 – Ross

1

我可以證實,以下的作品,當加入的 「類型」 的資源的配置: 「AWS ::自動縮放:: AutoScalingGroup」:

"LoadBalancerNames" : [ "YourELBNameHere" ] 

,或者,如果你擁有名作爲參數,

"LoadBalancerNames" : [ {"Ref" : "YourELBParameterNameHere"} ] 
  • 由ASG創建的實例被添加到/與ELB註冊,並刪除/自動註銷,如果你TERMINAT e堆棧。