I am trying to retrieve the private IP from my containers on AWS ECS, I am using python and boto3 for that work.
That is my code:
import json
import boto3
def lambda_handler(event, context):
client = boto3.client("ecs", region_name="sa-east-1")
clusterArn='ANY ARN'
tasks = client.list_tasks(cluster=clusterArn)
containers = []
for each in tasks['taskArns']:
containers.append(each)
resp = client.describe_tasks(cluster=clusterArn,tasks=containers)
print(json.dumps(resp, indent=4, default=str, sort_keys=True))
The describe tasks return for me the following IP from container:
{
"bindIP": "0.0.0.0",
"containerPort": 8080,
"hostPort": 8080,
"protocol": "tcp"
}
I alredy tried to use ecs describe task on aws cli, but its the same response. Is there another way to retrieve the private ip using boto3?
Thanks,
2
Answers
With ECS on EC2, your task will have the IP of your EC2 instance and will then bind with an available port.
You can get the port with the ECS API, but you’ll need the EC2 API to get the IP if there’s no
networkInterfaces
attached yo your containers.Here’s an example with the cli to get both IP and ports for a task:
In AWS IP addresses are provided for Network Interfaces, which will be attached to the running containers. So you would want to fetch the network interface section of a container.
For example:
It should output something like this: