Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

AWS CLI - Instances, Volumes & Tagging

Posted on 2016-12-01
2
Medium Priority
?
450 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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Or at least that’s the word according to a new blog from Tech Target on AWS’s new Managed Services (MS) offering. According to the blog, AWS is launching their AWS MS program to expedite the adoption of cloud by Fortune 1000 and Global 2000 companie…
In the wake of AWS' S3 outage, we want to discuss the importance of storage and data diversification in the event of a hack, crash, or system disruption. We spoke with Experts Exchange’s COO Gene Richardson for a deeper understanding.
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

715 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