代码之家  ›  专栏  ›  技术社区  ›  Subham Tripathi

由于私有ip,aws堆栈中存在循环依赖关系[aws CloudFormation]

  •  1
  • Subham Tripathi  · 技术社区  · 10 年前

    我正在使用一个cloudformation模板来创建我的EC2实例。在userdata部分,我需要运行在元数据中创建的shell文件。对于该shell文件,我将实例的私有ip作为参数传递。 要获取私有ip,我使用的是:

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

    我要求等待处理程序在执行用户数据时等待,但等待处理程序依赖于我试图配置的EC2。

    这导致了ciculal依赖,但我无法理解如何使用其他方式获取实例的私有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": {
                        "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"
                }
            }
        },
    

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

    1 回复  |  直到 10 年前
        1
  •  4
  •   Edward Samuel Pasaribu    10 年前

    要获取EC2 IP地址,可以使用 curl 在脚本中:

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

    阅读更多关于 Instance Metadata and User Data .