【问题标题】:At least one Resources member must be defined ...error in cloud formation ec2必须定义至少一个资源成员...云形成 ec2 中的错误
【发布时间】:2019-03-11 22:32:52
【问题描述】:
I tried other templates from the net but still getting the same error. Error

消息:模板包含错误。:模板格式错误:必须定义至少一个资源成员。

{
"AWSTemplateFormatVersion" : "2010-09-09",

"Description" : "TTND AWS CloudFormation template to launch first instance",

"Parameters" : {

"KeyName" : {
"Description" : "EC2 Key Pair for SSH Access",
"Default" : "sample",
"MinLength": "1",
"MaxLength": "64",
"AllowedPattern" : "[-_ a-zA-Z0-9]*",
"ConstraintDescription" : "can contain only alphanumeric characters, spaces, dashes and underscores."
},
"InstanceType" : {
"Description" : "Instance1 EC2 instance type",
"Type" : "String",
"Default" : "t2.micro",
"AllowedValues" : [ "t2.micro","m1.small","m1.medium","m1.large"],
"ConstraintDescription" : "must be a valid EC2 instance type."
}
},
"Mappings" : {
    "AWSInstanceMapping" : {
      "t2.micro"    : { "Arch" : "64" },
      "t2.small"    : { "Arch" : "64" },
      "t2.medium"   : { "Arch" : "64" },
      "t2.large"    : { "Arch" : "64" },
      "m3.medium"   : { "Arch" : "64" },
      "m4.large"   : { "Arch" : "64" },
      "m4.xlarge"  : { "Arch" : "64" },
      "m4.2xlarge"  : { "Arch" : "64" }
    }
    },

    "InstanceAMI" : {
      "us-east-1"      : { "64" : "ami-09ca8e1e" }
    },

我尝试了其他网络模板,但我得到了同样的错误

"Resources" : {

    "VPC" : {
      "Type" : "AWS::EC2::VPC",
      "Properties" : {
        "CidrBlock" : "10.0.0.0/16",
        "Tags" : [
          {"Key" : "Application", "Value" : { "Ref" : "AWS::StackId"} },
          { "Key": "Name", "Value": "Project_VPC"},
          {"Key" : "Network", "Value" : "Public" }
        ]
      }
    },

    "PublicSubnet" : {
      "Type" : "AWS::EC2::Subnet",
      "Properties" : {
        "VpcId" : { "Ref" : "VPC" },
        "CidrBlock" : "10.0.0.0/24",
        "Tags" : [
          {"Key" : "Application", "Value" : { "Ref" : "AWS::StackId"} },
          {"Key" : "Network", "Value" : "Public" },
          { "Key": "Name", "Value": "Project_Public_Subnet"}
        ]
      }
    },

    "InternetGateway" : {
      "Type" : "AWS::EC2::InternetGateway",
      "Properties" : {
        "Tags" : [
          {"Key" : "Application", "Value" : { "Ref" : "AWS::StackId"} },
          {"Key" : "Network", "Value" : "Public" },
          { "Key": "Name", "Value": "Project_Internetgateway"}
        ]
      }
    },

    "AttachGateway" : {
       "Type" : "AWS::EC2::VPCGatewayAttachment",
       "Properties" : {
         "VpcId" : { "Ref" : "VPC" },
         "InternetGatewayId" : { "Ref" : "InternetGateway" }
       }
    },
"PublicRouteTable" : {
      "Type" : "AWS::EC2::RouteTable",
      "Properties" : {
        "VpcId" : {"Ref" : "VPC"},
        "Tags" : [
          {"Key" : "Application", "Value" : { "Ref" : "AWS::StackId"} },
          {"Key" : "Network", "Value" : "Public" },
          { "Key": "Name", "Value": "cloudwords_public_routetable"}
        ]
      }
    },                                                           

我为发布它而删除的一些代码给出了很大的代码错误,所以

"Outputs" : {
 "InstanceId" : {
 "Description" : "InstanceId of the newly created instance",
 "Value" : { "Ref" : "Instance" }
 },
    }
}

如果有人有使用 CloudFormation 模板启动 AWS EC2 实例的简单模板,请发布

【问题讨论】:

  • 您似乎从某个示例中复制了此内容,但仅复制了示例的顶部。
  • 是的,我是从这个网站复制的 tothenew.com/blog/…... 但由于代码太大,我无法在此处发布完整代码
  • 他的模板非常好。涵盖了 EC2 实例和 VPC 的许多详细信息。你有什么问题?请注意,Internet 上有数千个 CloudFormation 示例。 Google 搜索是您的朋友。
  • 是的,这个模板很好,涵盖了很多东西……但是当我选择设计模板时,这个错误出现了“必须定义至少一个资源成员。”无论如何,我找到了另一个模板,当我选择设计模板选项时它也给出了同样的错误。然后我通过浏览附加文件及其工作。不知道为什么设计模板不起作用

标签: amazon-web-services amazon-ec2 cloud amazon-cloudformation


【解决方案1】:

您的示例似乎没有定义任何 AWS::EC2::Instance 资源,这就是告诉 CloudFormation 预置 EC2 实例的原因。

这是一个非常简约的 CloudFormation 模板,它将创建一个 t2.micro 实例。查看AWS::EC2::Instance resource definition,详细了解可以添加哪些属性来自定义它。

{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Resources": {
    "ExampleEc2Instance": {
      "Type": "AWS::EC2::Instance",
      "Properties": {
        "InstanceType": "t2.micro",
        "ImageId" : "ami-a0cfeed8"
      }
    }
  }
}

为特定操作系统、配置和区域查找有效的 AMI 可能有点棘手。 This walkthrough 讨论了一种使用 AWS Lambda 自动查找 AMI 的策略。

【讨论】:

  • 仍然收到相同的错误“必须定义至少一个资源成员。” "ImageId" : "ami-a0cfeed8" 是我需要创建的第一个 ami
  • 已在俄勒冈地区创建了 AMI“ami-a0cfeed8”。您可以在 EC2 -> Launch Instance 中看到一些默认值(AMI 名称右侧的 ami-[hex] 是 AMI id)。如果您将该 AMI 设置为您所在地区的有效 AMI,将模板保存到文件,然后转到 CloudFormation -> 创建堆栈 ->“将模板上传到 Amazon S3”,然后继续命名/创建堆栈,它应该可以工作.
  • 能够按照您的步骤创建 Ec2,但是当我在 Json 中选择设计模板时,出现此错误...“必须定义至少一个资源成员”
【解决方案2】:

您需要一个简单的模板来启动 EC2 实例,所以就这样吧。 请记住,这只是基本的,可以通过更多选项进行扩展。 如果您在此处需要任何具体帮助,请告诉我。 祝你好运。

{
  "AWSTemplateFormatVersion" : "2010-09-09",
  "Description" : "AWS CloudFormation Sample Template",
  "Parameters" : {
    "KeyName": {
      "Description" : "Name of an existing EC2 KeyPair to enable SSH access to the instance",
      "Type": "AWS::EC2::KeyPair::KeyName",
      "ConstraintDescription" : "must be the name of an existing EC2 KeyPair."
    },
    "InstanceType": {
      "Description": "EC2 instance type",
      "Type": "String",
      "Default": "t2.micro"
    },
    "ImageID": {
      "Description": "EC2 instance type",
      "Type": "String",
      "Default": "ami-xxxxxxxxxxxxxxx"
    },
    "SecurityGroupId" : {
      "Type" : "String",
      "Description" : "The SecurityGroupId of an existing EC2 SecurityGroup in your Virtual Private Cloud (VPC)",
      "Default": "sg-xxxxxxxx"
    },
    "SubnetID": {
      "Description": "Subnets where logging EC2 instances can be deployed, must be in same VPC as selected above",
      "Type": "String",
      "ConstraintDescription": "must be valid subnet.",
      "Default": "subnet-xxxxxxxxx"
    }
  },
  "Resources" : {
    "EC2Instance" : {
      "Type" : "AWS::EC2::Instance",
     "Properties" : {
        "InstanceType" : { "Ref" : "InstanceType" },
        "SecurityGroupIds" : [{ "Ref" : "SecurityGroupId"}],
        "KeyName" : { "Ref" : "KeyName" },
        "ImageId" : { "Ref" : "ImageID" },
        "InstanceInitiatedShutdownBehavior" : "stop",
        "SubnetId" : { "Ref": "SubnetID" }
      }
    }
  },
  "Outputs" : {
    "InstanceId" : {
      "Description" : "InstanceId of the newly created EC2 instance",
      "Value" : { "Ref" : "EC2Instance" }
    }
  }
}

【讨论】:

    【解决方案3】:

    您的模板有几个问题。 @Tom 有一些很好的建议可以遵循。

    AMI 的最低要求是这样。在对您的模板进行一些更正并添加下面的 sn-p 之后,我可以运行您的模板。更多示例请看这里:https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/resources-section-structure.html

    "Resources" : {
      "MyEC2Instance" : {
        "Type" : "AWS::EC2::Instance",
        "Properties" : {
          "ImageId" : "ami-0ff8a91507f77f867"
        }
      }
    }
    

    您的带有 EC2 实例 sn-p 的模板:

    {
        "AWSTemplateFormatVersion" : "2010-09-09",
    
        "Description" : "TTND AWS CloudFormation template to launch first instance",
    
        "Parameters" : {
    
              "KeyName" : {
                  "Description" : "EC2 Key Pair for SSH Access",
                  "Type": "String",
                  "Default" : "sample",
                  "MinLength": "1",
                  "MaxLength": "64",
                  "AllowedPattern" : "[-_ a-zA-Z0-9]*",
                  "ConstraintDescription" : "can contain only alphanumeric characters, spaces, dashes and underscores."
              },
              "InstanceType" : {
                  "Description" : "Instance1 EC2 instance type",
                  "Type" : "String",
                  "Default" : "t2.micro",
                  "AllowedValues" : [ "t2.micro","m1.small","m1.medium","m1.large"],
                  "ConstraintDescription" : "must be a valid EC2 instance type."
              }
        },
        "Mappings" : {
            "AWSInstanceMapping" : {
              "t2.micro"    : { "Arch" : "64" },
              "t2.small"    : { "Arch" : "64" },
              "t2.medium"   : { "Arch" : "64" },
              "t2.large"    : { "Arch" : "64" },
              "m3.medium"   : { "Arch" : "64" },
              "m4.large"   : { "Arch" : "64" },
              "m4.xlarge"  : { "Arch" : "64" },
              "m4.2xlarge"  : { "Arch" : "64" }
            }
        },
    
        "Resources" : {
          "MyEC2Instance" : {
            "Type" : "AWS::EC2::Instance",
            "Properties" : {
              "ImageId" : "ami-0ff8a91507f77f867"
            }
          }  
        }
    }
    

    【讨论】:

    • 同样的错误。 “ImageId”:“ami-a0cfeed8”对此感到困惑。我创建的 ami 我需要使用那个 id 否则??
    • 用您选择的 AMI id 替换它。一个是您创建的,或者一个来自 Marketplace。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-14
    • 1970-01-01
    • 1970-01-01
    • 2020-08-05
    • 2017-05-30
    相关资源
    最近更新 更多