/** * Waits for the specified instance to transition to the specified state, * otherwise throws a RuntimeException if this method gives up on the * instance ever transitioning to that state. * * @param instanceId * The ID of the instance to wait for. * @param state * The expected state for the instance to transition to. * * @throws Exception * If any problems were encountered while polling the instance's * state, or if this method gives up on the instance ever * transitioning to the expected state. */ public async Task waitForInstanceToTransitionToStateAsync(string instanceId, InstanceStateName state) { Console.WriteLine("Waiting for instance " + instanceId + " to transition to " + state + "..."); int count = 0; while (true) { Thread.Sleep(1000 * 5); count++; Instance runningInstance = (await _ec2Client.DescribeInstancesAsync( new DescribeInstancesRequest { InstanceIds = new List <string> { instanceId } })) .Reservations[0].Instances[0]; if (runningInstance.State.Name == state) { return; } if (count > 100) { throw new Exception("Instance " + instanceId + " never transitioned to " + state); } } }
private static Instance GenerateInstance(string instanceId, InstanceStateName stateName) { return(new Instance { InstanceId = instanceId, State = new InstanceState { Name = stateName } }); }