json - 用于创建 EC2 的 AWS CloudFormation 模板 (JSON) - 意外错误

标签 json amazon-ec2 aws-cloudformation

开始测试 Cloud Formation 模板以使用 JSON 格式创建 EC2 实例,收到错误“每个参数对象必须包含类型成员”。我在网上找不到解决方案。

我已经搜索了这个错误,我找到的唯一解决方案是将“Type”:“String”添加到模板中,但它已经存在了。

{
    "AWSTemplateFormatVersion": "2010-09-09",
    "Description": "EC2 CloudFormation Template - Version 1.0",
    "Metadata": {},
    "Parameters": {
      "InstanceType": {
        "Description": "EC2 instance type",
        "Type": "String",
        "Default": "t2.small",
        "AllowedValues": [
          "t1.micro",
          "t2.nano",
          "t2.micro",
          "t2.small",
          "t2.medium",
          "t2.large",
        ],
        "ConstraintDescription": "must be a valid EC2 instance type."
    },
    "Mappings": {

    },
    "Conditions": {

    },
    "Resources": {
      "EOTSS_EC2": {
          "Type": "AWS::EC2::Instance",
          "Properties": {
              "DisableApiTermination": "false",
              "ImageId": "ami-06bee8e1000e44ca4",
              "InstanceType": { "Ref": "InstanceType" },
              "Monitoring": "true",
              "Tags": [
                  {
                      "Key": "Name",
                      "Value": "test"
                  }
              ]
            }
          }
      },
      "Outputs": {

      }
    }
}

当我将其作为新堆栈启动时出现的错误是“模板格式错误:每个参数对象都必须包含类型成员。”

最佳答案

问题是您的模板嵌套得不好:Outputs 应该位于 EOTSS_EC2 之外,而 Resources 应该位于 EOTSS_EC2 之外,换句话说,应该位于在 AWSTemplateFormatVersionDescriptionMetadataParametersMappings 的同一级别, 条件资源

{  
   "AWSTemplateFormatVersion":"2010-09-09",
   "Description":"EC2 CloudFormation Template - Version 1.0",
   "Metadata":{  

   },
   "Parameters":{  
      "InstanceType":{  
         "Description":"EC2 instance type",
         "Type":"String",
         "Default":"t2.small",
         "AllowedValues":[  
            "t1.micro",
            "t2.nano",
            "t2.micro",
            "t2.small",
            "t2.medium",
            "t2.large"
         ],
         "ConstraintDescription":"must be a valid EC2 instance type."
      }
   },
   "Mappings":{  

   },
   "Conditions":{  

   },
   "Resources":{  
      "EOTSS_EC2":{  
         "Type":"AWS::EC2::Instance",
         "Properties":{  
            "DisableApiTermination":"false",
            "ImageId":"ami-06bee8e1000e44ca4",
            "InstanceType":{  
               "Ref":"InstanceType"
            },
            "Monitoring":"true",
            "Tags":[  
               {  
                  "Key":"Name",
                  "Value":"test"
               }
            ]
         }
      }
   },
   "Outputs":{  

   }
}

关于json - 用于创建 EC2 的 AWS CloudFormation 模板 (JSON) - 意外错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56530728/

相关文章:

java - 我如何在 JAVA 中将一个 JSON 数组重构为其他格式?

javascript - 将 JSON 格式转换为数组以在数据中使用

amazon-web-services - 将 VSTS Build 与 AWS 结合使用的最简单方法是什么?

node.js - 如何在 Amazon Linux EC2 上安装和设置 Node.js 、 NPM

amazon-web-services - AWS CDK : How to rename existing stacks without losing the ability to update?

c# - 序列化列表计数而不是列表

android - Firebase 云消息传递 - 响应 JSON 中的 "success"和 "failure"

amazon-ec2 - 从 terraform 中的变量在 aws_autoscaling_policy 中设置 step_ adjustment

amazon-web-services - 如何在 CloudFormation 模板中使用 SSM StringList 中的值?

amazon-web-services - AWS 特定参数和 EC2 SecurityGroupIds 列表字符串错误