AWS Amazon EC2 Volume Attachment

This page shows how to write Terraform and CloudFormation for Amazon EC2 Volume Attachment and write them securely.

aws_volume_attachment (Terraform)

The Volume Attachment in Amazon EC2 can be configured in Terraform with the resource name aws_volume_attachment. The following sections describe 1 example of how to use the resource and its parameters.

Example Usage from GitHub

main.tf#L11
resource "aws_volume_attachment" "not_ok_attachment1" {
  device_name = "/dev/sdh"
  volume_id   = aws_ebs_volume.not_ok_ebs1.id
  instance_id = aws_instance.web.id
}

Parameters

Explanation in Terraform Registry

Provides an AWS EBS Volume Attachment as a top level resource, to attach and detach volumes from AWS Instances.

NOTE on EBS block devices: If you use ebs_block_device on an aws_instance, Terraform will assume management over the full set of non-root EBS block devices for the instance, and treats additional block devices as drift. For this reason, ebs_block_device cannot be mixed with external aws_ebs_volume + aws_ebs_volume_attachment resources for a given instance.

AWS::EC2::VolumeAttachment (CloudFormation)

The VolumeAttachment in EC2 can be configured in CloudFormation with the resource name AWS::EC2::VolumeAttachment. The following sections describe 10 examples of how to use the resource and its parameters.

Example Usage from GitHub

otcs-otac.yml#L70
    Type: "AWS::EC2::VolumeAttachment"
    Properties:
      InstanceId:
        !Ref OTCSOTAC
      VolumeId:
        !Ref app
otcs-adm.yml#L66
    Type: "AWS::EC2::VolumeAttachment"
    Properties:
      InstanceId:
        !Ref OTCSADM
      VolumeId:
        !Ref app
hana_Nodes.yml#L62
    Type: 'AWS::EC2::VolumeAttachment'
    DependsOn: [ 'HanaSharedVolume1', 'HanaNode1' ]
    Properties:
      InstanceID: !Ref HanaNode1
      VolumeID: !Ref HanaSharedVolume1
      Device: !Join [ "", [ '/dev/', !FindInMap [ DeviceName, Node, Shared ] ] ]
otcs-adm.yml#L27
    Type: "AWS::EC2::VolumeAttachment"
    Properties:
      InstanceId:
        !Ref OTCSADM
      VolumeId:
        !Ref app
openshift_aws_base_01.yml#L148
    Type: AWS::EC2::VolumeAttachment
    Properties:
      Device: '/dev/xvdb'
      VolumeId: !Ref volume1
      InstanceId: !Ref openshiftmaster

restoresnapshot-product.template.json#L578
      "Type": "AWS::EC2::VolumeAttachment",
      "Properties": {
        "InstanceId": {
          "Ref": "EC2Instance"
        },
        "VolumeId": {
MongoStackVPC.json#L375
            "Type" : "AWS::EC2::VolumeAttachment",
            "Properties" : {
                "InstanceId" : { "Ref" : "MongoInstanceMemberArbiter" },
                "VolumeId" : { "Ref" : "MongoVolume1MemberArbiter" },
                "Device" : "/dev/sdh1"
            }
ec2WithSecGroups.json#L220
      "Type": "AWS::EC2::VolumeAttachment",
      "Properties": {
        "InstanceId": {
          "Ref": "OpenShiftMaster1"
        },
        "VolumeId": {
computeWithoutBastion.json#L177
      "Type": "AWS::EC2::VolumeAttachment",
      "Properties": {
        "InstanceId": {
          "Ref": "OpenShiftMaster1"
        },
        "VolumeId": {
mongodb-8ebs.json#L152
            "Type" : "AWS::EC2::VolumeAttachment",
            "Properties" : {
                "InstanceId" : { "Ref" : "MongoDBNode8EBS" },
                "VolumeId"  : { "Ref" : "EBSVolume1" },
                "Device" : "/dev/sdf"
                }

Parameters

Explanation in CloudFormation Registry

Attaches an Amazon EBS volume to a running instance and exposes it to the instance with the specified device name.

Before this resource can be deleted (and therefore the volume detached), you must first unmount the volume in the instance. Failure to do so results in the volume being stuck in the busy state while it is trying to detach, which could possibly damage the file system or the data it contains.

If an Amazon EBS volume is the root device of an instance, it cannot be detached while the instance is in the "running" state. To detach the root volume, stop the instance first.

If the root volume is detached from an instance with an AWS Marketplace product code, then the product codes from that volume are no longer associated with the instance.

Frequently asked questions

What is AWS Amazon EC2 Volume Attachment?

AWS Amazon EC2 Volume Attachment is a resource for Amazon EC2 of Amazon Web Service. Settings can be wrote in Terraform and CloudFormation.

Where can I find the example code for the AWS Amazon EC2 Volume Attachment?

For Terraform, the SnidermanIndustries/checkov-fork source code example is useful. See the Terraform Example section for further details.

For CloudFormation, the okram999/ec2-cfn, okram999/ec2-cfn and somanianshul/cloudfront source code examples are useful. See the CloudFormation Example section for further details.