如何获取实例中的 AWS OpsWorks 实例 ID?

How to get AWS OpsWorks instance id within an instance?

以下是获取我当前实例的OpsWorks实例id的方法:

ec2_instance_id=$(ec2-metadata -i)

aws opsworks describe-stacks ---> returns all the stacks

iterate over all stack_ids from above:
    aws opsworks describe-instances --stack-id <stack-id>
    
    iterate over all instances from above:
         if ec2_instance_id == instance's ec2 id:
              return instance's opsworks id

但除此之外,还有什么方法可以从实例中计算出我们的 OpsWorks ID?此信息是否存储在实例本身的任何位置?

我每次登录一个实例,它的摘要打印如下:

 This instance is managed with AWS OpsWorks.

   ######  OpsWorks Summary  ######
   Operating System: Amazon Linux AMI release 2018.03
   OpsWorks Instance: XXXXXX
   OpsWorks Instance ID: XXXXXX
   OpsWorks Layers: XXXXXX
   OpsWorks Stack: XXXXXX
   EC2 Region: XXXXXX
   EC2 Availability Zone: XXXXX
   EC2 Instance ID: XXXXXX
   Public IP: XXXXXXX
   Private IP: XXXXXX
   VPC ID: XXXXX
   Subnet ID: XXXXX

实例中是否有我可以找到此信息的文件?

亚马逊Linux 2,ID在/etc/aws/opsworks/instance-agent.yml

这样你就可以得到它:

ops_instance_id=$(sudo grep identity /etc/aws/opsworks/instance-agent.yml | cut -d ' ' -f2)

echo $ops_instance_id

示例输出:

54e0799e-bf86-426f-b08c-06530ca6b914

也许有更简单的方法。我只知道上面一个