gpt4 book ai didi

amazon-ec2 - AWS CloudFormation - 使用 cfn-init 跨 EC2 共享用户数据脚本

转载 作者:行者123 更新时间:2023-12-03 07:45:52 25 4
gpt4 key购买 nike

我有一个这样的实例资源:

testservice:
Type: 'AWS::EC2::Instance'
Metadata:
AWS::CloudFormation::Init:
configSets:
serviceConfigSet:
- serviceConfig
serviceConfig:
packages:
yum:
salt-minion: []
commands:
hostnameConfig:
command: hostname test-service-1.0
services:
sysvinit:
salt-minion:
enabled: 'true'
ensureRunning: 'true'
Properties:
ImageId: !Ref BaseServiceAmi
KeyName: test_ssh_key
SubnetId: !Ref ServiceSubnetId
UserData:
'Fn::Base64':
'Fn::Sub': |
#!/bin/bash

yum update -y

yum install -y aws-cfn-bootstrap

/opt/aws/bin/cfn-init -v --stack ${AWS::StackName} --resource testservice --configsets serviceConfigSet --region ${AWS::Region}

但是,我想将 UserData 脚本移至参数中并在多个实例资源中重用它。这应该是微不足道的,但我找不到在用户数据脚本中获取当前资源名称的方法(我已在这部分中显式设置它:--resource testservice)。

有没有一种方法可以通过用户数据脚本内部的引用获取资源名称,以便我可以概括它?它不会出现在伪参数列表中。

最佳答案

我相信最好的选择是使用嵌套堆栈。如果您可以将“AWS::EC2::Instance”资源中的所有设置转换为参数,则可以为该资源创建模板堆栈并从另一个模板调用它。

快速信息: http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/best-practices.html#stackpolicy

使用方法: http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-stack.html

关于amazon-ec2 - AWS CloudFormation - 使用 cfn-init 跨 EC2 共享用户数据脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44594232/

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