Search code examples
amazon-web-servicesaws-sdkamazon-ecs

ECS DescribeTasksResponse always returns an empty `networkBindings`


I couldn't find a way to get the public IP the docs says it should be in the response:

 * const command = new DescribeTasksCommand(input);
 * const response = await client.send(command);
 * // { // DescribeTasksResponse
 * //   tasks: [ // Tasks
 * //     { // Task
 * //       attachments: [ // Attachments
 * //        ...
 * //       ],
 * //       ...
 * //       containers: [ // Containers
 * //         { // Container
 * //           containerArn: "STRING_VALUE",
 * //           taskArn: "STRING_VALUE",
 * //           name: "STRING_VALUE",
 * //           ...
 * //           networkBindings: [ // NetworkBindings
 * //             { // NetworkBinding
 * //               bindIP: "STRING_VALUE",
 * //               containerPort: Number("int"),
 * //               hostPort: Number("int"),
 * //               protocol: "tcp" || "udp",
 * //               containerPortRange: "STRING_VALUE",
 * //               hostPortRange: "STRING_VALUE",
 * //             },
 * //           ],

My code:


  let waitECSTask = await waitUntilTasksRunning(
    {"client": ecsClient, "maxWaitTime": 6000, "maxDelay": 1, "minDelay": 1}, 
    {"cluster": "my_cluster", "tasks": [taskArn]}
  )

But I'm always getting an empty list in waitECSTask of networkBindings but looking at AWS console I do see that a public IP was allocated.


Solution

  • I was refereed to the link: https://medium.com/the-aws-coder/create-and-run-aws-ecs-task-programmatically-using-net-5-c-aws-ecs-sdk-82b31df98fd3 It's in c# but does provide a good start, so apparently there's no simple solution, the way to get the public IP is by the EC2 API.

    This is how the code in TS looks like:

        import { EC2Client, DescribeNetworkInterfacesCommand } from "@aws-sdk/client-ec2";
    
        ...
    
    
        const command = new DescribeNetworkInterfacesCommand({
            Filters: [
                {
                    Name: "private-ip-address",
                    Values: [waitECSTask.reason?.tasks[0].containers[0].networkInterfaces[0].privateIpv4Address],
                },
            ],
        });
    
        // Get the public IP address of the ECS task
        let network = await ec2Client.send(command)
        let publicIp = network.NetworkInterfaces?.[0].Association?.PublicIp?.toString() ?? "";