Circular dependency in aws stack because of privat

2019-08-08 11:30发布

问题:

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

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.

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

Below are the part that matter : 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" ]
                                            },

and this is my userdata section followed by 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

回答1:

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

Read more about Instance Metadata and User Data.