0
votes

I have to query my AWS account to find latest created volume with specific tags and should have it attached to running EC2 instance. How do I achieve this using aws cli and powershell?

I was using below powershell script and aws cli to achieve this, But I was not able to find out exact query to get latest volume id using aws cli command to replace variable $volumeid. Any help would be appreciated.

$volumeid = "aws ec2 describe-volumes --region us-east-1 --filters Name=tag:Application_Name,Values=APPone Name=tag:Name,Values=APP_test --query "Volumes[*].{ID:VolumeId}"

$instanceId = (New-Object System.Net.WebClient).DownloadString("http://169.254.169.254/latest/meta-data/instance-id")

Add-EC2Volume -VolumeId $volumeid  -InstanceId $instanceId -Device xvdf -Region us-east-1
1

1 Answers

2
votes

You can use the following query to fetch the latest volume id using the specific tag :

aws ec2 describe-volumes --query Volumes[*].[VolumeId] --filters Name=tag-value,Values=testvolume --output text

The above query first describes the volume descriptions of all the volumes.

Then the "--query" returns only the volume ids of all the volumes present.

Later it filters out the volume id based on the Tag you specify.

And the "--output text" converts the output in text format which you can store in the variable.

For more details you can refer describe-volumes

This should help.