skip to Main Content

Is there a way/method in the EC2/AWS API such that I can get the EBS volume that is attached to a particular EC2 instance?

IE:
I just want something that allows me to do something along the lines of:
method(instance_id) – returns the EBS volume that this particular instance_id is attached to.

2

Answers


  1. Yes, you can use the describe-instances command from the AWS CLI.

    An example:

    aws ec2 describe-instances --query 'Reservations[*].Instances[?InstanceId==`i-058aba2af1c14e93e`].BlockDeviceMappings[*]'
    
    Login or Signup to reply.
  2. If you know your instance ID, you can use CLI command:

    aws ec2 describe-volumes 
        --region us-east-1 
        --filters Name=attachment.instance-id,Values=i-08e0325f9e7218a02
    
    

    where Values is my instance ID.

    The answer looks like:

    {
        "Volumes": [
            {
                "Attachments": [
                    {
                        "AttachTime": "2023-02-28T23:44:46+00:00",
                        "Device": "/dev/xvda",
                        "InstanceId": "i-08e0325f9e7218a02",
                        "State": "attached",
                        "VolumeId": "vol-087d2d787f60de5d8",
                        "DeleteOnTermination": true
                    }
                ],
                "AvailabilityZone": "us-east-1b",
                "CreateTime": "2023-02-28T23:44:46.880000+00:00",
                "Encrypted": false,
                "Size": 8,
                "SnapshotId": "snap-0d521a3c01cf13eee",
                "State": "in-use",
                "VolumeId": "vol-087d2d787f60de5d8",
                "Iops": 100,
                "VolumeType": "gp2",
                "MultiAttachEnabled": false
            }
        ]
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search