amazon-web-services - 在 CloudFormation cfn-init 中运行 shell 脚本

标签 amazon-web-services aws-cloudformation

我尝试在 cfn-init 命令中运行脚本,但它总是超时。 运行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": "~",
              }
            }
          }
        }
      },

文件部分工作正常,它创建了文件,但在运行命令时超时。 执行 shell 脚本的正确方法是什么?

最佳答案

您可以跟踪 /var/log/cfn-init.log 中的日志并在运行脚本时检测问题。

Cloudformation Init 中的命令默认以 sudo 用户 身份运行。如果您的脚本驻留在 /home/ec2-user/ 中,并且您尝试从 '~'(即/root) 运行脚本,则可能会出现问题.

请在cwd中给出绝对路径(/home/ec2-user)。它将解决您的担忧。

但是,只能从日志中获取确切的问题。

关于amazon-web-services - 在 CloudFormation cfn-init 中运行 shell 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56557288/

相关文章:

amazon-web-services - 我的存储桶是否应该针对我的用例公开?我应该如何避免不良做法?

c# - 使用 AWSSDK.S3 通过 C# 验证 Amazon S3 存储桶

amazon-web-services - aws cloudformation s3 向 SQS 发出的事件通知不起作用

amazon-web-services - 使用 Azure Devops 将 .NET 核心代码部署到 AWS lambda - 找不到项目路径

amazon-web-services - 不允许使用AWS Elasticsearch API _cluster

amazon-web-services - 将 StackName 添加到 Cloudformation 资源之前

amazon-web-services - 在 CloudFormation 模板中从辅助区域引用全局 dynamodb 表

amazon-web-services - 如果所需的容器数量大于 1,则 ECS 服务处于挂起状态

json - 适用于 EC2 的 CloudFormation 模板 (JSON),具有 VPC、子网和安全组选择

amazon-web-services - 使用 Cloudformation AWS SNS 到 SQS 发布失败