使用脚本将 EBS 卷挂载到 运行 AWS 实例

Mount EBS volume to a running AWS instance with a script

我想使用脚本将 EBS 卷动态挂载和卸载到 运行 AWS 实例,我想知道这是否可以在 linux 和 windows 上实现实例,如果是,这种操作的预期持续时间是多少。

使用 AWS CLI 和 Bourne shell 脚本。

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

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

使用具有 API 的 Python 和 Boto3 来附加和分离卷。

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
)