由于专用IP,aws堆栈中的循环依赖性[AWS CloudFormation] [英] Circular dependency in aws stack because of private ip [AWS CloudFormation]

查看:91
本文介绍了由于专用IP,aws堆栈中的循环依赖性[AWS CloudFormation]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用cloudformation模板创建EC2实例。在userdata部分中,我需要运行在元数据中创建的shell文件。对于该外壳文件,我将实例的私有ip作为参数传递。
我要使用以下私有IP:

I am using a cloudformation template to create my EC2 instance. In userdata section i need to run a shell file that i have created in metadata. For that shell file i am passing private ip of the instance as a parameter. To get the private ip i am using this :

{
    "Fn::GetAtt" : [ "ConsoleServer", "PrivateIp" ]
},      

我请等待处理程序我的用户数据被执行时等待,但是等待处理程序取决于我尝试配置的EC2。

i ask the wait handler to wait while my user data gets executed but the wait handeler is dependent on the EC2 that i am trying to configure.

这引起了特殊的依赖关系,但我无法理解如何使用其他方式获取实例的私有IP?

This is causing cicular dependency but i am unable to understand how to get private ip of the instance using some other way ?

下面是重要的部分:元数据

 "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"
            }
        }
    },

我添加了*********,其中正在从用户数据部分进行调用

i have added ********* where call is being made from user data section

推荐答案

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

To get EC2 IP address, you can get it by call this API using curl inside your script:

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

了解有关实例元数据和用户数据

这篇关于由于专用IP,aws堆栈中的循环依赖性[AWS CloudFormation]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆