启动配置Userdata shell脚本不从Cloudformation运行

时间:2016-02-01 12:50:38

标签: shell amazon-web-services amazon-cloudformation aws-ec2

我尝试从AWS Cloudformation模板中的Launch Configuration的User-data运行shell脚本。我用里面的一些脚本创建了AMI。

以下是LaunchConfiguration的Cloudformation模板的示例:

 "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"
              ]
            ]
          }
        } 
      }
    }

当我创建堆栈时,它没有运行(正如我在log" /var/log/cloud-init.log"中看到的那样)。 我做错了什么?

谢谢!

1 个答案:

答案 0 :(得分:2)

将此行放在顶部,而不是向下: "#!/斌/庆典\ n&#34 ;.该行告诉它将整个脚本作为bash命令处理。它必须是第一线。 (所以,在你的加入中,如果你把它放在""而不是在加入之后,你应该很好。)

修改 有了你的联接,这应该有效:

"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"
              ]