简体   繁体   English

从元数据中获取 ec2 实例名称

[英]Get an ec2 instance name from metadata

I need to grab the instance name from EC2 instance for my python script.我需要为我的 python 脚本从 EC2 实例中获取实例名称。 But stuck how to do that.但坚持如何做到这一点。

My script for other data:我的其他数据脚本:

#!/usr/bin/python
from ec2_metadata import ec2_metadata
import sys
print("Region =  " + ec2_metadata.region)

Need something similar to that.需要类似的东西。

I've tried virtual_name but it is not working.我试过virtual_name但它不起作用。

What do you mean "instance name"?你是什么意思“实例名称”?

If it is the first column in the console, try to get the Tags data with "Name" is the key如果是控制台的第一列,尝试获取以“名称”为关键的标签数据

You have to use boto3 to get the Tags and locate the lone with Name as key:您必须使用boto3来获取标签并以Name为键找到孤独的人:

ec2 = boto3.resource('ec2')
instance = ec2.Instance('intance_id')
print(instance.tags)

Your instance will also need instance role with permissions to interact with ec2.您的实例还需要具有与 ec2 交互权限的实例角色

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM