Skip to content

Instantly share code, notes, and snippets.

@omerxx
Created July 10, 2019 09:33
Show Gist options
  • Save omerxx/656217bb1ad177d2d7cee4ca9c5fae3a to your computer and use it in GitHub Desktop.
Save omerxx/656217bb1ad177d2d7cee4ca9c5fae3a to your computer and use it in GitHub Desktop.
Getting EC2 instance tags from within the instance
import boto3
import requests
def _get_metadata_region():
r = requests.get(
'http://169.254.169.254/latest/dynamic/instance-identity/document')
return r.json()['region']
def _get_instance_id():
r = requests.get('http://169.254.169.254/latest/meta-data/instance-id')
return r.text
def get_instance_tags():
client = boto3.client('ec2', region_name=_get_metadata_region())
response = client.describe_tags(
Filters=[
{
'Name': 'resource-id',
'Values': [
_get_instance_id(),
]
},
],
)
print(response['Tags'])
get_instance_tags()
@tinproject
Copy link

@kesor No, you can not restrict that action in an IAM policy...
describetagspolicy

FYI: don't lose time trying to do this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment