LoginSignup
1
1

More than 5 years have passed since last update.

EC2インスタンスから自分自身のNameタグを取得(Python版)

Last updated at Posted at 2017-12-28

こいつのpython版

import boto3
import requests

region = requests.get('http://169.254.169.254/latest/meta-data/local-hostname').text.split('.')[1]
instance_id = requests.get('http://169.254.169.254/latest/meta-data/instance-id').text

client = boto3.client('ec2', region_name=region)
for tag in client.describe_tags(Filters=[{ 'Name': 'resource-id', 'Values': [ instance_id ] }])['Tags']:
  if tag['Key'] == 'Name':
    print(tag['Value'])
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1