【问题标题】:Circular dependency in aws stack because of private ip [AWS CloudFormation]由于私有 ip [AWS CloudFormation],aws 堆栈中的循环依赖
【发布时间】:2015-10-23 05:32:51
【问题描述】:

我正在使用 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 \"$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"
            }
        }
    },

我已添加 ********* 正在从用户数据部分进行调用的位置

【问题讨论】:

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


    【解决方案1】:

    要获取 EC2 IP 地址,您可以通过在脚本中使用 curl 调用此 API 来获取它:

    curl http://169.254.169.254/latest/meta-data/local-ipv4
    

    阅读更多关于Instance Metadata and User Data的信息。

    【讨论】:

    • 终于有解决方案了,谢谢,我无法用言语表达我的感激之情。非常感谢。
    猜你喜欢
    • 2020-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-06
    • 2021-04-16
    • 2016-10-02
    • 2018-10-13
    • 2021-04-17
    相关资源
    最近更新 更多