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

Last updated: 2022-04-12

I want to get the ID and IP address of an Amazon Elastic Compute Cloud (Amazon EC2) instance for an AWS Batch job. How do I find that information?

Short description

To get information about an AWS Batch job's Amazon EC2 instance, first get the job's Amazon Elastic Container Service (Amazon ECS) container instance ID. Then, use the container instance ID to get the associated Amazon ECS cluster name and Amazon EC2 instance ID. You can use the Amazon EC2 instance ID to get the instance's IP address.

Note: Amazon ECS resources created after April 1, 2021 use a new long Amazon Resource Name (ARN) format that now includes the ECS cluster name. This article provides instructions for using either the new or the old ARN formats. For more information, see ARNs and IDs in the Amazon ECS Developer Guide.

Resolution

Note: If you receive errors when running AWS Command Line Interface (AWS CLI) commands, make sure that you're using the most recent AWS CLI version.

Get the AWS Batch job's Amazon ECS container instance ID, cluster name, and Amazon EC2 instance ID

For the new ARN format

Get your AWS Batch job's Amazon ECS container instance ID, cluster name, and Amazon EC2 instance ID by running the following bash script:

Important: Replace <your_job_ID> with your AWS Batch job's job ID.

#!/bin/bash
JOB_ID=<your_job_ID>

CONTAINER_INSTANCE_ARN=$(aws batch describe-jobs --jobs "$JOB_ID" --query 'jobs[0].container.containerInstanceArn' --output text);
TMP=${CONTAINER_INSTANCE_ARN#*/}
CLUSTER_NAME=${TMP%/*}

EC2_ID=$(aws ecs describe-container-instances  --container-instances "$CONTAINER_INSTANCE_ARN" --cluster "$CLUSTER_NAME" --query "containerInstances[0].ec2InstanceId" --output text)

echo ${EC2_ID}

For the old ARN format

1.    Get your AWS Batch job's Amazon ECS container instance ID by running the following describe-jobs command:

$ aws batch describe-jobs --jobs <your_job_ID> --query 'jobs[0].container.containerInstanceArn' --output text

The output returns a container instance ARN value that includes the container instance ID.

2.    Find the Amazon ECS cluster that corresponds to your AWS Batch compute environment by doing the following:
Open the Amazon ECS console.
Choose Clusters.
Choose the Services tab.
Then, search for the cluster name that appears in the following format: ComputeEnvironmentName_Batch_RandomStringOfCharacters

3.    Get the cluster's associated Amazon EC2 instance ID by running the following describe-container-instances command:

Important: Replace <Your_Cluster_Name> with your Amazon ECS cluster's name. Replace <Your_Container_Instance_ID> with your AWS Batch job's Amazon ECS container instance ID.

$ aws ecs describe-container-instances --cluster <Your_Cluster_Name> --container-instances <Your_Container_Instance_ID> --query "containerInstances[0].ec2InstanceId"

The output returns the job's associated Amazon EC2 instance ID.

Use the Amazon EC2 instance ID to get the instance's IP address

You can use either Amazon EC2 console or the AWS CLI to get an Amazon EC2 instance's IP address.

Amazon EC2 console

1.    Open the Amazon EC2 console.

2.    Choose Running instances.

3.    In the Instance ID column, find your Amazon EC2 instance's instance ID.

4.    In the same row as your instance ID, look at the IPv4 Public IP address column. The value listed is the Amazon EC2 instance's IP address.

AWS CLI

Run the following describe-instances command:

Important: Replace <EC2_ID> with your Amazon EC2 instance ID.

aws ec2 describe-instances --instance-ids <EC2_ID> --query 'Reservations[0].Instances[0].{"PrivateIP":PrivateIpAddress,"PublicIP":PublicIpAddress}'

To get instance information for multi-node AWS Batch jobs

1.    Get the container instance ID for a child job by running the following describe-jobs command on an individual child job:

Important: Replace Your_Job_ID with your AWS Batch job ID.

$ aws batch describe-jobs --jobs Your_Job_ID#1 --query 'jobs[0].container.containerInstanceArn' --output text

Important: Because each child job is on a unique node, the parent job doesn't have information on the container instance. For child jobs, use node notation to append the child job's index to the job ID. In this example describe-jobs command, #1 accesses the first node of the job. Adding a number #2 accesses the second node, and so on. For more information, see Multi-node parallel jobs.

2.    Get the associated Amazon EC2 instance ID by running the following describe-container-instances command:

Important: Replace Your_Cluster_Name with your Amazon ECS cluster's name. Replace Your_Container_Instance_ID with your AWS Batch job's Amazon ECS container instance ID.

$ aws ecs describe-container-instances --cluster Your_Cluster_Name --container-instances Your_Container_Instance_ID --query "containerInstances[0].ec2InstanceId"

Did this article help?


Do you need billing or technical support?