AWS CloudFormation - 在 EC2 之间使用 cfn-init 共享用户数据脚本



我有这样的实例资源:

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

最新更新