【问题标题】:Start cfn-init in Ubuntu instance with cloudformation (yaml)使用 cloudformation (yaml) 在 Ubuntu 实例中启动 cfn-init
【发布时间】:2018-09-21 03:19:21
【问题描述】:

我尝试使用以下命令启动 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'

第一步有效。我可以访问该实例并安装了cfn-init。当我在我的 ec2 实例中执行 cfn-init --stack.. 命令时,当我硬编码堆栈名称和区域的值时,它工作正常。

如何使它在yaml 脚本中工作?似乎无法读取 StackName 和 region 的值。

【问题讨论】:

    标签: amazon-web-services yaml amazon-cloudformation


    【解决方案1】:

    如果您使用!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}
    

    如果您指定模板参数名称或资源逻辑 ID,例如 作为 ${InstanceTypeParameter},AWS CloudFormation 返回相同 值,就好像您使用了 Ref 内在函数一样。如果您指定 资源属性,例如 ${MyInstance.PublicIp}、AWS CloudFormation 返回与您使用 Fn::GetAtt 相同的值 内在函数。

    更多详情请查看AWS - Fn::Sub

    【讨论】:

    • 我用 ubuntu 试过了。没有工作。有什么解决办法吗?
    【解决方案2】:
    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}
    
    

    如果 easy_install 不适用于 ubuntu 18.04,请使用以下命令:

    python /usr/lib/python2.7/dist-packages/easy_install.py aws-cfn-bootstrap-latest
    

    【讨论】:

      猜你喜欢
      • 2019-07-08
      • 1970-01-01
      • 1970-01-01
      • 2016-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-13
      • 2019-06-29
      相关资源
      最近更新 更多