必须至少定义一个资源成员...云形成ec2中的错误

时间:2018-10-06 18:35:27

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

I tried other templates from the net but still getting the same error. Error

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

{
"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" }
    },

我尝试了net的其他模板,但遇到了相同的错误

"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实例的简单模板,请发布

3 个答案:

答案 0 :(得分: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查找的策略。

答案 1 :(得分:0)

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

您所需的AMI最低要求是这个。对模板进行一些更正并在下面添加代码段后,我可以运行您的模板。有关更多示例,请点击此处:https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/resources-section-structure.html

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

您的带有EC2实例代码段的模板:

{
    "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"
        }
      }  
    }
}

答案 2 :(得分:0)

您要求一个简单的模板来启动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" }
    }
  }
}