由于私有 ip [AWS CloudFormation],aws 堆栈中的循环依赖
Circular dependency in aws stack because of private ip [AWS CloudFormation]
我正在使用 cloudformation 模板创建我的 EC2 实例。在用户数据部分,我需要 运行 我在元数据中创建的 shell 文件。对于那个 shell 文件,我将实例的私有 ip 作为参数传递。
要获取私有 ip,我正在使用它:
{
"Fn::GetAtt" : [ "ConsoleServer", "PrivateIp" ]
},
我要求等待处理程序在执行我的用户数据时等待,但等待处理程序取决于我尝试配置的 EC2。
这导致了循环依赖,但我无法理解如何使用其他方式获取实例的私有 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 \"\" '",
{
"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"
}
}
},
我添加了 ********* 从用户数据部分进行调用的地方
要获取 EC2 IP 地址,您可以通过在脚本中使用 curl
调用此 API 来获取它:
curl http://169.254.169.254/latest/meta-data/local-ipv4
我正在使用 cloudformation 模板创建我的 EC2 实例。在用户数据部分,我需要 运行 我在元数据中创建的 shell 文件。对于那个 shell 文件,我将实例的私有 ip 作为参数传递。 要获取私有 ip,我正在使用它:
{
"Fn::GetAtt" : [ "ConsoleServer", "PrivateIp" ]
},
我要求等待处理程序在执行我的用户数据时等待,但等待处理程序取决于我尝试配置的 EC2。
这导致了循环依赖,但我无法理解如何使用其他方式获取实例的私有 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 \"\" '",
{
"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"
}
}
},
我添加了 ********* 从用户数据部分进行调用的地方
要获取 EC2 IP 地址,您可以通过在脚本中使用 curl
调用此 API 来获取它:
curl http://169.254.169.254/latest/meta-data/local-ipv4