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
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
-
device_name
required - string -
force_detach
optional - bool -
id
optional computed - string -
instance_id
required - string -
skip_destroy
optional - bool -
volume_id
required - string
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 anaws_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 externalaws_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.
aws_default_vpc
Ensure to avoid using default VPC
It is better to define the own VPC and use it.
aws_network_acl_rule
Ensure your network ACL rule blocks unwanted inbound traffic
It is better to block unwanted inbound traffic.
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).
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.
aws_security_group
Ensure your security group blocks unwanted inbound traffic
It is better to block unwanted inbound traffic.
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
Type: "AWS::EC2::VolumeAttachment"
Properties:
InstanceId:
!Ref OTCSOTAC
VolumeId:
!Ref app
Type: "AWS::EC2::VolumeAttachment"
Properties:
InstanceId:
!Ref OTCSADM
VolumeId:
!Ref app
Type: 'AWS::EC2::VolumeAttachment'
DependsOn: [ 'HanaSharedVolume1', 'HanaNode1' ]
Properties:
InstanceID: !Ref HanaNode1
VolumeID: !Ref HanaSharedVolume1
Device: !Join [ "", [ '/dev/', !FindInMap [ DeviceName, Node, Shared ] ] ]
Type: "AWS::EC2::VolumeAttachment"
Properties:
InstanceId:
!Ref OTCSADM
VolumeId:
!Ref app
Type: AWS::EC2::VolumeAttachment
Properties:
Device: '/dev/xvdb'
VolumeId: !Ref volume1
InstanceId: !Ref openshiftmaster
"Type": "AWS::EC2::VolumeAttachment",
"Properties": {
"InstanceId": {
"Ref": "EC2Instance"
},
"VolumeId": {
"Type" : "AWS::EC2::VolumeAttachment",
"Properties" : {
"InstanceId" : { "Ref" : "MongoInstanceMemberArbiter" },
"VolumeId" : { "Ref" : "MongoVolume1MemberArbiter" },
"Device" : "/dev/sdh1"
}
"Type": "AWS::EC2::VolumeAttachment",
"Properties": {
"InstanceId": {
"Ref": "OpenShiftMaster1"
},
"VolumeId": {
"Type": "AWS::EC2::VolumeAttachment",
"Properties": {
"InstanceId": {
"Ref": "OpenShiftMaster1"
},
"VolumeId": {
"Type" : "AWS::EC2::VolumeAttachment",
"Properties" : {
"InstanceId" : { "Ref" : "MongoDBNode8EBS" },
"VolumeId" : { "Ref" : "EBSVolume1" },
"Device" : "/dev/sdf"
}
Parameters
-
Device
required - String -
InstanceId
required - String -
VolumeId
required - String
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.