如何从ec2实例中找到ec2实例的实例id ?


当前回答

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]

其他回答

对于所有ec2机器,实例id可以在以下文件中找到:

    /var/lib/cloud/data/instance-id

实例id也可以通过如下命令获取:

    ec2metadata --instance-id

获取实例元数据使用

wget -q -O - http://169.254.169.254/latest/meta-data/instance-id

你可以试试这个:

#!/bin/bash
aws_instance=$(wget -q -O- http://169.254.169.254/latest/meta-data/instance-id)
aws_region=$(wget -q -O- http://169.254.169.254/latest/meta-data/hostname)
aws_region=${aws_region#*.}
aws_region=${aws_region%%.*}
aws_zone=`ec2-describe-instances $aws_instance --region $aws_region`
aws_zone=`expr match "$aws_zone" ".*\($aws_region[a-z]\)"`

如果您还需要查询实例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"
}

在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)