Adam
Adam

Reputation: 4780

Getting startup timestamp of Amazon EC2 instance

I am writing java code for EC2 that will consume messages from SQS. My code will run for the hour and then review the queue to determine whether it should shut itself down (due to not enough items to warrant processing for another hour) or to keep running. In order for this to work, I need to know the precise time stamp of when billing for the instance began so I can add one hour to it and shutdown a few minutes before the hour is up if necessary.

How can I get the launch time stamp (the time AWS begins billing for the instance hour) of the EC2 instance my java code is currently running on using the java SDK preferably?

Upvotes: 4

Views: 1906

Answers (1)

phs
phs

Reputation: 11051

If shelling to uptime or dropping date +%s > /etc/started-at into your bootscript are too platform-dependent for your tastes, you could perform the DescribeInstances SOAP call and pluck out the launchTime field.

To discover the running machine's instance id, you can (from within the machine) make an http GET such as

curl http://169.254.169.254/latest/meta-data/instance-id

Upvotes: 3

Related Questions