如何从ec2实例中找到ec2实例的实例id ?
当前回答
简单的一行
猫/ sys /设备/虚拟/ dmi / id / board_asset_tag
or
Curl_cli -s http://169.254.169.254/latest/meta-data/instance-id
来源:https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/identify_ec2_instances.html
其他回答
我写了一个FUSE文件系统来提供对EC2元数据服务的访问:https://github.com/xdgc/ec2mdfs。 我在所有自定义ami上运行这个;它允许我使用这个习语:cat /ec2/meta-data/ami-id
我从http api为EC2元数据编写的c# .net类。 我将根据需要构建它的功能。如果你喜欢,你可以用它。
using Amazon;
using System.Net;
namespace AT.AWS
{
public static class HttpMetaDataAPI
{
public static bool TryGetPublicIP(out string publicIP)
{
return TryGetMetaData("public-ipv4", out publicIP);
}
public static bool TryGetPrivateIP(out string privateIP)
{
return TryGetMetaData("local-ipv4", out privateIP);
}
public static bool TryGetAvailabilityZone(out string availabilityZone)
{
return TryGetMetaData("placement/availability-zone", out availabilityZone);
}
/// <summary>
/// Gets the url of a given AWS service, according to the name of the required service and the AWS Region that this machine is in
/// </summary>
/// <param name="serviceName">The service we are seeking (such as ec2, rds etc)</param>
/// <remarks>Each AWS service has a different endpoint url for each region</remarks>
/// <returns>True if the operation was succesful, otherwise false</returns>
public static bool TryGetServiceEndpointUrl(string serviceName, out string serviceEndpointStringUrl)
{
// start by figuring out what region this instance is in.
RegionEndpoint endpoint;
if (TryGetRegionEndpoint(out endpoint))
{
// now that we know the region, we can get details about the requested service in that region
var details = endpoint.GetEndpointForService(serviceName);
serviceEndpointStringUrl = (details.HTTPS ? "https://" : "http://") + details.Hostname;
return true;
}
// satisfy the compiler by assigning a value to serviceEndpointStringUrl
serviceEndpointStringUrl = null;
return false;
}
public static bool TryGetRegionEndpoint(out RegionEndpoint endpoint)
{
// we can get figure out the region end point from the availability zone
// that this instance is in, so we start by getting the availability zone:
string availabilityZone;
if (TryGetAvailabilityZone(out availabilityZone))
{
// name of the availability zone is <nameOfRegionEndpoint>[a|b|c etc]
// so just take the name of the availability zone and chop off the last letter
var nameOfRegionEndpoint = availabilityZone.Substring(0, availabilityZone.Length - 1);
endpoint = RegionEndpoint.GetBySystemName(nameOfRegionEndpoint);
return true;
}
// satisfy the compiler by assigning a value to endpoint
endpoint = RegionEndpoint.USWest2;
return false;
}
/// <summary>
/// Downloads instance metadata
/// </summary>
/// <returns>True if the operation was successful, false otherwise</returns>
/// <remarks>The operation will be unsuccessful if the machine running this code is not an AWS EC2 machine.</remarks>
static bool TryGetMetaData(string name, out string result)
{
result = null;
try { result = new WebClient().DownloadString("http://169.254.169.254/latest/meta-data/" + name); return true; }
catch { return false; }
}
/************************************************************
* MetaData keys.
* Use these keys to write more functions as you need them
* **********************************************************
ami-id
ami-launch-index
ami-manifest-path
block-device-mapping/
hostname
instance-action
instance-id
instance-type
local-hostname
local-ipv4
mac
metrics/
network/
placement/
profile
public-hostname
public-ipv4
public-keys/
reservation-id
security-groups
*************************************************************/
}
}
在Amazon Linux ami上,你可以做:
$ ec2-metadata -i
instance-id: i-1234567890abcdef0
或者,在Ubuntu和其他一些linux版本上,ec2metadata——instance-id(这个命令可能不会在Ubuntu上默认安装,但你可以用sudo apt-get install cloud-utils添加它)
顾名思义,您也可以使用该命令获取其他有用的元数据。
对于。net用户:
string instanceId = new StreamReader(
HttpWebRequest.Create("http://169.254.169.254/latest/meta-data/instance-id")
.GetResponse().GetResponseStream())
.ReadToEnd();
您可以通过传递元数据参数发出HTTP请求来获取任何元数据。
curl http://169.254.169.254/latest/meta-data/instance-id
or
wget -q -O - http://169.254.169.254/latest/meta-data/instance-id
您不会为获取元数据和用户数据的HTTP请求收费。
Else
您可以使用EC2实例元数据查询工具,这是一个简单的bash脚本,它使用curl从文档中提到的正在运行的EC2实例中查询EC2实例元数据。
下载工具:
$ wget http://s3.amazonaws.com/ec2metadata/ec2-metadata
现在运行命令获取所需的数据。
$ec2metadata -i
参考:
http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-metadata.html
https://aws.amazon.com/items/1825?externalID=1825
乐意帮忙。:)
推荐文章
- 警告:未受保护的私钥文件!当尝试SSH到Amazon EC2实例时
- 使用boto3连接CloudFront时,如何选择AWS配置文件
- 在亚马逊云服务器上设置FTP
- 使用scp将文件复制到Amazon EC2实例?
- 无法将图像推送到Amazon ECR -由于“没有基本的身份验证凭据”而失败
- 如何测试AWS命令行工具的凭据
- 将Keypair添加到现有的EC2实例中
- AWS S3:您试图访问的桶必须使用指定的端点寻址
- 你会因为EC2上的“停止”实例而被收费吗?
- 下载一个已经上传的Lambda函数
- S3 - Access-Control-Allow-Origin头
- 何时使用Amazon Cloudfront或S3
- 如何处理错误与boto3?
- 什么数据存储在亚马逊EC2实例的临时存储?
- boto3 client NoRegionError:只能在某些时候指定区域错误