You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

设置DeleteOnTermination=true创建EC2实例失败问题排查

AWS CloudFormation: Fixing "/dev/sdf is already in use" Error When Setting DeleteOnTermination to True

The Problem

You’re trying to set DeleteOnTermination to true for all volumes in your CloudFormation template by adding the BlockDeviceMappings section, but you’re hitting this error:

Invalid value '/dev/sdf' for unixDevice. Attachment point /dev/sdf is already in use

Let’s break down why this is happening and how to fix it.

Why This Error Occurs

Your template has two conflicting configurations for the /dev/sdf device:

  1. The Volumes section mounts a pre-created EBS volume (Volume67430dc919ab486a887388695720e574) to /dev/sdf.
  2. The BlockDeviceMappings section tries to create a new 100GB EBS volume and attach it to the same /dev/sdf path.

EC2 instances can’t attach two volumes (or a pre-created + new volume) to the same device path—this is what’s causing the "already in use" conflict.

How to Fix It

You have two valid solutions depending on whether you need to keep the pre-created volume or not:

Solution 1: Keep the Pre-Created Volume & Set DeleteOnTermination

If you want to retain the standalone AWS::EC2::Volume resource, update your BlockDeviceMappings to reference this existing volume instead of creating a new one. You can still set DeleteOnTermination here:

  • Remove the /dev/sdf entry in BlockDeviceMappings that uses the Ebs property (which creates a new volume)
  • Add a new /dev/sdf entry that links to your pre-created volume via VolumeId, and include the DeleteOnTermination setting
  • Remove the Volumes section entirely—BlockDeviceMappings handles both volume attachment and deletion policy when using VolumeId

Here’s the modified AWS::EC2::Instance Properties section:

"Properties": {
  "InstanceType": "t2.micro",
  "BlockDeviceMappings": [
    {
      "DeviceName": "/dev/sda1",
      "Ebs": {
        "VolumeSize": "50",
        "DeleteOnTermination": "true"
      }
    },
    {
      "DeviceName": "/dev/sdf",
      "VolumeId": { "Ref": "Volume67430dc919ab486a887388695720e574" },
      "Ebs": {
        "DeleteOnTermination": "true"
      }
    }
  ],
  "ImageId": "ami-6871a115",
  "KeyName": "${aws_key_pair.KP6a0efc01a84c4103b648b57b425b0ca2.key_name}",
  "NetworkInterfaces": [
    {
      "AssociatePublicIpAddress": "true",
      "DeviceIndex": "0",
      "SubnetId": "subnet-07f722e24335cea6f",
      "GroupSet": [ "sg-0ed40ed2e6af3db09" ]
    }
  ],
  "Tags": [
    { "Key": "Name", "Value": "jagaranteamvoltest" },
    { "Key": "vid", "Value": "c42a7c2e-ed09-4a79-acd8-51bdc9772a13" }
  ]
}

Solution 2: Remove the Pre-Created Volume, Use Only BlockDeviceMappings

If you don’t need to manage the volume independently of the instance, simplify your template:

  • Delete the Volumes section from the instance properties
  • Delete the standalone Volume67430dc919ab486a887388695720e574 resource (and any related dependencies if applicable)
  • Keep your original BlockDeviceMappings for /dev/sdf—it will create the volume directly with your desired size and DeleteOnTermination setting

This is the cleaner option if the volume doesn’t need to exist separate from the instance.

Key Takeaway

Never mix Volumes and BlockDeviceMappings for the same device path. BlockDeviceMappings is the only section where you can set DeleteOnTermination, so use it to either:

  • Create and attach new volumes, or
  • Attach existing volumes while defining their deletion policy

内容的提问来源于stack exchange,提问作者nightfly

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 09:04:59