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
}

Review your Terraform file for AWS best practices

Shisho Cloud, our free checker to make sure your Terraform configuration follows best practices, is available (beta).

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.

Tips: Best Practices for The Other AWS Amazon EC2 Resources

In addition to the aws_default_vpc, AWS Amazon EC2 has the other resources that should be configured for security reasons. Please check some examples of those resources and precautions.

risk-label

aws_default_vpc

Ensure to avoid using default VPC

It is better to define the own VPC and use it.

risk-label

aws_network_acl_rule

Ensure your network ACL rule blocks unwanted inbound traffic

It is better to block unwanted inbound traffic.

risk-label

aws_ebs_volume

Ensure to use a customer-managed key for EBS volume encryption

It is better to use a customer-managed key for EBS volume encryption. It can be gain more control over the encryption by using customer-managed keys (CMK).

risk-label

aws_instance

Ensure to avoid storing AWS access keys in user data

It is better to avoid storing AWS access keys in user data. `aws_iam_instance_profile` could be used instead.

risk-label

aws_security_group

Ensure your security group blocks unwanted inbound traffic

It is better to block unwanted inbound traffic.

Review your AWS Amazon EC2 settings

In addition to the above, there are other security points you should be aware of making sure that your .tf files are protected in Shisho Cloud.

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.