0
votes

I'm trying to run shell scripts from User-data of Launch Configuration in AWS Cloudformation template. I created AMI with some script inside.

Here is example from Cloudformation template of LaunchConfiguration:

 "ProcessLC": {
        "Type" : "AWS::AutoScaling::LaunchConfiguration",
        "Properties" : {
            "ImageId": {"Ref" : "GeneralAMI"},
            "InstanceType" : "t2.medium",
            "SecurityGroups" : [{"Ref": "SecurityGroup"}],
            "KeyName" : {"Ref": "KeyPair"},
            "UserData": {"Fn::Base64": {"Fn::Join": ["", [
              {"Fn::Join": ["", ["Env=",{"Ref": "Env"}," \n"]]},
              {"Fn::Join": ["", ["DBConn=", {"Ref": "Database"}," \n"]]},
              {"Fn::Join": ["", ["DBEngine=", {"Ref": "Metabase"}," \n"]]},
              "#!/bin/bash\n",
              "cd /project/\n",
              "./stop.sh\n",
              "./vpcAssignIP.sh\n"
              ]
            ]
          }
        } 
      }
    }

It doesn't run(as I see in log "/var/log/cloud-init.log") when I creating stack. What I'm doing wrong?

Thanks!

1

1 Answers

3
votes

Put this line at the top, instead of further down: "#!/bin/bash\n". That line tells it to process the entire script as bash commands. It has to be the first line. (So, in your join, if you put it in the "" instead of after the Join, you should be good.)

Edit: With your joins, this should work:

"UserData": {"Fn::Base64": {"Fn::Join": ["", [
              "#!/bin/bash\n",
              {"Fn::Join": ["", ["Env=",{"Ref": "Env"}," \n"]]},
              {"Fn::Join": ["", ["DBConn=", {"Ref": "Database"}," \n"]]},
              {"Fn::Join": ["", ["DBEngine=", {"Ref": "Metabase"}," \n"]]},
              "cd /project/\n",
              "./stop.sh\n",
              "./vpcAssignIP.sh\n"
              ]