user1382613
user1382613

Reputation: 33

Mount EBS volume to a running AWS instance with a script

I'd like to dynamically mount and unmount an EBS volumes to a running AWS instance using a script and was wondering whether this is achievable, both on linux and windows instances, and if so, what is the expected duration of such operation.

Upvotes: 2

Views: 1933

Answers (1)

helloV
helloV

Reputation: 52375

Using AWS CLI and Bourne shell script.

attach-volume

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

aws ec2 attach-volume --volume-id vol-1234567890abcdef0 --instance-id i-01474ef662b89480 --device /dev/sdf

detach-volume

Detaches an EBS volume from an instance. Make sure to unmount any file systems on the device within your operating system before detaching the volume.

aws ec2 detach-volume --volume-id vol-1234567890abcdef0

--------------------------------------------------------------------------

Use Python and Boto3 which has APIs to attach and detach volumes.

attach_volume

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

import boto3

client = boto3.client('ec2')
response = client.attach_volume(
    DryRun=True|False,
    VolumeId='string',
    InstanceId='string',
    Device='string'
)

detach_volume

Detaches an EBS volume from an instance. Make sure to unmount any file systems on the device within your operating system before detaching the volume.

response = client.detach_volume(
    DryRun=True|False,
    VolumeId='string',
    InstanceId='string',
    Device='string',
    Force=True|False
)

Upvotes: 3

Related Questions