Solved

AWS CLI - Instances, Volumes & Tagging

Posted on 2016-12-01
2
3 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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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.
Utilizing an array to gracefully append to a list of EmailAddresses
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.:
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)

763 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

11 Experts available now in Live!

Get 1:1 Help Now