EC2 Cloudformation模板的Cloud-init中的Userdata脚本中的错误

时间:2019-05-04 07:40:17

标签: amazon-web-services amazon-ec2 amazon-cloudformation user-data cloud-init

我正在执行以下代码来创建堆栈。实例已创建,但收到此错误:

Loaded plugins: amazon-id, rhui-lb, search-disabled-repos
No Match for argument: aws-cfn-bootstrap
No package aws-cfn-bootstrap available.
No packages marked for update
/var/lib/cloud/instance/scripts/part-001: line 4: cd: /opt/aws/bin/cfn-init: No such file or directory

我的CloudFormation模板是:

{
  "Resources": {
    "MyInstance": {
      "Type": "AWS::EC2::Instance",
      "Properties": {
        "ImageId": "ami-011b3ccf1bd6db744",
        "InstanceType": "t2.micro",
        "KeyName": "EC2KeyPair",
        "UserData": {
          "Fn::Base64": {
            "Fn::Join": [
              "",
              [
                "#!/bin/bash",
                "\n",
                "exec > /tmp/user_data.log 2>&1 \n",
                "yum update -y\n",
                "yum install -y epel-release\n",
                "yum install -y https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-latest.amzn1.noarch.rpm \n",
                "ln -s /usr/local/lib/python2.7/site-packages/cfnbootstrap /usr/lib/python2.7/site-packages/cfnbootstrap\n",
                "/opt/aws/bin/cfn-init -v",
                " --stack ",
                {
                  "Ref": "AWS::StackName"
                },
                " --resource MyInstance",
                " --configsets scripts ",
                " --region ",
                {
                  "Ref": "AWS::Region"
                },
                "\n"
              ]
            ]
          }
        }
      },
      "Metadata": {
        "AWS::CloudFormation::Init": {
          "configSets": {
            "scripts": [
              "configure_cfn",
              "pythonInstallation"
            ]
          },
          "configure_cfn": {
            "files": {
              "/etc/cfn/hooks.d/cfn-auto-reloader.conf": {
                "content": {
                  "Fn::Join": [
                    "",
                    [
                      "[cfn-auto-reloader-hook]\n",
                      "triggers=post.update\n",
                      "path=Resources.EC2.Metadata.AWS::CloudFormation::Init\n",
                      "action=/opt/aws/bin/cfn-init -v",
                      " --stack ",
                      {
                        "Ref": "AWS::StackName"
                      },
                      " --resource EC2",
                      " --configsets wordpress",
                      " --region ",
                      {
                        "Ref": "AWS::Region"
                      },
                      "\n"
                    ]
                  ]
                },
                "mode": "000400",
                "owner": "root",
                "group": "root"
              },
              "/etc/cfn/cfn-hup.conf": {
                "content": {
                  "Fn::Join": [
                    "",
                    [
                      "[main]\n",
                      "stack=",
                      {
                        "Ref": "AWS::StackId"
                      },
                      "\n",
                      "region=",
                      {
                        "Ref": "AWS::Region"
                      },
                      "\n",
                      "verbose=true\n",
                      "interval=5\n"
                    ]
                  ]
                },
                "mode": "000400",
                "owner": "root",
                "group": "root"
              },
              "/var/www/html/index2.html": {
                "content": "Hi"
              }
            },
            "services": {
              "sysvinit": {
                "cfn-hup": {
                  "enabled": "true",
                  "ensureRunning": "true",
                  "files": [
                    "/etc/cfn/cfn-hup.conf",
                    "/etc/cfn/hooks.d/cfn-auto-reloader.conf"
                  ]
                }
              }
            }
          },
          "pythonInstallation": {
            "packages": {
              "yum": {
                "wget": [],
                "unzip": [],
                "gcc-c++": [],
                "zlib-devel": [],
                "libffi-devel": [],
                "httpd": []
              }
            },
            "sources": {
              "usr/src/": "https://www.python.org/ftp/python/3.7.2/Python-3.7.2.tgz"
            },
            "commands": {
              "python1": {
                "command": "tar xzf Python-3.7.2.tgz",
                "cwd": "/usr/src/"
              },
              "python2": {
                "command": {
                  "comm1": "./configure --enable-optimizations",
                  "comm2": "make altinstall",
                  "comm3": "rm /usr/src/Python-3.7.2.tgz"
                },
                "cwd": "/usr/src/Python-3.7.2"
              }
            },
            "files": {
              "/var/www/html/index.php": {
                "content": {
                  "Fn::Join": [
                    "",
                    [
                      "<html>\n",
                      "  <head>\n",
                      "    <title>AWS CloudFormation PHP Sample</title>\n",
                      "    <meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\">\n",
                      "  </head>\n",
                      "  <body>\n",
                      "    <h1>Welcome to the AWS CloudFormation PHP Sample</h1>\n",
                      "  </body>\n",
                      "</html>\n"
                    ]
                  ]
                }
              }
            }
          }
        }
      }
    }
  }
}

1 个答案:

答案 0 :(得分:0)

看来Redhat没有预加载cfn-init

您可以改用 Amazon Linux AMI ,也可以查看How to install aws-cfn-bootstrap/cfn-init package in Redhat using CloudFormation? - Stack Overflow,以获取有关如何在引导过程中加载cfn-init的提示。