chiru
chiru

Reputation: 832

Use cloudformation parameter in userdata

I have zookeeper connect as ip1:port1,ip2:port2,ip3:port3 and I want to use this in my userdata How can i do it?

"ZooKeeperConnect": {
         "Type": "String",
         "Description": "list of IP or CNAME's for the associated zookeeper ensemble",
         "AllowedPattern": "([1-9][0-9]?|[1-2][0-9]{2})(\\.([1-9][0-9]?|[1-2][0-9]{2})){3}(:[1-9][0-9]{1,4})?(,([1-9][0-9]?|[1-2][0-9]{2})(\\.([1-9][0-9]?|[1-2][0-9]{2})){3}(:[1-9][0-9]{1,4})?)*"
       }


"UserData": {
         "Fn::Base64": {
           "Fn::Join": [
             "",
             [
               "#!/bin/bash \n",
               "echo Cache proxy vars... \n",

I want to use ZooKeeperConnect values in my userdata

Upvotes: 6

Views: 9543

Answers (2)

Jeremy Thompson
Jeremy Thompson

Reputation: 65702

Basically you can't directly reference Parameters or built in types such as AWS::StackName inside the UserData. Instead you generate the UserData script concatenating the Parameter and built-in types. See the line $HowDoIGetTheStackNameOrAParameter?

BEFORE

UserData:
        !Base64 |
          <powershell>
          #setup an install folder
          $path = "C:\temp\"
          If(!(test-path $path))
          {
            New-Item -ItemType Directory -Force -Path $path
          }
          cd $path

          # missing variables and assignment
      
          # This code downloads a PS script from S3 and executes it
          Copy-S3Object -BucketName $S3BucketName -key $bootstrap -LocalFile ($path + $bootstrap)
          & $script -S3Name $S3BucketName -StackName $HowDoIGetTheStackNameOrAParameter?
          </powershell>

AFTER

UserData : { "Fn::Base64" : { "Fn::Join" : ["", [
        "<powershell>\n",
          "$path = \"C:/temp/\"\n",
          "If(!(test-path $path)) {\n",
          "  New-Item -ItemType Directory -Force -Path $path\n",
          "}\n",
          "cd $path\n",

         # missing variables and assignment
      
         # This code downloads a PS script from S3 and executes it

          "Copy-S3Object -BucketName $S3BucketName -key $bootstrap -LocalFile ($path + $bootstrap)\n",
          "& $script -S3Name $S3BucketName -StackName ", { "Ref" : "AWS::StackName" }, "\n",
          "</powershell>\n", 
          "<persist>true</persist>\n",
          "<runAsLocalSystem>true</runAsLocalSystem>"

That's how to get inbuilt types and its practically the same thing to get Parameters:

 { "Ref" : "ParameterName" }

Upvotes: 2

jzonthemtn
jzonthemtn

Reputation: 3414

You can reference the ZooKeeperConnect parameter or any other parameter in your userdata section:

"UserData" : {
    "Fn::Base64" : {
        "Fn::Join" : [ ",", [
            { "Ref" : "MyValue" },
            { "Ref" : "MyName" },
            "Hello World" ] ]
    }
}

The above snippet was taken from http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/quickref-general.html.

Upvotes: 3

Related Questions