为什么我的 Cloudformation 脚本没有启动 apache?

为什么我的 Cloudformation 脚本没有启动 apache?

我正在尝试创建一个实例来运行 apache,并且一切似乎都正常,但是 Web 服务器尚未启动。

手动启动时一切正常。如果我重新启动实例,它不会自动启动 Web 服务器。

我的资源部分包含:

"Resources" : {
  "CfnUser" : {
    "Type" : "AWS::IAM::User",
    "Properties" : {
      "Path": "/",
      "Policies": [{
        "PolicyName": "root",
        "PolicyDocument": { "Statement":[{
          "Effect":"Allow",
          "Action":"cloudformation:DescribeStackResource",
          "Resource":"*"
        }]}
      }]
    }
  },

  "HostKeys" : {
    "Type" : "AWS::IAM::AccessKey",
    "Properties" : {
      "UserName" : {"Ref": "CfnUser"}
    }
  },

  "testInstance" : {
    "Type" : "AWS::EC2::Instance",
    "Metadata" : {
        "Comment1" : "Just testing",
        "AWS::CloudFormation::Init" : {
          "config" : {
              "packages" : {
                  "yum" : {
                      "mysql"        : [],
                      "mysql-server" : [],
                      "mysql-libs"   : [],
                      "httpd24"      : [],
                      "php54"        : [],
                      "php54-common" : [],
                      "php54-mysql"  : [],
                      "php54-pdo"    : [],
                      "php54-xml"    : [],
                      "php54-mcrypt" : [],
                      "php54-gd"     : []
                  }
              }
          },

          "services" : {
              "sysvinit" : {
                  "httpd"    : { "enabled" : "true", "ensureRunning" : "true" },
                  "mysqld"   : { "enabled" : "true", "ensureRunning" : "true" }
              }
          }

      }
  },

  "Properties" : {
    "ImageId" : { "Fn::FindInMap" : [ "RegionMap", { "Ref" : "AWS::Region" }, "64"] },
    "InstanceType"   : { "Ref" : "InstanceType" },
    "SecurityGroups" : [ {"Ref" : "StackWebAccess"} ],
    "KeyName"        : { "Ref" : "KeyName" },

    "UserData"       : { "Fn::Base64" : { "Fn::Join" : ["", [
      "#!/bin/bash -v\n",
      "yum update -y aws-cfn-bootstrap\n",
      "# Install LAMP packages\n",
      "/opt/aws/bin/cfn-init -s ", { "Ref" : "AWS::StackName" }, " -r testInstance ",
      "    --access-key ",  { "Ref" : "HostKeys" },
      "    --secret-key ", {"Fn::GetAtt": ["HostKeys", "SecretAccessKey"]},
      "    --region ", { "Ref" : "AWS::Region" }, " || error_exit 'Failed to run cfn-init'\n"
        ] ] } }        
  }
},

"StackWebAccess" : {
  "Type" : "AWS::EC2::SecurityGroup",
  "Properties" : {
    "GroupDescription" : "Enable SSH access and HTTP access on the inbound port",
    "SecurityGroupIngress" : [ 
      {
        "IpProtocol" : "tcp",
        "FromPort" : "22",
        "ToPort" : "22",
        "CidrIp" : "0.0.0.0/0"
      },
      {
        "IpProtocol" : "tcp",
        "FromPort" : "80",
        "ToPort" : "80",
        "CidrIp" : "0.0.0.0/0"
      }
    ]
  }
  }
},

我的 cfn-init.log:

2013-01-23 13:55:58,064 [INFO] Running configSets: default
2013-01-23 13:55:58,065 [INFO] Running configSet default
2013-01-23 13:55:58,065 [INFO] Running config config
2013-01-23 13:57:23,097 [INFO] Yum installed [u'php54-common', u'php54-xml', u'php54', u'httpd24', u'php54-gd', u'mysql-server', u'php54-mcrypt', u'mysql-libs', u'php54-mysql', u'php54-pdo', u'mysql']
2013-01-23 13:57:23,102 [INFO] ConfigSets completed

是否缺少了什么,或者应该启动服务?

答案1

以防万一有人遇到这个问题,问题是一个嵌套错误 - services {} 块需要成为 config {} 块的一部分,而不是在同一级别。

编辑:工作资源块:

  "Resources" : {

    "CfnUser" : {
      "Type" : "AWS::IAM::User",
      "Properties" : {
        "Path": "/",
        "Policies": [{
          "PolicyName": "root",
          "PolicyDocument": { "Statement":[{
            "Effect":"Allow",
            "Action":"cloudformation:DescribeStackResource",
            "Resource":"*"
          }]}
        }]
      }
    },

    "HostKeys" : {
      "Type" : "AWS::IAM::AccessKey",
      "Properties" : {
        "UserName" : {"Ref": "CfnUser"}
      }
    },

    "testInstance" : {
      "Type" : "AWS::EC2::Instance",
      "Metadata" : {
        "Comment1" : "Install the default packages",
        "AWS::CloudFormation::Init" : {
          "config" : {
            "packages" : {
              "yum" : {
                "mysql"        : [],
                "mysql-server" : [],
                "mysql-libs"   : [],
                "httpd24"      : [],
                "php54"        : [],
                "php54-common" : [],
                "php54-mysql"  : [],
                "php54-pdo"    : [],
                "php54-xml"    : [],
                "php54-mcrypt" : [],
                "php54-gd"     : []
              }
            },

            "services" : {
              "sysvinit" : {
                "httpd"    : { "enabled" : "true", "ensureRunning" : "true" },
                "mysqld"   : { "enabled" : "true", "ensureRunning" : "true" }
              }
            }
          }
        }
      },
      "Properties" : {
        "ImageId" : { "Fn::FindInMap" : [ "RegionMap", { "Ref" : "AWS::Region" }, "64"] },
        "InstanceType"   : { "Ref" : "InstanceType" },
        "SecurityGroups" : [ {"Ref" : "StackWebAccess"} ],
        "KeyName"        : { "Ref" : "KeyName" },


        "UserData"       : { "Fn::Base64" : { "Fn::Join" : ["", [
          "#!/bin/bash -v\n",
          "yum update -y aws-cfn-bootstrap\n",
          "# Install LAMP packages\n",
          "/opt/aws/bin/cfn-init -s ", { "Ref" : "AWS::StackName" }, " -r testInstance ",
          "    --access-key ",  { "Ref" : "HostKeys" },
          "    --secret-key ", {"Fn::GetAtt": ["HostKeys", "SecretAccessKey"]},
          "    --region ", { "Ref" : "AWS::Region" }, " || error_exit 'Failed to run cfn-init'\n"
            ] ] } }        
      }
    },

    "StackWebAccess" : {
      "Type" : "AWS::EC2::SecurityGroup",
      "Properties" : {
        "GroupDescription" : "Enable SSH access and HTTP access on the inbound port",
        "SecurityGroupIngress" : [ 
          {
            "IpProtocol" : "tcp",
            "FromPort" : "22",
            "ToPort" : "22",
            "CidrIp" : "0.0.0.0/0"
          },
          {
            "IpProtocol" : "tcp",
            "FromPort" : "80",
            "ToPort" : "80",
            "CidrIp" : "0.0.0.0/0"
          }
        ]
      }
    }


  },

相关内容