/// <summary> /// Gets the public IP of the EC2 host that runs that tasks' containers. /// </summary> /// <param name="ecsTask"></param> /// <returns></returns> private async Task <Instance> GetTaskHostInstanceAsync(Amazon.ECS.Model.Task ecsTask) { var containerInstances = await _ecs.DescribeContainerInstancesAsync(new DescribeContainerInstancesRequest { Cluster = ecsTask.ClusterArn, ContainerInstances = new List <string> { ecsTask.ContainerInstanceArn } }); var containerInstance = containerInstances.ContainerInstances.FirstOrDefault(); if (containerInstance == null) { return(null); } var instances = await _ec2.DescribeInstancesAsync(new DescribeInstancesRequest { InstanceIds = new List <string> { containerInstance.Ec2InstanceId } }); var instance = instances.Reservations.SelectMany(r => r.Instances).FirstOrDefault(); if (instance == null) { return(null); } return(instance); }
// **Step 2** Look up the current container's status via AWS SDK public static Boolean QuerySdkTaskStatus(string cluster, string uuid) { Boolean taskSdkStatus = false; AmazonECSClient ecs = new AmazonECSClient(); Task <DescribeTasksResponse> response = ecs.DescribeTasksAsync( new DescribeTasksRequest { Cluster = cluster, Tasks = new List <string> { uuid } } ); // this will run until completion or until task is killed due to timeout while (response.Status != TaskStatus.RanToCompletion) { // failure possible if container doesn't have access to 'ecs.DescribeTasks' if (response.Status == TaskStatus.Faulted) { Console.WriteLine("Error in getting a response from request."); break; } } if (response.Result.Tasks.Count > 0) { Amazon.ECS.Model.Task currentTask = response.Result.Tasks[0]; Console.WriteLine("Task stopped reason: {0}", currentTask.StoppedReason); if (!ValidTaskStopReason(currentTask.StoppedReason)) { taskSdkStatus = true; } else { Console.WriteLine("Task has stopped with valid reason: {0}", currentTask.StoppedReason); } } return(taskSdkStatus); }