AWS Developer Tools Blog

Instance Status Checks with the AWS SDK for .NET

A question that we have heard from our customers is, "How do we get access to the Amazon EC2 instance status checks?" If you go the AWS Management Console, you can easily see those status checks displayed. The Amazon EC2 DescribeInstanceStatus API action returns the instance status for one or more EC2 instances. This code shows you how to make that call in the AWS SDK for .NET:

var instanceId = "yourInstanceIdHere";
var ec2Client = new AmazonEC2Client(RegionEndpoint.USWest2);
var statusRequest = new DescribeInstanceStatusRequest
{
    InstanceId = { instanceId }
};
var result = ec2Client.DescribeInstanceStatus(statusRequest).DescribeInstanceStatusResult;

This block of code returns an InstanceStatusResult. Inside of this object is an array of InstanceStatus objects. You get one ‘InstanceStatus’ for each instance that you asked for. In this example, since we asked for only one instance, there is only one element in that array. You access this object by retrieving result.InstanceStatus[0]; Status checks are separated into two types: system status checks and instance status checks. For more information about the types of status checks, see the EC2 documentation— Monitoring Instances with Status Checks. This code shows how to access the EC2 instance status checks and writes the output to the console:

//Get the instance status checks
Console.WriteLine("Instance Status = " +
    status.InstanceStatusDetail.Status);
Console.WriteLine("Instance Status Detail Name = " +
    status.InstanceStatusDetail.Detail[0].Name);
Console.WriteLine("Instance Status Detail Status = " +
    status.InstanceStatusDetail.Detail[0].Status;);

//Get the system status checks
Console.WriteLine("System Status = " +
    status.SystemStatusDetail.Status);
Console.WriteLine("System Status Detail Name = " +
    status.SystemStatusDetail.Detail[0].Name);
Console.WriteLine("System Status Detail Status = " +
    status.SystemStatusDetail.Detail[0].Status);

The output for this looks like:

Instance Status = ok
Instance Status Detail Name = reachability
Instance Status Detail Status = passed
System Status = ok
System Status Detail Name = reachability
System Status Detail Status = passed

The current status check API has only one check, so the Detail property contains a single element. The Name and Status properties contain the name of the check, currently only reachability, and the status of the check, which can contain passed | failed | insufficient-data. There is another property, ImpairedSince, which would contain a String of when the status check failed. If the status check was not a failure, this property will be empty. Now that you know how to use instance status checks, how will you use them? Let us know in the comments!