简体   繁体   中英

.Net AWS Server Manager

I am trying to build a simple app to start, stop, and change instance types of an AWS Instance.

End goal: Instance Type - Increase or Decrease the Instance Type (t1.micro, t1.large etc) Start/Stop instances -

My question at the moment is regarding the code below:

InstanceState Start(RunningInstance instance)
        {
            using (var client = CreateClient())
            {
                var confirmStart = MessageBox.Show("Are you sure you want to START this server?", "Start Server", MessageBoxButtons.YesNo);
                if (confirmStart == DialogResult.Yes)
                {
                    var doubleCheck = MessageBox.Show("Start the Server?", "Are you sure?", MessageBoxButtons.YesNo);
                    if (doubleCheck == DialogResult.Yes)
                    {
                        var request = new StartInstancesRequest();
                        request.InstanceId.Add(instance.InstanceId);
                        var response = client.StartInstances(request);
                        return response.StartInstancesResult.StartingInstances[0].CurrentState;
                    }
                    else
                        return null;

                }
                else
                    return null;
            }
        }

Basically, double checking if you want to "Start" the server. If the user decides they dont want to start a server, I need it to "Do Nothing" Returning null "Works" for the aspect that nothing changes; however, I am getting a NullReferenceException (Expected). What can I return in order for this to "Do Nothing" or keep the current state" without using Null?

On a side note - I am still new to AWS .Net SDK so any tips would be appreciated.

It sounds to me like you could simply just retrieve the current state of the instance in all cases, even if you don't start it.

To get the instance state of an instance you don't intend to start, you'll need to call another API that describes that instance. There are several that can return the instance state, here are a couple of suggestions for different use cases:

  • If you intend to return instance metadata as well, or are otherwise unsure what all info you may need and want a full RunningInstance object, use DescribeInstances.
  • If you just need the state, DescribeInstanceStatus will return a smaller response.

DescribeInstanceStatus Example

Here is a minimal example function that you could call from both of your else statements. You would use this to supply the InstanceState in cases where you aren't starting the instance.

private InstanceState getInstanceState(string instanceId)
{
    using (IAmazonEC2 client = new AmazonEC2Client())
    {
        var statusResponse = client.DescribeInstanceStatus(new DescribeInstanceStatusRequest()
        {
            InstanceIds = new List<string>() { instanceId }
        });

        return statusResponse?.InstanceStatuses?.FirstOrDefault(
            x => x.InstanceId == instanceId).InstanceState;
    }
}

Error Handling

Note that DescribeInstanceStatus will throw an AmazonEC2Exception with status code 400 BadRequest if the input InstanceId is invalid. You seem like you're already validating the instance's existence, but if intend to have a layer working directly with the AWS API like this you may want to wrap this layer in a try/catch, catch on AmazonEC2Exception, and gracefully handle failures from the AWS API in that manner.

For example, calling to DescribeInstanceStatus could simply be because the InstanceId is invalid, but it could also just be a transient failure or even be due to the EC2 service being down in your region. If the EC2 service is not available, your application is likewise not going to be able to do much, so make sure you catch that and communicate it to your users through graceful error handling.

Further Reading

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