使用 cloudformation (yaml) 在 Ubuntu 实例中启动 cfn-init
Start cfn-init in Ubuntu instance with cloudformation (yaml)
我尝试使用以下命令启动 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..
命令时,当我对 stackname 和 region 的值进行硬编码时它工作正常。
如何让它在 yaml
脚本中工作?它似乎无法读取 StackName 和区域的值。
如果您使用 !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}
If you specify template parameter names or resource logical IDs, such
as ${InstanceTypeParameter}, AWS CloudFormation returns the same
values as if you used the Ref intrinsic function. If you specify
resource attributes, such as ${MyInstance.PublicIp}, AWS
CloudFormation returns the same values as if you used the Fn::GetAtt
intrinsic function.
有关详细信息,请查看 AWS - Fn::Sub
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
如果找不到 easy_install 命令,那么试试这个对我有用
"UserData": {
"Fn::Base64": { "Fn::Join":["", [
"#!/bin/bash\n",
"apt-get update\n",
"apt-get install -y python-setuptools\n",
"mkdir -p /opt/aws/bin\n",
"apt-get install -y wget\n",
"wget https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-py3-latest.tar.gz\n",
"python3 -m easy_install --script-dir /opt/aws/bin aws-cfn-bootstrap-py3-latest.tar.gz\n",
"/usr/local/bin/cfn-init --stack ", { "Ref":"AWS::StackName" }, " --resource WebServer", " --region ", { "Ref": "AWS::Region" }, "\n",
"\n",
"/usr/local/bin/cfn-signal --exit-code $? '", { "Ref" : "WaitHandle" }, "'\n"
]]}
}
我尝试使用以下命令启动 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..
命令时,当我对 stackname 和 region 的值进行硬编码时它工作正常。
如何让它在 yaml
脚本中工作?它似乎无法读取 StackName 和区域的值。
如果您使用 !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}
If you specify template parameter names or resource logical IDs, such as ${InstanceTypeParameter}, AWS CloudFormation returns the same values as if you used the Ref intrinsic function. If you specify resource attributes, such as ${MyInstance.PublicIp}, AWS CloudFormation returns the same values as if you used the Fn::GetAtt intrinsic function.
有关详细信息,请查看 AWS - Fn::Sub
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
如果找不到 easy_install 命令,那么试试这个对我有用
"UserData": {
"Fn::Base64": { "Fn::Join":["", [
"#!/bin/bash\n",
"apt-get update\n",
"apt-get install -y python-setuptools\n",
"mkdir -p /opt/aws/bin\n",
"apt-get install -y wget\n",
"wget https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-py3-latest.tar.gz\n",
"python3 -m easy_install --script-dir /opt/aws/bin aws-cfn-bootstrap-py3-latest.tar.gz\n",
"/usr/local/bin/cfn-init --stack ", { "Ref":"AWS::StackName" }, " --resource WebServer", " --region ", { "Ref": "AWS::Region" }, "\n",
"\n",
"/usr/local/bin/cfn-signal --exit-code $? '", { "Ref" : "WaitHandle" }, "'\n"
]]}
}