0

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 Answer 1

3

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"
              ]
Sign up to request clarification or add additional context in comments.

2 Comments

So if "fn::join" will be after bash script, they will not work, no?
You have to make your join output "#!/bin/bash\n" as the first line. Currently, your joins output that line as the 4th line. See my edit for a code snippet.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.