简体   繁体   中英

Running a shell script in CloudFormation cfn-init

I am trying to run a script in the cfn-init command but it keeps timing out. What am I doing wrong when running the startup-script.sh?

"WebServerInstance" : {
      "Type" : "AWS::EC2::Instance",
      "DependsOn" : "AttachGateway",
      "Metadata" : {
        "Comment" : "Install a simple application",
        "AWS::CloudFormation::Init" : {
          "config" : {
            "files": {
              "/home/ec2-user/startup_script.sh": {
                "content": {
                  "Fn::Join": [
                    "",
                    [
                      "#!/bin/bash\n",
                      "aws s3 cp s3://server-assets/startserver.jar . --region=ap-northeast-1\n",
                      "aws s3 cp s3://server-assets/site-home-sprint2.jar . --region=ap-northeast-1\n",
                      "java -jar startserver.jar\n",
                      "java -jar site-home-sprint2.jar --spring.datasource.password=`< password.txt` --spring.datasource.username=`< username.txt` --spring.datasource.url=`<db_url.txt`\n"
                    ]
                  ]
                },
                "mode": "000755"
              }
            },
            "commands": {
              "start_server": {
                "command": "./startup_script.sh",
                "cwd": "~",
              }
            }
          }
        }
      },

The file part works fine and it creates the file but it times out at running the command. What is the correct way of executing a shell script?

You can tail the logs in /var/log/cfn-init.log and detect the issues while running the script.

The commands in Cloudformation Init are ran as sudo user by default. Maybe there can be an issue were your script is residing in /home/ec2-user/ and you are trying to run the script from '~' (ie /root) .

Please give the absolute path (/home/ec2-user) in cwd . It will solve your concern.

However, the exact issue can be fetched from the logs only.

Usually the init scripts are executed by root unless specified otherwise. Can you try giving the full path while running your startup script. You can give cloudkast a try. It is an online cloudformation template generator. Makes easier creating objects such as aws::cloudformation::init.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM