tarikfasun
tarikfasun

Reputation: 324

How can I get Public IP of my FARGATE ECS task with metadata endpoint or java sdk?

I want to get public IP of my fargate ECS task after container started. I get IPv4Addresses when requested http://169.254.170.2/v2/metadata/ but I don't get public IP of task.

When I use runTask(request) method of RunTaskResult, again I don't get public IP from RunTaskResult.

Is there a way to get public IP of my fargate task something like http://169.254.169.254/latest/meta-data/public-hostname (for EC2)?

Upvotes: 9

Views: 5991

Answers (1)

tarikfasun
tarikfasun

Reputation: 324

I solved my problem with DescribeNetworkInterfacesRequest.I get private IP of my container with curl http://169.254.170.2/v2/metadata/ then run this code.

    AmazonEC2 ec2AsyncClient= AmazonEC2ClientBuilder.standard().withCredentials(new 
    AWSStaticCredentialsProvider(credentials)).build();

    DescribeNetworkInterfacesRequest request = new DescribeNetworkInterfacesRequest();
    request.withFilters(new Filter().withName("private-ip-address").withValues("my-container-private-Ip"));
    DescribeNetworkInterfacesResult result = ec2AsyncClient.describeNetworkInterfaces(request);
    String publicIP = result.getNetworkInterfaces().get(0).getAssociation().getPublicDnsName();

Upvotes: 5

Related Questions