/// <summary> /// Starts an EC2 instance /// </summary> /// <param name="instanceId"></param> /// <returns></returns> public static EC2Instance StartInstance(string instanceId) { var instance = new EC2Instance() { InstanceId = instanceId }; var request = new StartInstancesRequest(); request.InstanceId = new List<string>(); request.InstanceId.Add(instanceId); try { var response = EC2.Provider.StartInstances(request); var stateChanges = response.StartInstancesResult.StartingInstances; var runningInstance = (from i in stateChanges where i.InstanceId == instanceId select i).FirstOrDefault(); if (runningInstance != null) { instance.CurrentStateName = runningInstance.CurrentState.Name; } } catch (Exception ex) { MessageBox.Show(string.Format("Error calling AWS.StartInstances: {0}", ex.Message)); } return instance; }
/// <summary> /// Gets the current state of an EC2 instance /// </summary> /// <param name="instanceId"></param> /// <returns></returns> public static EC2Instance GetInstance(string instanceId) { var instance = new EC2Instance() { InstanceId = instanceId }; var request = new DescribeInstancesRequest(); request.InstanceId = new List<string>(); request.InstanceId.Add(instanceId); try { var response = Provider.DescribeInstances(request); var reservation = response.DescribeInstancesResult.Reservation[0]; var runningInstance = (from i in reservation.RunningInstance where i.InstanceId == instanceId select i).FirstOrDefault(); if (runningInstance != null) { instance.CurrentStateName = runningInstance.InstanceState.Name; if (instance.IsRunning()) { instance.PublicDnsName = runningInstance.PublicDnsName; } } } catch (Exception ex) { MessageBox.Show(string.Format("Error calling AWS.DescribeInstances: {0}", ex.Message)); } return instance; }