使用 cloudformation (yaml) 在 Ubuntu 实例中启动 cfn-init

Den*_*boy 6 yaml amazon-web-services aws-cloudformation

我尝试使用以下命令启动 cfn-init:

Fn::Base64: !Sub | 
  #!/bin/bash
  sudo apt-get -y install python-setuptools
  mkdir aws-cfn-bootstrap-latest
  curl https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-latest.tar.gz | tar xz -C aws-cfn-bootstrap-latest --strip-components 1
  sudo easy_install aws-cfn-bootstrap-latest
  sudo /usr/local/bin/cfn-init --stack !Ref 'AWS::StackName' --resource xxx --region !Ref 'AWS::Region'
Run Code Online (Sandbox Code Playgroud)

第一步有效。我可以访问该实例并cfn-init已安装。当我在 ec2 实例内执行cfn-init --stack..命令时,当我对 stackname 和region 的值进行硬编码时,它工作得很好。

如何让它在yaml脚本中运行?它似乎无法读取 StackName 和区域的值。

Mai*_*KaY 4

如果您正在使用,!Sub则需要将变量包装起来,${}而不是使用!Ref

尝试这个

Fn::Base64: !Sub | 
  #!/bin/bash
  sudo apt-get -y install python-setuptools
  mkdir aws-cfn-bootstrap-latest
  curl https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-latest.tar.gz | tar xz -C aws-cfn-bootstrap-latest --strip-components 1
  sudo easy_install aws-cfn-bootstrap-latest
  sudo /usr/local/bin/cfn-init --stack ${AWS::StackName} --resource xxx --region ${AWS::Region}
Run Code Online (Sandbox Code Playgroud)

如果您指定模板参数名称或资源逻辑 ID(例如 ${InstanceTypeParameter}),AWS CloudFormation 返回的值与您使用 Ref 内部函数时所返回的值相同。如果您指定资源属性,例如 ${MyInstance.PublicIp},AWS CloudFormation 将返回与使用 Fn::GetAtt 内部函数相同的值。

有关更多详细信息,请检查AWS - Fn::Sub