Following is the way to get OpsWorks instance id of my current instance:
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
But instead of this, is there any way to figure our the OpsWorks id from an instance? Is this information stored anywhere in the instance itself?
Every time I log in to an instance, it's summary is printed as follows:
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
Is there any file inside the instance where I can find this information?
On Amazon Linux 2, the ID is in /etc/aws/opsworks/instance-agent.yml
Thus you could get it:
ops_instance_id=$(sudo grep identity /etc/aws/opsworks/instance-agent.yml | cut -d ' ' -f2)
echo $ops_instance_id
example output:
54e0799e-bf86-426f-b08c-06530ca6b914
Maybe there is simpler way. I know only of the above one.