Solved

AWS CLI - Instances, Volumes & Tagging

Posted on 2016-12-01
2
112 Views
Last Modified: 2016-12-01
Hello Expert,

What I am trying to do is for AWS EC2 instances, I am planning to list instances as per a specific tag and find the volumes and then tag those volumes with a specific tag (Name=SSVD)

So here is my non working script, Don't know where the error is

for i in $(aws ec2 describe-instances --filter Name=tag:Name,Values=EHI-SSD --query 'Reservations[*].Instances[*].InstanceId' --output text)
do
for j in $(aws ec2 describe-volumes --region us-east-1 --filters Name=attachment.instance-id,Values=$i --query 'Volumes[*].{ID:VolumeId}' --output text)
aws ec2 create-tags --resources $j --tags Key=Name,Value=SSVD
done
done

Open in new window


Thanks
0
Comment
Question by:Rama Badra
2 Comments
 
LVL 13

Accepted Solution

by:
Phil Phillips earned 500 total points
ID: 41909414
One of your loops is missing a "do".  Also, you specify the region in one of the commands, but not the other.  That could cause issues if the default region is not us-east-1.

Something like this should work:
export AWS_DEFAULT_REGION=us-east-1

for i in $(aws ec2 describe-instances  --filter Name=tag:Name,Values=EHI-SSD --query 'Reservations[*].Instances[*].InstanceId' --output text); do
   echo $i
   for j in $(aws ec2 describe-volumes --filters Name=attachment.instance-id,Values=$i --query 'Volumes[*].{ID:VolumeId}' --output text); do
      echo $j
      aws ec2 create-tags --resources $j --tags Key=Name,Value=SSVD
   done
done

Open in new window

0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question