How can I determine an EC2 instances index/position in an Auto-scaling group?

I'm working on some automation scripts to bootstrap my application and will be using an auto-scaling group in AWS to spin up/down EC2 instances for my application.

I need to be able to re-create a consistent name for the machine that follows a pattern: MYAPP001, MYAPP002, MYAPP003.

The Question

Is there a way to query meta-data during the user-data bootstrapping phase to determine that this machine that just came up is 002 in the group?

If I can determine this, then I can rename the machine to MYAPP002.

Scenario to Help Explain

Imagine a 3rd-party service that expects machines to be named MYAPP001, MYAPP002, MYAPP003. In this software, it's OK for a machine to go offline and come back online later with a different IP address. However, you cannot have two different machines talk to this software at the same time with the same host name.


The documentation says that it's possible: Example: AMI Launch Index Value.

$ TOKEN=$(curl -X PUT "http://169.254.169.254/latest/meta-data/api/token"-H "X-aws-ec2-metadata-token-ttl-seconds: 21600") \
&& curl -H "X-aws-ec2-metadata-token: $TOKEN" -v http://169.254.169.254/latest/meta-data/ami-launch-index