如何从ec2实例中找到ec2实例的实例id ?
当前回答
对于。net代码来说非常简单: var instanceId = Amazon.Util.EC2InstanceMetadata.InstanceId
其他回答
如果您还需要查询实例ID以外的其他信息,请使用/dynamic/instance-identity/document URL。
wget -q - o - http://169.254.169.254/latest/dynamic/instance-identity/document
这将为您提供这样的JSON数据——只需要一个请求。
{
"devpayProductCodes" : null,
"privateIp" : "10.1.2.3",
"region" : "us-east-1",
"kernelId" : "aki-12345678",
"ramdiskId" : null,
"availabilityZone" : "us-east-1a",
"accountId" : "123456789abc",
"version" : "2010-08-31",
"instanceId" : "i-12345678",
"billingProducts" : null,
"architecture" : "x86_64",
"imageId" : "ami-12345678",
"pendingTime" : "2014-01-23T45:01:23Z",
"instanceType" : "m1.small"
}
所有与EC2资源相关的元数据都可以由EC2实例本身通过执行以下命令来访问:
旋度:
http://169.254.169.254/<api-version>/meta-data/<metadata-requested>
对于您的情况:"metadata-requested"应该是instance-id, "api-version"通常是可以使用的最新版本。
附加注意:您还可以使用上述命令获取与以下EC2属性相关的信息。
ami id, ami-launch-index, ami-manifest-path, block-device-mapping /, 主机名、 我/, instance-action, 实例id, 实例类型, local-hostname, local-ipv4, mac, 指标/, 网络/, 位置/, 配置文件, public-hostname, public-ipv4, 公钥/, 预订标识, 安全组, 服务/
欲了解更多详情,请点击此链接:https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-metadata.html
对于c++(使用cURL):
#include <curl/curl.h>
//// cURL to string
size_t curl_to_str(void *contents, size_t size, size_t nmemb, void *userp) {
((std::string*)userp)->append((char*)contents, size * nmemb);
return size * nmemb;
};
//// Read Instance-id
curl_global_init(CURL_GLOBAL_ALL); // Initialize cURL
CURL *curl; // cURL handler
CURLcode res_code; // Result
string response;
curl = curl_easy_init(); // Initialize handler
curl_easy_setopt(curl, CURLOPT_URL, "http://169.254.169.254/latest/meta-data/instance-id");
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_to_str);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
res_code = curl_easy_perform(curl); // Perform cURL
if (res_code != CURLE_OK) { }; // Error
curl_easy_cleanup(curl); // Cleanup handler
curl_global_cleanup(); // Cleanup cURL
Python:
import boto.utils
region=boto.utils.get_instance_metadata()['local-hostname'].split('.')[1]
这可以归结为一句话:
python -c "import boto.utils; print boto.utils.get_instance_metadata()['local-hostname'].split('.')[1]"
除了local_hostname,你还可以使用public_hostname,或者:
boto.utils.get_instance_metadata()['placement']['availability-zone'][:-1]
您可以通过传递元数据参数发出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
乐意帮忙。:)
推荐文章
- 亚马逊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 -由于“没有基本的身份验证凭据”而失败