amazon-web-services - Cloudformation init 不执行命令,而是创建文件

标签 amazon-web-services aws-cloudformation

我正在尝试创建一个 canformation 模板,并在其中使用 cfn-init 。但我遇到了这个随机问题,cfn-init 模块将完美运行文件和服务配置,但命令模块中的任何命令都不会执行。以下是我的模板的片段。

"Metadata": {
    "AWS::CloudFormation::Init": {
      "config": {
        "commands": {
          "cloud": {
            "command" : "whoami >> /home/ubuntu/blah"
          },
          "test" : {
              "command" : "echo \"$MAGIC\" > test.txt",
              "env" : { "MAGIC" : "I come from the environment!" },
              "cwd" : "~",
              "test" : "test ! -e ~/test.txt",
              "ignoreErrors" : "false"
          },
          "test2": {
            "command" : "whoami >> wala",
            "cwd" : "/home/ubuntu"
          }
        },
        "files": {
          "/home/ubuntu/sambhav": {
            "content": {
              "Fn::Join": [
                "",
                [
                  "Ok Cool!!!",
                  "\n"
                ]
              ]
            }
          },
          "/etc/cfn/cfn-hup.conf": {
            "content": {
              "Fn::Join": [
                "",
                [
                  "[main]\n",
                  "stack=",
                  {
                    "Ref":"AWS::StackId"
                  },
                  "\n",
                  "region=",
                  {
                    "Ref":"AWS::Region"
                  },
                  "\n",
                  "interval=2",
                  "\n"
                ]
              ]
            },
            "mode": "000400",
            "owner": "root",
            "group": "root"
          },
          "/etc/cfn/hooks.d/cfn-auto-reloader.conf": {
            "content": {
              "Fn::Join": [
                "",
                [
                  "[cfn-auto-reloader-hook]\n",
                  "triggers=post.update\n",
                  "path=Resources.EC2Instance.Metadata.AWS::CloudFormation::Init\n",
                  "action=/usr/local/bin/cfn-init -s ",
                  {
                  "Ref":"AWS::StackId"
                  },
                  " -r EC2Instance ",
                  " --region ",
                  {
                    "Ref":"AWS::Region" 
                  },
                  "\n",
                  "runas=root\n"
                ]
              ]
            }
          }
        },
        "services" : {
          "sysvinit" : {
            "cfn-hup" : { "enabled" : "true", "ensureRunning" : "true",
                "files" : ["/etc/cfn/cfn-hup.conf", "/etc/cfn/hooks.d/cfn-auto-reloader.conf"]}
          }
        }
      }
    }
  },
  "Properties": {
    "InstanceType": {
      "Ref": "InstanceType"
    },
    "SecurityGroups": ["staging"],
    "KeyName": {
      "Ref": "KeyName"
    },
    "ImageId": "ami-6.....",
    "Tags" : [
        {"Key" : "Name", "Value" : "Staging"}
    ],
    "UserData" : { 
      "Fn::Base64" : { 
        "Fn::Join" : [
          "", 
          [
            "#!/bin/bash \n",
            "# Install cfn bootstraping tools \n",
            "apt-get update \n",
            "apt-get -y install python-setuptools \n",
            "mkdir aws-cfn-bootstrap-latest\n",
            "mkdir -p /home/ubuntu/work/projects\n",
            "sudo curl https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-latest.tar.gz | tar xz -C aws-cfn-bootstrap-latest --strip-components 1 \n",
            "sudo easy_install aws-cfn-bootstrap-latest \n",
            "cp /usr/local/bin/cfn-hup /etc/init.d/cfn-hup \n",
            "chmod +x /etc/init.d/cfn-hup \n",
            "update-rc.d cfn-hup defaults \n ",
            "service cfn-hup start \n",
            "/usr/local/bin/cfn-init --stack ", { "Ref":"AWS::StackName" }, 
            " --resource EC2Instance", " --region ", { "Ref": "AWS::Region" },
            "\n"
          ]
        ]
      }
    }
  }

最佳答案

您提供的模板片段似乎按预期工作,并且使用适当的内容创建了文件:

ubuntu@ip-172-31-9-151:~$ cat /home/ubuntu/*
root
Ok Cool!!!
root
cat: /home/ubuntu/work: Is a directory

检查 cfn-init 日志以确保命令正在执行并显示遇到的任何错误:

ubuntu@ip-172-31-9-151:~$ cat /var/log/cfn-init.log 
2015-10-18 05:37:49,087 [INFO] -----------------------Starting build-----------------------
2015-10-18 05:37:49,300 [INFO] Running configSets: default
2015-10-18 05:37:49,300 [INFO] Running configSet default
2015-10-18 05:37:49,301 [INFO] Running config config
2015-10-18 05:37:49,306 [INFO] Command cloud succeeded
2015-10-18 05:37:49,310 [INFO] Command test succeeded
2015-10-18 05:37:49,313 [INFO] Command test2 succeeded
2015-10-18 05:37:49,323 [INFO] enabled service cfn-hup
2015-10-18 05:37:49,420 [INFO] Restarted cfn-hup successfully
2015-10-18 05:37:49,421 [INFO] ConfigSets completed
2015-10-18 05:37:49,427 [INFO] -----------------------Build complete-----------------------

关于amazon-web-services - Cloudformation init 不执行命令,而是创建文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31788564/

相关文章:

amazon-web-services - 使用 boto3 在 SQS 中创建一个 fifo 队列

amazon-web-services - AWS 云形成错误 : ElasticMapReduce Cluster failed to stabilize

amazon-web-services - Cloudformation:如何使用 AWS::Route53::RecordSet 设置多个 dns 条目

amazon-web-services - AWS API Gateway 和 Lambda 的公共(public) IP 地址(无 VPC)

amazon-web-services - Localstack throws 请求中包含的安全 token 无效

amazon-web-services - 无法查看云信息中下载的文件

amazon-web-services - key 对 'AdvancedCFN' 不存在(服务 : AmazonEC2; Status Code: 400; Error Code:

aws-cloudformation - YAML 错误 : could not determine a constructor for the tag

amazon-web-services - cfnUpdate WaiterUnrecoverableException

c++ - 为iOS生成AWS-SDK-CPP时在cmake上损坏的CXX编译器错误