ec2 实例缺少 Cloudformation DeletionPolicy 策略

Cloudformation DeletionPolicy policy missing for ec2 instance

我正在尝试创建一个堆栈(参见下面的代码)

但我收到以下错误:

There was an error creating this change set.
The following resources to import [masterinstance] must have DeletionPolicy attribute specified in the template.

我不确定如何解决这个问题。我尝试在 InstanceType 定义下添加 "DeletionPolicy": "Retain",但这是不正确的。

"InstanceType": {

  "Description": "EC2 instance type for the node instances",

  "Type": "String",
  "Default": "t3.micro",
  "DeletionPolicy": "Retain",

谁能解释一下我做错了什么以及我该如何更改模板?

非常感谢。

模板:

{

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

  "Description" : "Template to create a RHEL8 instances for testings",

  "Parameters": {

    "instanceName": {

      "Description": "The instance name.",

      "Type": "String"

    },

    "Subnet": {

      "Description": "The subnets where the instance is created.",

      "Type": "AWS::EC2::Subnet::Id"

    },

    "securitygroup": {

      "Description": "The subnets where workers can be created.",

      "Type": "List<AWS::EC2::SecurityGroup::Id>"

    },

    "InstanceType": {

      "Description": "EC2 instance type for the node instances",

      "Type": "String",
      "Default": "t3.micro",
      "AllowedValues": [

        "t3.micro", "t3.small", "t2.medium"

      ],

      "ConstraintDescription": "Must be a valid EC2 instance type"

    },

    "KeyName": {

      "Description": "The EC2 Key Pair to allow SSH access to the instances",

      "Type": "AWS::EC2::KeyPair::KeyName"

    },

    "volumeSize": {

      "Description": "Size of EBS volume in GB",

      "Type": "Number"

    },

    "ami" : {

      "Description": "ami of instance",

      "Type" : "AWS::EC2::Image::Id",

    }
  },

  "Resources" : {

    "masterinstance" : {

      "Type" : "AWS::EC2::Instance",

      "Properties" : {

        "BlockDeviceMappings" : [ {

          "DeviceName" :  "/dev/sda1",

          "Ebs" : {

            "DeleteOnTermination" : "False",

            "Encrypted" : "False",

            "VolumeSize" : {"Ref":  "volumeSize"},

            "VolumeType" : "gp2"

          }

        }],

        "ImageId" : {"Ref": "ami"},

        "InstanceType" : {"Ref" : "InstanceType"},

        "KeyName" : {"Ref": "KeyName"},

        "SecurityGroupIds" : {"Ref" :  "securitygroup"},

        "SubnetId" : {"Ref": "Subnet"},

        "Tags" : [ {

          "Key" : "Name",

          "Value" : {"Ref": "instanceName"}

        } ]

      }

    }

  }

}

DeletionPolicy应该放在你实际的实例资源中,而不是输入参数。例如:

"MyEC2Instance" : {
   "Type" : "AWS::EC2::Instance",
   "DeletionPolicy" : "Retain",  
   "Properties" : {
      "ImageId" : "ami-79fd7eee",
      "KeyName" : "testkey",
   ...