Solved

AWS CLI - Instances, Volumes & Tagging

Posted on 2016-12-01
2
264 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 14

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Exchange server is not supported in any cloud-hosted platform (other than Azure with Azure Premium Storage).
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

635 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