[英]LaunchConfiguration Userdata vs AWS::CloudFormation::Init
[英]Reusing AWS::CloudFormation::Init (and userdata?) for multiple instances
是否可以在AWS::CloudFormation::Init
(和/或userdata
)中為模板中的多個EC2::Instance
重用相同的bootstrapping配置?
我需要設置3個文件的內容,然后運行3個命令來引導所有服務器,但Metadata
塊大約有30行(並且可能會增長)。 每個服務器實例都有一組不同的標簽,有些標簽比其他標簽更多。
理想情況下,我認為您應該能夠將AWS::CloudFormation::Init
聲明為資源,並從多個EC2::Instance
引用它,但我不認為這是可能的。
我最初認為(作為新手) AWS::CloudFormation::CustomResource
可能是合適的,但我認為不是。
我目前正在考慮使用AWS::CloudFormation::Stack
來導入共享實例模板,但我需要以某種方式將堆棧模板中每個資源的Tags
參數傳遞給實例模板。 問題是 - 如果這是最好的方法,我在目前擁有的3個地點輸入什么????
?
(獎勵積分 - userdata
和這個init
塊之間有什么區別?)
...
"Resources" : {
"Server1" : {
"Type": "AWS::CloudFormation::Stack",
"Properties": {
"Parameters": {
"InstanceType": "m1.medium",
...
"Tags": { ???? }
},
"TemplateURL": "https://s3.amazonaws.com/mybucket/instance.template"
}
...
"Parameters" : {
"InstanceType" : {...}
"KeyName": {...}
...
"Tags": {
????
}
},
"Resources" : {
"Instance" : {
"Type" : "AWS::EC2::Instance",
"Metadata" : {
"AWS::CloudFormation::Init" : {
"config" : {
"files" : {
"/etc/apt/apt.conf.d/99auth" : {
"content" : "APT::Get::AllowUnauthenticated yes;"
},
"/etc/apt/sources.list.d/my-repo.list" : {
"content" : "deb http://my-repo/repo apt/"
},
},
"commands" : {
"01-apt-get update" : {
"command" : "apt-get update"
},
"02-apt-get install puppet" : {
"command" : "apt-get install puppet my-puppet-config"
},
"03-puppet apply" : {
"command" : "puppet apply"
}
}
}
}
},
"Properties" : {
"InstanceType" : {"Ref" : "InstanceType"},
"ImageId" : "ami-84a333be",
"KeyName" : {"Ref" : "KeyName"},
"SubnetId" : {"Ref" : "SubnetId"},
"SecurityGroupIds" : [ { "Ref" : "SecurityGroupId"] } ],
"Tags" : [
????
]
}
}
}
是否可以在AWS :: CloudFormation :: Init(和/或userdata)中為模板中的多個EC2 :: Instances重用相同的bootstrapping配置?
不,在單個模板中使用AWS :: EC2 :: Instance資源是不可能的。 但是,有一種資源類型AWS :: AutoScaling :: LaunchConfiguration ,但今天此資源僅適用於Auto Scaling組。 理想情況下,AWS將提供可應用於多個AWS :: EC2 :: Instance資源的類似資源類型。 話雖這么說, 使用Auto Scaling組有很多價值 。
這是一個簡單的示例,允許您在單個模板中使用單個啟動配置和多個Auto Scaling組資源執行此操作。 自動擴展通常配置有多個實例,但我使用MinSize和MaxSize為1來鏡像您使用AWS :: EC2 :: Instance資源類型進行的配置。 即使我們使用Auto Scaling組的單個實例,我們仍然可以通過單實例彈性獲得Auto Scaling的優勢。 如果實例變得不健康,Auto Scaling將自動替換實例。
{
"AWSTemplateFormatVersion": "2010-09-09",
"Resources" : {
"LaunchConfig" : {
"Type" : "AWS::AutoScaling::LaunchConfiguration",
"Properties" : {
"InstanceType" : { "Ref" : "InstanceType" },
"ImageId" : "ami-84a333be",
"KeyName" : { "Ref" : "KeyName" },
"SecurityGroupIds" : [{"Ref" : "SecurityGroupId"}],
"UserData" : { "Fn::Base64" : { "Fn::Join" : [ "", [
"#!/bin/bash -v\n",
"# Run cfn-init\n",
"/opt/aws/bin/cfn-init -v ",
" -stack ", { "Ref": "AWS::StackName" },
" -resource LaunchConfig ",
" --region ", { "Ref" : "AWS::Region" }, "\n",
"# Signal success\n",
"/opt/aws/bin/cfn-signal -e $? '", { "Ref" : "WaitConditionHandle" }, "'\n"
]]}}
},
"Metadata" : {
"AWS::CloudFormation::Init" : {
"config" : {
"files" : {
"/etc/apt/apt.conf.d/99auth" : {
"content" : "APT::Get::AllowUnauthenticated yes;"
},
"/etc/apt/sources.list.d/my-repo.list" : {
"content" : "deb http://my-repo/repo apt/"
}
},
"commands" : {
"01-apt-get update" : {
"command" : "apt-get update"
},
"02-apt-get install puppet" : {
"command" : "apt-get install puppet my-puppet-config"
},
"03-puppet apply" : {
"command" : "puppet apply"
}
}
}
}
}
},
"ASG1" : {
"Type" : "AWS::AutoScaling::AutoScalingGroup",
"Properties" : {
"AvailabilityZones" : [ { "Ref" : "AZ" } ],
"VPCZoneIdentifier" : [ { "Ref" : "SubnetId" } ],
"LaunchConfigurationName" : { "Ref" : "LaunchConfig" },
"MaxSize" : "1",
"MinSize" : "1",
"Tags" : [
{ "Key" : "Name", "Value": "Server1", "PropagateAtLaunch" : "true" },
{ "Key" : "Version", "Value": "1.0", "PropagateAtLaunch" : "true" }
]
}
},
"ASG2" : {
"Type" : "AWS::AutoScaling::AutoScalingGroup",
"Properties" : {
"AvailabilityZones" : [ { "Ref" : "AZ" } ],
"VPCZoneIdentifier" : [ { "Ref" : "SubnetId" } ],
"LaunchConfigurationName" : { "Ref" : "LaunchConfig" },
"MaxSize" : "1",
"MinSize" : "1",
"Tags" : [
{ "Key" : "Name", "Value": "Server2", "PropagateAtLaunch" : "true" },
{ "Key" : "Version", "Value": "1.0", "PropagateAtLaunch" : "true" }
]
}
},
"WaitConditionHandle" : {
"Type" : "AWS::CloudFormation::WaitConditionHandle"
},
"WaitCondition" : {
"Type" : "AWS::CloudFormation::WaitCondition",
"Properties" : {
"Handle" : { "Ref" : "WaitConditionHandle" },
"Timeout" : "300"
}
}
}
}
這是一個不完整的示例,您可以使用Auto Scaling做更多的事情,但我只想給您一個簡單的示例,與多個實例共享啟動配置。 每個Auto Scaling組都會定義自己的一組標記。
我目前正在考慮使用AWS :: CloudFormation :: Stack來導入共享實例模板,但我需要以某種方式將堆棧模板中每個資源的Tags參數傳遞給實例模板。 問題是 - 如果這是最好的方法,我在目前擁有的3個地點輸入什么?
我會推薦上面描述的解決方案,但我也想回答一些關於如何使用嵌套堆棧方法的問題的問題。
stack.template
{
"AWSTemplateFormatVersion": "2010-09-09",
"Resources" : {
"Server1" : {
"Type": "AWS::CloudFormation::Stack",
"Properties": {
"TemplateURL": "https://s3.amazonaws.com/mybucket/instance.template",
"Parameters": {
"InstanceType": "m1.medium",
"TagName": "Server1"
"TagVersion": "1.0"
}
}
},
"Server2" : {
"Type": "AWS::CloudFormation::Stack",
"Properties": {
"TemplateURL": "https://s3.amazonaws.com/mybucket/instance.template",
"Parameters": {
"InstanceType": "m1.medium",
"TagName": "Server2"
"TagVersion": "1.0"
}
}
}
}
}
instance.template
{
"AWSTemplateFormatVersion": "2010-09-09",
"Parameters" : {
"InstanceType" : {...},
"KeyName": {...}
"TagName": {
"Description" : "The name tag to be applied to each instance",
"Type" : "String"
},
"TagVersion": {
"Description" : "The version tag to be applied to each instance",
"Type" : "String"
}
},
"Resources" : {
"Instance" : {
"Type" : "AWS::EC2::Instance",
"Metadata" : {
"AWS::CloudFormation::Init" : {
...
}
},
"Properties" : {
"InstanceType" : { "Ref" : "InstanceType" },
"ImageId" : "ami-84a333be",
"KeyName" : { "Ref" : "KeyName" },
"SubnetId" : { "Ref" : "SubnetId" },
"SecurityGroupIds" : [ { "Ref" : "SecurityGroupId"] } ],
"Tags" : [
{ "Key" : "Name", "Value": { "Ref" : "TagName" } },
{ "Key" : "Version", "Value": { "Ref" : "TagVersion" } },
]
}
}
}
}
將整個標記數組作為參數傳遞沒有標准,因此您可以看到我只是將每個標記分解為自己的參數並將它們傳遞給嵌套堆棧。
(獎勵積分 - userdata和這個init塊之間有什么區別?)
UserData允許您在首次啟動時將任意數據傳遞給實例。 通常,這是一個shell腳本,可以在實例啟動時自動執行任務。 例如,您可以簡單地運行yum更新。
"UserData" : { "Fn::Base64" : { "Fn::Join" : [ "", [
"#!/bin/bash\n"
"yum update -y", "\n"
]]}}
當與AWS :: CloudFormation :: Init元數據結合使用時,UserData變得更加有用,它允許您構建引導配置。 在這種情況下,UserData僅用於調用執行AWS :: CloudFormation :: Init元數據的cfn-init腳本。 我在上面的第一個例子中使用Launch Configuration包含了這個模式。 請務必注意,UserData部分僅在第一次引導實例期間執行一次。 在考慮如何處理實例更新時,請務必牢記這一點。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.