使用clojure获取instance-id的最佳方法

时间:2021-12-16 22:47:13

I'm using the clojure client amazonica for trying out clojure and write some scripts to help with managing aws ec2 instances.

我正在使用clojure客户端amazonica来尝试clojure并编写一些脚本来帮助管理aws ec2实例。

I'm trying to get the instance-id using this snippet:

我正在尝试使用此代码段获取instance-id:

(def instances ((describe-instances) :reservations))
(def an_instance ((first instances) :instances))
(def instance_id ((first an_instance) :instance-id))

Looking for a way to do this better, so I can use it for any other json output.

寻找一种更好地做到这一点的方法,所以我可以将它用于任何其他json输出。

EDIT: trying to get the instance-id, security groups and tag names from an instance.

编辑:尝试从实例中获取实例ID,安全组和标记名称。

In addition to the above question, when I use the amazonica client which uses the aws java sdk version 1.9.33, I get this on the console:

除了上面的问题,当我使用使用aws java sdk版本1.9.33的amazonica客户端时,我在控制台上得到了这个:

May 10, 2015 1:31:57 PM com.amazonaws.http.AmazonHttpClient logRequestId
INFO: x-amzn-RequestId: not available

What can I do to fix that?

我该怎么做才能解决这个问题?

EDIT: To clarify, I'm trying to this info from outside the VPC or the instance.

编辑:澄清一下,我正在尝试从VPC或实例外部获取此信息。

1 个解决方案

#1


If you want to find the instance-id from the instance itself:

如果要从实例本身中查找instance-id:

(slurp "http://169.254.169.254/latest/meta-data/instance-id")

(See Instance Metadata and User Data for more information.)

(有关详细信息,请参阅实例元数据和用户数据。)

To find selected properties for all your instances:

要查找所有实例的选定属性:

(map #(select-keys % [:instance-id :tags :security-groups])
     (flatten
      (map :instances
           (:reservations
            (amazonica.aws.ec2/describe-instances)))))

#1


If you want to find the instance-id from the instance itself:

如果要从实例本身中查找instance-id:

(slurp "http://169.254.169.254/latest/meta-data/instance-id")

(See Instance Metadata and User Data for more information.)

(有关详细信息,请参阅实例元数据和用户数据。)

To find selected properties for all your instances:

要查找所有实例的选定属性:

(map #(select-keys % [:instance-id :tags :security-groups])
     (flatten
      (map :instances
           (:reservations
            (amazonica.aws.ec2/describe-instances)))))