Since many of the OpsWorks APIs take an OpsWorks id (different than an EC2 instance id), it seems like there should be an easy way to get the id. There is an opswork-agent-cli stack_state
command that returns a JSON blob that has includes the id, but that still requires parsing, and I can't be sure what tools will be available on the instance. It is reasonably easy to parse the id out of the JSON using shell commands, but they feel like an ugly hack. Are there any commands I'm missing or other ways to get an instance to report its id?
I think you have to parse it.
You can use jq
to parse JSON data, like it's typically done when reading EC2 instance metadata. jq
package is included in AWS Linux AMIs (see available packages).
In your case, try opswork-agent-cli stack_state | jq '.stack.stack_id'
.