How do I get the ID or IP address of an Amazon EC2 instance for an AWS Batch job?

Last updated: 2020-02-25

I want to get the ID or IP address of an Amazon Elastic Compute Cloud (Amazon EC2) instance for an AWS Batch job.

Short Description

To get information about the Amazon EC2 instance that runs on your AWS Batch job, you must first get the container ID of your job. Then, you can use the container instance ID to get your EC2 instance ID. Finally, you can use your EC2 instance ID to get the IP address of that instance.

Resolution

Get the container instance ID of your AWS Batch job

1.    To get the container instance ID of your AWS Batch job, run the following describe-jobs command:

$ aws batch describe-jobs --jobs YourJobID

The output describes all the properties and parameters of the job, including the container instance ID. The containerInstanceArn property shows your container instance ID. See the following example:

{
    "jobs": [
        {
            "status": "SUCCEEDED",
            "container": {
                "mountPoints": [],
                "taskArn": "arn:aws:ecs:us-east-1:1234567890:task/8fde8ebf-c5d8-40ca-8999-b0c2185f577a",
                "logStreamName": "job-definition/default/8fde8ebf-c5d8-40ca-8999-b0c2185f577a",
                "image": "busybox",
                "containerInstanceArn": "arn:aws:ecs:us-east-1:1234567890:container-instance/Your_Container_Instance_ID",
                "environment": [],
                "vcpus": 2,
                "jobRoleArn": "arn:aws:iam::1234567890:role/ecsTaskExecutionRole",
                "volumes": [],
                "memory": 2000,
                "command": [
                    "echo",
                    "'hello world'"
                ],
            },
}

2.    To find the Amazon Elastic Container Service (Amazon ECS) cluster that corresponds to your AWS Batch compute environment, open the Amazon ECS console, and then choose Clusters.

Note: Every AWS Batch compute environment that you create has a corresponding Amazon ECS cluster.

3.    On the Services tab, search for the cluster name that appears in the following format: ComputeEnvironmentName_Batch_RandomStringOfCharacters

Get the EC2 instance ID of your job

To get the EC2 instance ID, run the describe-container-instances command and pass in the container instance ID that you retrieved in the preceding step 1:

$ aws ecs describe-container-instances --cluster Your_Cluster_Name --container-instances Your_Container_Instance_ID

The output shows the EC2 instance ID. See the following example:

CONTAINERINSTANCES          True     arn:aws:ecs:us-east-1:1234567890:container-instance/Your_EC2_Instance_ID           i-0ab1cdefghij23k45    0          1234567890.12           0          ACTIVE 5

Get the IP address of the instance

Now that you have the EC2 instance ID, you can use that ID to find the IP address of your EC2 instance.

1.    Open the Amazon EC2 console.

2.    Choose Running instances.

3.    In the Instance ID column, look for the instance ID that you retrieved earlier.

4.    For the instance that you identify in the preceding step 3, look for its IP address in the IPv4 Public IP address column.

Get instance information for multi-node AWS Batch jobs

To get the container instance ID for the child job, run the following describe-jobs command on an individual child job:

$ aws batch describe-jobs --jobs Your_Job_ID#1

Important: The parent job doesn't have information on the container instance, as each child job is on a unique node. For child jobs, use node notation to append the index of the child job to the job ID. In the preceding command, #1 accesses the first node of the job. #2 accesses the second node, and so on. For more information, see Multi-node Parallel Jobs.

To get the EC2 instance ID, run the describe-container-instances command and pass in the container instance ID that you retrieved earlier:

$ aws ecs describe-container-instances --cluster Your_Cluster_Name --container-instances Your_Container_Instance_ID

Did this article help you?

Anything we could improve?


Need more help?