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


こいつの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'])