简体   繁体   English

在 CloudFormation cfn-init 中运行 shell 脚本

[英]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.我试图在 cfn-init 命令中运行一个脚本,但它一直超时。 What am I doing wrong when running the startup-script.sh?运行 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?执行shell脚本的正确方法是什么?

You can tail the logs in /var/log/cfn-init.log and detect the issues while running the script.您可以/var/log/cfn-init.log的日志并在运行脚本时检测问题。

The commands in Cloudformation Init are ran as sudo user by default. Cloudformation Init 中的sudo user默认以sudo user身份运行。 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) .如果您的脚本驻留在/home/ec2-user/并且您试图从'~' (ie /root)运行脚本,则可能存在问题。

Please give the absolute path (/home/ec2-user) in cwd .请在cwd给出绝对路径(/home/ec2-user) 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.通常 init 脚本由 root 执行,除非另有说明。 Can you try giving the full path while running your startup script.您可以尝试在运行启动脚本时提供完整路径。 You can give cloudkast a try.你可以试试cloudkast It is an online cloudformation template generator.它是一个在线 cloudformation 模板生成器。 Makes easier creating objects such as aws::cloudformation::init.更容易创建 aws::cloudformation::init 等对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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