我正在使用cloudformation模板来创建我的EC2实例。在userdata部分中,我需要运行我在元数据中创建的shell文件。对于那个shell文件,我将实例的私有ip作为参数传递。 要获得私人IP,我正在使用它:
{
"Fn::GetAtt" : [ "ConsoleServer", "PrivateIp" ]
},
我要求等待处理程序在我的用户数据执行时等待,但等待处理程序依赖于我正在尝试配置的EC2。
这导致了cicular依赖,但是我无法理解如何使用其他方式获取实例的私有ip?
以下是重要的部分:Metadata
"Resources": {
"ConsoleServer": {
"Type": "AWS::EC2::Instance",
"Metadata": {
"AWS::CloudFormation::Init": {
"config": {
"files": {
"/usr/local/share/deployment-script.sh": {
"mode": "755",
"owner": "ec2-user",
"group": "ec2-user",
"content": {
"Fn::Join": [
"",
[
"#!/bin/bash\n",
"sh master.sh ",
{
"Ref": "S3ConsoleZip"
}, " ",
{
"Fn::GetAtt" : [ "ConsoleServer", "PrivateIp" ]
},
这是我的 userdata 部分,后跟 waithandler
"UserData": {
"Fn::Base64": {
"Fn::Join": [
"",
[
"#!/bin/bash -v\n",
"sudo su",
"\n",
"chmod -R 775 /usr/local/share\n",
"yum update -y aws-cfn-bootstrap\n",
"## Error reporting helper function\n",
"function error_exit\n",
"{\n",
" /opt/aws/bin/cfn-signal -e 1 -r \"$1\" '",
{
"Ref": "WaitHandleServer"
},
"'\n",
" exit 1\n",
"}\n",
"## Initialize CloudFormation bits\n",
"/opt/aws/bin/cfn-init -v -s ",
{
"Ref": "AWS::StackName"
},
" -r ConsoleServer",
" --region ",
{
"Ref": "AWS::Region"
},
" > /tmp/cfn-init.log 2>&1 || error_exit $(</tmp/cfn-init.log)\n",
"cd /usr/local/share\n",
********* "sh deployment-script.sh >> /home/ec2-user/deployment-script.log\n",
"/opt/aws/bin/cfn-signal",
" -e 0",
" '",
{
"Ref": "WaitHandleServer"
},
"'",
"\n",
"date > /home/ec2-user/stoptime"
]
]
}
}
}
},
"WaitHandleServer": {
"Type": "AWS::CloudFormation::WaitConditionHandle"
},
"WaitConditionServer": {
"Type": "AWS::CloudFormation::WaitCondition",
"DependsOn": "ConsoleServer",
"Properties": {
"Handle": {
"Ref": "WaitHandleServer"
},
"Timeout": "1200"
}
}
},
我添加了*********,其中正在从用户数据部分进行调用
答案 0 :(得分:4)
要获取EC2 IP地址,您可以通过在脚本中使用curl
调用此API来获取它:
curl http://169.254.169.254/latest/meta-data/local-ipv4