This simple bash script can be used to get the public and private IP addresses of the Tasks within a Fargate Cluster. With the CLUSTER_NAME
defined as an environment variable, this script will iterate through all of the Tasks in a Cluster and create a .env
file with both the public and private addresses of the Tasks.
#!/bin/bash > taskIps.env TASK_LIST=$(aws ecs list-tasks --cluster $CLUSTER_NAME) TASK_ARNS=$(echo "$TASK_LIST" | jq -r '.taskArns[]') while IFS= read -r TASK_ARN; do NETWORK_INTERFACE_ID=$(aws ecs describe-tasks --cluster $CLUSTER_NAME --tasks "$TASK_ARN" --query 'tasks[0].attachments[0].details[?name==`networkInterfaceId`].value' --output text) PRIVATE_IP=$(aws ecs describe-tasks --cluster $CLUSTER_NAME --tasks "$TASK_ARN" --query 'tasks[0].attachments[0].details[?name==`privateIPv4Address`].value' --output text) PUBLIC_IP=$(aws ec2 describe-network-interfaces --network-interface-ids "$NETWORK_INTERFACE_ID" --query 'NetworkInterfaces[*].Association.PublicIp' --output text) TASK_NAME=$(aws ecs describe-tasks --cluster $CLUSTER_NAME --tasks "$TASK_ARN" --query 'tasks[0].containers[0].name' --output text | tr '[:lower:]' '[:upper:]') echo "${TASK_NAME}_PUBLIC_IP=$PUBLIC_IP" >> taskIps.env echo "${TASK_NAME}_PRIVATE_IP=$PRIVATE_IP" >> taskIps.env done <<< "$TASK_ARNS"
The basic steps involved:
- Get the Tasks in the Cluster
- Get the ARNs of the Tasks
- Loop through the Task ARNs
- Get the Network Interface ID of the Task using the Task ARN and Cluster Name
- Get the Private IP using the Task ARN and Cluster Name
- Get the Public IP using the Network Interface ID
- Get the Task Name from the Task ARN and Cluster Name
- Write everything to the the
.env
file
GitHub Gist
https://gist.github.com/schuettc/5e805ec2ca6c21f9b3af3f9cfa75fade