On a running EC2 instance, how do I identify EBS volumes and local disks ? I would like to identify them from shell.
fdisk, lsblk is not helping to pinpoint the EBS volumes only.
Lets say I have total 20 disks, out of 20 identify ebs stores.
Any suggestions?
Best Answer
Every EC2 instance can access a REST endpoint at the link-local address
http://169.254.169.254
that provides access to metadata about that instance.Block device mapping of EBS and instance store volumes are one of several properties available there, and the data is easily accessed from the shell and scripts using a tool like "curl."
http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AESDG-chapter-instancedata.html
This is probably the "most correct" way of discovering what you're looking for.
Update: I hammered out a quick and extremely dirty one-liner to demonstrate this functionality. I don't intend to imply that this is production quality code, nor particularly well-documented, but it definitely works.
The endpoint above appears to offer several "versions" of its interface, most of which use the version release date, or you can use the "latest" version. So that this example code will continue to work even if the interface changes, I'll choose the version "2012-01-12."
We'll store a link in the shell variable $METAURL, retrieve the list of mappings available there with
curl
and then iterate though each mapping with abash
for
/do
/done
loop (again withcurl
) to find the block device... then useperl
to make it tidier and rewrite "sdX" as "xvdX" as my particular instance uses for its device names, and finallysort
to put it in lexical order.Example output:
That was pretty slick and relatively painless.
But, there is one small problem with my answer.
This solution won't tell you about EBS volumes mounted after the instance was launched. That may be fine, because ephemeral volume information is always available, since ephemeral disks can only be added at launch time, not later... so if a volume is ephemeral, you should always be able to discover it via this method. If it's not here, it's not an ephemeral disk.
In light of this, the EC2 API or the aws cli utility (which calls the EC2 API) would be what you'd need to use if you really need to know all there is to know about what's attached to the instance. There's more data there, but it would be somewhat tricker to utilize in scripts. Classic tradeoff, I suppose.