Skip to content

How to get Fargate task IP address (boto3/Python)

0

**EDIT: **When exporting correct AWS username access keys in used docker image, boto3 started working. However I could not get in working without exporting access keys

I need to get my Fargate task IP address in order to connect Redis running in the task. If I just use public IP (hardcode) , its all fine, but when I try get get address using boto3 /Python/Flask server) , will get "no cluster found error message" for task log. Cluster does exists in the region I am using and I can check cluster/service/my task data fine via console UI /ASW cli. Here is code snippet used:


    ecs = boto3.client("ecs", region_name="eu-north-1")
    cluster_name = "my-cluster-name"
    service_name = "my-redis-service-name"

    tasks = ecs.list_tasks(cluster=cluster_name, serviceName=service_name)['taskArns']
    if not tasks:
        return None

    task_desc = ecs.describe_tasks(cluster=cluster_name, tasks=tasks)['tasks']
    for task in task_desc:
        for attachment in task.get("attachments", []):
            for detail in attachment.get("details", []):
                if detail["name"] == "privateIPv4Address":
                    return detail["value"] 

    return None
1 Answer
0

Hello.

**EDIT: **When exporting correct AWS username access keys in used docker image, boto3 started working. However I could not get in working without exporting access keys

If you are running it on Fargate, I think it will work without setting an access key if you set an IAM policy on the ECS task roles.
Please refer to the following document to set it up.
https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-iam-roles.html

By the way, if containers within the same task are communicating with each other, you can connect using localhost and port number as described in the document below.
https://docs.aws.amazon.com/AmazonECS/latest/developerguide/fargate-task-networking.html

By default, every Amazon ECS task on Fargate is provided an elastic network interface (ENI) with a primary private IP address. When using a public subnet, you can optionally assign a public IP address to the task's ENI. If your VPC is configured for dual-stack mode and you use a subnet with an IPv6 CIDR block, your task's ENI also receives an IPv6 address. A task can only have one ENI that's associated with it at a time. Containers that belong to the same task can also communicate over the localhost interface. For more information about VPCs and subnets, see How Amazon VPC works in the Amazon VPC User Guide.

EXPERT

answered 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.