如何从ec2实例中找到ec2实例的实例id ?
当前回答
动机:用户想检索aws实例元数据。
解决方案: IP地址169.254.169.254是一个链接本地地址(仅对实例有效)aws为我们提供了用于检索运行实例的元数据的专用Restful API的链接(注意,用于检索实例元数据和用户数据的HTTP请求不会收费)。其他文件
例子:
//Request:
curl http://169.254.169.254/latest/meta-data/instance-id
//Response
ami-123abc
你可以使用这个链接http://169.254.169.254/latest/meta-data/<metadata-field>获得你实例的额外元数据标签,只需选择正确的标签:
ami id ami-launch-index ami-manifest-path 块设备 映射 事件 冬眠 主机名 我 身份凭证 instance-action 实例id 实例类型 local-hostname local-ipv4 mac 指标 网络 放置 配置文件 预订标识 安全组 服务
其他回答
我从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
*************************************************************/
}
}
在Ubuntu上你可以:
sudo apt-get install cloud-utils
然后你就可以:
EC2_INSTANCE_ID=$(ec2metadata --instance-id)
你可以通过这种方式获取大多数与实例相关的元数据:
ec2metadata --help Syntax: /usr/bin/ec2metadata [options] Query and display EC2 metadata. If no options are provided, all options will be displayed Options: -h --help show this help --kernel-id display the kernel id --ramdisk-id display the ramdisk id --reservation-id display the reservation id --ami-id display the ami id --ami-launch-index display the ami launch index --ami-manifest-path display the ami manifest path --ancestor-ami-ids display the ami ancestor id --product-codes display the ami associated product codes --availability-zone display the ami placement zone --instance-id display the instance id --instance-type display the instance type --local-hostname display the local hostname --public-hostname display the public hostname --local-ipv4 display the local ipv4 ip address --public-ipv4 display the public ipv4 ip address --block-device-mapping display the block device id --security-groups display the security groups --mac display the instance mac address --profile display the instance profile --instance-action display the instance-action --public-keys display the openssh public keys --user-data display the user data (not actually metadata)
在Amazon Linux ami上,你可以做:
$ ec2-metadata -i
instance-id: i-1234567890abcdef0
或者,在Ubuntu和其他一些linux版本上,ec2metadata——instance-id(这个命令可能不会在Ubuntu上默认安装,但你可以用sudo apt-get install cloud-utils添加它)
顾名思义,您也可以使用该命令获取其他有用的元数据。
你也可以安装awscli并使用它来获得你想要的所有信息:
AWS_DEFAULT_REGION=your-region aws ec2 describe-instances
你会得到很多输出,所以一定要通过你的标识符grep,如ip,并打印更多的行:
AWS_DEFAULT_REGION=your-region aws ec2 describe-instances | grep your-ip -A 10 | grep InstanceId
对于。net用户:
string instanceId = new StreamReader(
HttpWebRequest.Create("http://169.254.169.254/latest/meta-data/instance-id")
.GetResponse().GetResponseStream())
.ReadToEnd();
推荐文章
- 亚马逊ECS和亚马逊EC2有什么区别?
- 我如何知道我在S3桶中存储了多少对象?
- S3 Bucket操作不应用于任何资源
- 将AWS凭证传递给Docker容器的最佳方法是什么?
- 当权限为S3时,AccessDenied for ListObjects for S3 bucket:*
- 电子邮件地址未验证(AWS SES)
- 使用Boto3将S3对象作为字符串打开
- AWS VPC - Internet网关vs. NAT
- 如何在AWS Lambda中加载npm模块?
- 亚马逊S3 -如何修复“我们计算的请求签名与签名不匹配”错误?
- 警告:未受保护的私钥文件!当尝试SSH到Amazon EC2实例时
- 使用boto3连接CloudFront时,如何选择AWS配置文件
- 在亚马逊云服务器上设置FTP
- 使用scp将文件复制到Amazon EC2实例?
- 无法将图像推送到Amazon ECR -由于“没有基本的身份验证凭据”而失败