aws-cloudformationaws-ebs

creating instances with volume DeleteOnTermination=true failing


I have the cloud formation template like below. Here I want to make the volumes all DeleteOnTermination as true. To achieve this I have included the BlockDeviceMappings section as only that allows setting value for DeleteOnTermination. But I am getting error: Invalid value '/dev/sdf' for unixDevice. Attachment point /dev/sdf is already in use

What am I doing wrong? Should I remove the /dev/sdf from volume section and keep it only in BlockDeviceMappings?

"Resources":{ 
   "INSc42a7c2eed094a79acd851bdc9772a13":{ 
      "Type":"AWS::EC2::Instance",
      "Properties":{ 
         "InstanceType":"t2.micro",
         "BlockDeviceMappings":[ 
            { 
               "DeviceName":"/dev/sda1",
               "Ebs":{ 
                  "VolumeSize":"50",
                  "DeleteOnTermination":"true"
               }
            },
            { 
               "DeviceName":"/dev/sdf",
               "Ebs":{ 
                  "VolumeSize":"100",
                  "DeleteOnTermination":"true"
               }
            }
         ],
         "ImageId":"ami-6871a115",
         "KeyName":"${aws_key_pair.KP6a0efc01a84c4103b648b57b425b0ca2.key_name}",
         "NetworkInterfaces":[ 
            { 
               "AssociatePublicIpAddress":"true",
               "DeviceIndex":"0",
               "SubnetId":"subnet-07f722e24335cea6f",
               "GroupSet":[ 
                  "sg-0ed40ed2e6af3db09"
               ]
            }
         ],
         "Volumes":[ 
            { 
               "Device":"/dev/sdf",
               "VolumeId":{ 
                  "Ref":"Volume67430dc919ab486a887388695720e574"
               }
            }
         ],
         "Tags":[ 
            { 
               "Key":"Name",
               "Value":"jagaranteamvoltest"
            },
            { 
               "Key":"vid",
               "Value":"c42a7c2e-ed09-4a79-acd8-51bdc9772a13"
            }
         ]
      }
   },
   "Alias60d46c3715ee4a72a4d3739b951bd4e0":{ 
      "Type":"AWS::KMS::Alias",
      "Properties":{ 
         "AliasName":"alias/KmsKeyVolTest",
         "TargetKeyId":{ 
            "Ref":"KmsKey60d46c3715ee4a72a4d3739b951bd4e0"
         }
      }
   },
   "KmsKey60d46c3715ee4a72a4d3739b951bd4e0":{ 
      "Type":"AWS::KMS::Key",
      "Properties":{ 
         "Description":"key description sample",
         "KeyPolicy":{ 
            "Version":"2012-10-17",
            "Statement":[ 
               { 
                  "Sid":"Allow access for Key Administrators",
                  "Effect":"Allow",
                  "Principal":{ 
                     "AWS":[ 
                        "${data.aws_caller_identity.current.arn}"
                     ]
                  },
                  "Action":[ 
                     "kms:Create*",
                     "kms:Describe*",
                     "kms:Enable*",
                     "kms:List*",
                     "kms:Put*",
                     "kms:Update*",
                     "kms:Revoke*",
                     "kms:Disable*",
                     "kms:Get*",
                     "kms:Delete*",
                     "kms:TagResource",
                     "kms:UntagResource",
                     "kms:ScheduleKeyDeletion",
                     "kms:CancelKeyDeletion",
                     "kms:Encrypt",
                     "kms:Decrypt",
                     "kms:ReEncrypt*",
                     "kms:GenerateDataKey*",
                     "kms:CreateGrant",
                     "kms:ListGrants",
                     "kms:DescribeKey",
                     "ec2:*"
                  ],
                  "Resource":"*"
               }
            ]
         }
      }
   },
   "Volume67430dc919ab486a887388695720e574":{ 
      "Type":"AWS::EC2::Volume",
      "DeletionPolicy":"Delete",
      "Properties":{ 
         "AutoEnableIO":"false",
         "AvailabilityZone":"us-east-1a",
         "Encrypted":"true",
         "KmsKeyId":{ 
            "Ref":"KmsKey60d46c3715ee4a72a4d3739b951bd4e0"
         },
         "Size":"15",
         "VolumeType":"standard",
         "Tags":[ 
            { 
               "Key":"vid",
               "Value":"67430dc9-19ab-486a-8873-88695720e574"
            }
         ]
      }
   }
},
"Outputs":{ 
   "caceedaacdbdca":{ 
      "Description":"Instance ID",
      "Value":{ 
         "Ref":"INSc42a7c2eed094a79acd851bdc9772a13"
      }
   },
   "dceeaadbbde":{ 
      "Description":"KMS Id",
      "Value":{ 
         "Ref":"KmsKey60d46c3715ee4a72a4d3739b951bd4e0"
      }
   }
}
}

Solution

  • Keep only one /dev/sdf in your template. Either "AWS::EC2::Volume" or from BlockDeviceMappings Or Change the name for one drive. You can't keep two volumes with the same name.