gpt4 book ai didi

amazon-web-services - 对多个实例重用 AWS::CloudFormation::Init (和用户数据?)

转载 作者:行者123 更新时间:2023-12-04 14:12:18 24 4
gpt4 key购买 nike

是否可以为多个EC2::Instance<重复使用来自AWS::CloudFormation::Init(和/或userdata)的相同引导配置在模板中?

我需要设置 3 个文件的内容,然后运行 ​​3 个命令来引导所有服务器,但是 Metadata block 大约有 30 行长(并且可能会增长)。 每个服务器实例都有一组不同的标签,有些实例比其他实例拥有更多标签。

理想情况下,我认为您应该能够将 AWS::CloudFormation::Init 声明为资源,并从多个 EC2::Instance 引用它s,但我认为这是不可能的。

我最初认为(作为新手)AWS::CloudFormation::CustomResource 可能合适,但我认为事实并非如此。

我目前正在考虑使用 AWS::CloudFormation::Stack 导入共享实例模板,但我需要以某种方式传递 Tags 参数将堆栈模板中的每个资源放入实例模板中。问题是 - 如果这是最好的方法,我应该在当前有 ???? 的 3 个位置中输入什么?

(额外积分 - userdata 和这个 init block 之间有什么区别?)

堆栈.模板

...
"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" : [
????
]
}
}
}

最佳答案

Is it possible to reuse the same bootstrapping config from AWS::CloudFormation::Init (and/or userdata) for multiple EC2::Instances in a template?

不,对于单个模板中的 AWS::EC2::Instance 资源,这是不可能的。但是,有一个资源类型 AWS::AutoScaling::LaunchConfiguration ,但目前此资源仅适用于 Auto Scaling 组。理想情况下,AWS 将提供可应用于多个 AWS::EC2::Instance 资源的类似资源类型。话虽这么说,有很多value in using Auto Scaling groups .

这是一个简单的示例,允许您在单个模板中使用单个启动配置和多个 Auto Scaling 组资源来执行此操作。自动扩展通常配置多个实例,但我使用 MinSize 和 MaxSize 为 1 来镜像您使用 AWS::EC2::Instance 资源类型进行的配置。即使我们在 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 组都定义了自己的一组标签。


I'm currently thinking of using a AWS::CloudFormation::Stack to import a shared instance template, but I would need to somehow pass the Tags parameter for each resource in the stack template into the instance template. The question is - if this is the best approach, what do I enter in the 3 locations that currently have ?????

我会推荐上面描述的解决方案,但我也想回答您有关如何通过嵌套堆栈方法使用标签的问题。

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"
}
}
}
}
}

实例.模板

{
"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" } },
]
}
}
}
}

没有将整个标签数组作为参数传递的标准,因此您可以看到我只是将每个标签分解为自己的参数并将它们传递到嵌套堆栈。


(Bonus credit - what's the difference between userdata and this init block?)

UserData 允许您在首次启动时将任意数据传递给实例。通常,这是一个 shell 脚本,可以在实例启动时自动执行任务。例如,您可以简单地运行 yum update。

"UserData" : { "Fn::Base64" : { "Fn::Join" : [ "", [
"#!/bin/bash\n"
"yum update -y", "\n"
]]}}

AWS::CloudFormation::Init 结合使用时,UserData 变得更加有用元数据允许您构建引导配置。在本例中,UserData 仅用于调用执行 AWS::CloudFormation::Init 元数据的 cfn-init 脚本。我已使用启动配置在上面的第一个示例中包含此模式。需要注意的是,UserData 部分在实例首次启动期间仅执行一次。在考虑如何处理实例更新时,请记住这一点很重要。

关于amazon-web-services - 对多个实例重用 AWS::CloudFormation::Init (和用户数据?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27499509/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com