Solved

AWS CLI - Instances, Volumes & Tagging

Posted on 2016-12-01
2
71 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 12

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

With the spotlight very much on Cloud technology within the IT industry, it’s difficult to avoid the topic these days. Due to the constant flood of new information, added pressure, emphasis and focus on cloud migration is driving corporations to inv…
This article provides a guide on how to optimise your costs within your AWS infrastructure when using some of the common services such as EC2, EBS, S3, Glacier, CloudFront, EIP & ELB.
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

932 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now