Possibility of Querying EC2 tags from within instance

+2 votes

Amazon lets you  tag EC2 instances with key-value pairs. This enables proper management of large numbers of VMs efficient and easy

Are there ways to query these tags in the same way as some of the other user-set data? For example:

$ wget -q -O - http://169.254.169.254/latest/meta-data/placement/availability-zone
us-east-1d

Apr 10, 2018 in Cloud Computing by hemant
• 5,750 points
61 views

5 answers to this question.

+1 vote


Try amalgamating AWS new tag API and AWS Metadata tool and i belive you will be able to retrieve tags from current instance

answered Apr 10, 2018 by brat_1
• 7,080 points
+1 vote

Use the following code in python to query your EC2 instances

from boto import utils, ec2
from os import environ

# import keys from os.env or use default (not secure)
aws_access_key_id = environ.get('AWS_ACCESS_KEY_ID', failobj='XXXXXXXXXXX')
aws_secret_access_key = environ.get('AWS_SECRET_ACCESS_KEY', failobj='XXXXXXXXXXXXXXXXXXXXX')

#load metadata , if  = {} we are on localhost
# http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AESDG-chapter-instancedata.html
instance_metadata = utils.get_instance_metadata(timeout=0.5, num_retries=1)
region = instance_metadata['placement']['availability-zone'][:-1]
instance_id = instance_metadata['instance-id']

conn = ec2.connect_to_region(region, aws_access_key_id=aws_access_key_id, aws_secret_access_key=aws_secret_access_key)
# get tag status for our  instance_id using filters
# http://docs.aws.amazon.com/AWSEC2/latest/CommandLineReference/ApiReference-cmd-DescribeTags.html
tags = conn.get_all_tags(filters={'resource-id': instance_id, 'key': 'status'})
if tags:
    instance_status = tags[0].value
else:
    instance_status = None
    logging.error('no status tag for '+region+' '+instance_id)
answered Oct 25, 2018 by Nabarupa
+1 vote

Install AWS CLI:

curl "https://s3.amazonaws.com/aws-cli/awscli-bundle.zip" -o "awscli-bundle.zip"
sudo apt-get install unzip
unzip awscli-bundle.zip
sudo ./awscli-bundle/install -i /usr/local/aws -b /usr/local/bin/aws

Get the tags for the current instance:

aws ec2 describe-tags --filters "Name=resource-id,Values=`ec2metadata --instance-id`"

Outputs:

{
    "Tags": [
        {
            "ResourceType": "instance", 
            "ResourceId": "id", 
            "Value": "Webserver", 
            "Key": "Name"
        }
    ]
}

Use a bit of perl to extract the tags:

aws ec2 describe-tags --filters \
"Name=resource-id,Values=`ec2metadata --instance-id`" | \
perl -ne 'print "$1\n" if /\"Value\": \"(.*?)\"/'

Returns:

Webserver
answered Oct 25, 2018 by deamon
+1 vote

You can download and run a standalone executable to do this

Many a times you cannot install awscli that depends on python and neither docker.

Here is what i found using golang, this persons implementation is easy to understand and worked well for me: https://github.com/hmalphettes/go-ec2-describe-tags

answered Oct 25, 2018 by doremon
+1 vote

You can use the describe-instances cli call rather than describe-tags:

Here is an example that guides you as how to get the value of 'my-tag-name' for the instance:

aws ec2 describe-instances --instance-id $(curl -s http://169.254.169.254/latest/meta-data/instance-id) --region ap-southeast-1 --query "Reservations[*].Instances[*].Tags[?Key=='my-tag-name'].Value" --output text
answered Oct 25, 2018 by nabarupa

Related Questions In Cloud Computing

0 votes
3 answers

Using EC2 instance to access S3 bucket locally

You can use the boto library to ...READ MORE

answered Nov 30, 2018 in Cloud Computing by Aniket
522 views
0 votes
2 answers

How can I route request to EC2 instance behind AWS ELB?

You can have a look at the ...READ MORE

answered Aug 17, 2018 in Cloud Computing by Priyaj
• 56,520 points
103 views
0 votes
1 answer

Is there a way to use amazon ECR image from one account in the code build of another account?

These forums might just about help https://forums.aws.amazon.com/t ...READ MORE

answered May 7, 2018 in Cloud Computing by brat_1
• 7,080 points
145 views
0 votes
1 answer

Does it make sense to have an Amazon Elastic Load Balancer with just one EC2 instance?

ELB automatically distributes incoming application traffic across ...READ MORE

answered Jul 12, 2018 in Cloud Computing by ArchanaNagur
• 2,270 points
80 views
0 votes
1 answer

SSH access to Amazon EC2 instance shows permission denied

The error is an authentication error. The ...READ MORE

answered Jul 16, 2018 in Cloud Computing by Gopalan
• 1,260 points
104 views
0 votes
1 answer

How to increase the EBS volume size of an instance that is on running state ?

Unluckily, it is not possible to increase ...READ MORE

answered Jul 17, 2018 in Cloud Computing by Gopalan
• 1,260 points
74 views
0 votes
1 answer

How to duplicate AWS EC2 instance without any downtime?

Technically it is not possible, because the ...READ MORE

answered Jul 24, 2018 in Cloud Computing by Gopalan
• 1,260 points
75 views
0 votes
1 answer

How to self terminate an EC2 instance?

To self-terminate an EC2 instance, follow the ...READ MORE

answered Jul 26, 2018 in Cloud Computing by Gopalan
• 1,260 points
125 views
+2 votes
2 answers

Can I run 'manage.py' from AWS EB Linux instance

You can refer to this documentation :- https://docs.aws.amazon.com/el ...READ MORE

answered Aug 17, 2018 in Cloud Computing by Priyaj
• 56,520 points
161 views
0 votes
1 answer

EC2 Instance: Auto Scheduling

Yes, this possible. Add a schedule to ...READ MORE

answered Apr 13, 2018 in Cloud Computing by brat_1
• 7,080 points
18 views