简体   繁体   中英

How to use EC2 api to tell instance status?

In the AWS console, you can see what instances are online, what are shutting down, and what are shut down. I'm trying to replicate this functionality in my application, but EC2 api doesn't seem to cooperate.

Here's what I'm doing:

            DescribeInstanceStatusRequest rr=new DescribeInstanceStatusRequest();
            rr.InstanceIds=new List<string>(new[]{instanceId});
            var status = ec2.DescribeInstanceStatus(rr);
            List<InstanceStatus> statusses = new List<InstanceStatus>();
            foreach (var s in status.InstanceStatuses)
            {
                if (s.InstanceId == instanceId)
                {
                    statusses.Add(s);
                }
            }
            if (statusses.Any())
            {
                var instanceStatus = statusses.First();
                ...
            }

This works fine when the instance is online, but as soon as I request to shut it down, the instance disappears from the info.

How do I get info for all instances, including those shutting down, shut down and terminated ones?

By default, DescribeInstanceStatus only captures instances that are running. You can set the property IncludeAllInstances in the request to true to change this. From the documentation:

IncludeAllInstances

When true, includes the health status for all instances. When false, includes the health status for running instances only.

Default: false

Code example:

DescribeInstanceStatusRequest rr = new DescribeInstanceStatusRequest()
{
    IncludeAllInstances = true
};

Reference:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM