Use cloudformation parameter in userdata
Asked Answered
M

2

6

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

Molly answered 29/7, 2016 at 19:3 Comment(0)
M
3

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.

Minter answered 30/7, 2016 at 0:22 Comment(0)
S
2

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" }
Semi answered 9/9, 2020 at 5:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.