Identify currently-executing EC2 instance
I have an EC2 instance that 开发者_如何学Pythondoes work for some time, and when it's finished I want it to terminate itself. Seems like a simple request, but I can't figure out how to have an instance uniquely identify itself. The AWS API lets me enumerate the running instances, but if I have several that are booted from the same AMI, how do I find the current one?
I'm running a Windows Server 2008 instance, using the AWS .NET toolkit.
Some options for you to pursue:
- Base on the phrasing of your question, I believe you are trying to get the instance id from within the server and then use that as input to terminate the server. A call from within your server to
http://169.254.169.254/latest/meta-data/instance-id
will give you the instance id needed to terminate the machine. You can then feed that ID to yourTerminateInstancesRequest
.NET API and make the call to terminate the machine. - Just write a program to shut down the server. Unless I am mistaken, shutting down the server will terminate it (as opposed to just stopping it)? Seems much easier than option 1.
- You can use a third-party tool like http://LabSlice.com to automatically schedule a machine termination after a fixed time period (say, after 1 hour). This may not be a bad idea... Keep in mind that you are billed per hour, not per minute. So if you have a short running task then it won't make a difference to your bill if you terminate the server immediately after task completion (after 10 minutes), or if you automated a termination after exactly 1 hour.
not exactly sure if this helps, but another option is to put your instance(s) behind an Auto Scaling Group and setup a heartbeat/health check url against your service endpoint running within the instance.
this way, when your service is done processing, it can stop responding to the heartbeat and your instance will be terminated.
you'll need to adjust the min/max instance properties of the ASG to accomodate whatever use case you're trying to satisfy.
精彩评论