Pages

Wednesday 29 January 2014

AWS:Cloudformation:::Wait till instance is launched

Resources Used:

  • AWS::CloudFormation::WaitConditionHandle
    •   has no properties.
    • When you reference the WaitConditionHandle resource by using the Ref function, AWS CloudFormation returns a presigned URL. You pass this URL to applications or scripts that are running on your Amazon EC2 instances to send signals to that URL. An associated AWS::CloudFormation::WaitCondition resource checks the URL for the required number of success signals or for a failure signal. 



Steps Involved:
  1. Attach wait for resource . (instance)
  2. Get presigned url to fire query at ,when instance is launched.  You get this from AWS::CloudFormation::WaitConditionHandle. Use Ref to use it throughout cloudformation script.
  3. Fire the query at presigned url from UserData section.

With Amazon Linux
{
    "AWSTemplateFormatVersion" : "2010-09-09",
    "Mappings" : {
        "RegionMap" : {
            "us-east-1" : {
                "AMI" : "ami-76f0061f"
            },
            "us-west-1" : {
                "AMI" : "ami-655a0a20"
            },
            "eu-west-1" : {
                "AMI" : "ami-7fd4e10b"
            },
            "ap-northeast-1" : {
                "AMI" : "ami-8e08a38f"
            },
            "ap-southeast-1" : {
                "AMI" : "ami-72621c20"
            }
        }
    },
    "Resources" : {
        "Ec2Instance" : {
            "Type" : "AWS::EC2::Instance",
            "Properties" : {
                "UserData" : { "Fn::Base64" : {"Ref" : "myWaitHandle"}},
                "ImageId" : { "Fn::FindInMap" : [ "RegionMap", { "Ref" : "AWS::Region" }, "AMI" ]}
            }
        },
        "myWaitHandle" : {
            "Type" : "AWS::CloudFormation::WaitConditionHandle",
            "Properties" : {
            }
        },
        "myWaitCondition" : {
            "Type" : "AWS::CloudFormation::WaitCondition",
            "DependsOn" : "Ec2Instance",
            "Properties" : {
                "Handle" : { "Ref" : "myWaitHandle" },
                "Timeout" : "4500"
            }
        }
    },
    "Outputs" : {
        "ApplicationData" : {
            "Value" : { "Fn::GetAtt" : [ "myWaitCondition", "Data" ]},
            "Description" : "The data passed back as part of signalling the WaitCondition."
        }
    }
}



With Amazon Linux
"UserData" : { "Fn::Base64" : {"Ref" : "myWaitHandle"}},

Mere mention of UserData with Ref :"myWaitHandle.." will trigger the signal to end waitcondition.

For other linux like ubuntu you need to fire signal(cfn-signal) through their cloudformation init script. Download and install them.
  1. cfn-init: help you run "AWS::CloudFormation::Init" (files, commands, services)It Help in setting up files and environment prior to running user code.
  2. You can run your custom bash script or anything using UserData
  3. cfn-singal  : singal aws 

In Ubuntu,  userdata section of ec2::instance will change to this:

       "UserData"       : { "Fn::Base64" : { "Fn::Join" : ["", [

            "#!/bin/bash\n",
  "apt-get update\n",
  "apt-get -y install python-setuptools\n",
  "wget -P /root https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-latest.tar.gz","\n",
   "mkdir -p /root/aws-cfn-bootstrap-latest","\n",   "tar xvfz/root/aws-cfn-bootstrap-latest.tar.gz --strip-components=1 -C /root/aws-cfn-bootstrap-latest","\n",
    "easy_install /root/aws-cfn-bootstrap-latest/","\n",
        ---- RUN YOUR CUSTOM SCRIPT ---
     "/usr/local/bin/cfn-init -s ", { "Ref" : "AWS::StackId" }, " -r AuthLaunchConfigSpot ",
                                                "    --access-key ", {"Ref" : "HostKeys" },
                                                "    --secret-key ", {
"Fn::GetAtt" : [ "HostKeysHostKeysHostKeysHostKeys", "SecretAccessKey" ]},
                                                "    --region ", { "Ref" :"AWS::Region" }, "\n", "chef-client -j /etc/chef/node.json\n",
          "/usr/local/bin/cfn-signal -e $? '", { "Ref" : "myWaitHandle" },"'\n"
        ]]}}
      }
    },



Explaination: 
   In userdata section we've downloaded aws cloudformation helper script and installed it with the help of python easy install.
To run cfn-init , we need access-key, secret-key and region value. How to get them?
To run cfn-signal, we've simply specify our waitHandle reference (same as above). It'll signal the aws that our instance is launched successfully.

Reference:

  1. http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-waitcondition.html
  2. http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-waitconditionhandle.html

2 comments:

  1. Good article on AWS, The content is good and very informative, Thanks For sharing this with us.

    Best Regards,
    CourseIng - AWS Online Training in Hyderabad

    ReplyDelete
  2. Nice article, users are attracted when they see your post thanks for posting keep updating. Get touch with AWS Online Course Bangalore

    ReplyDelete