Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)?

like image 747
tarikfasun Avatar asked May 07 '18 13:05

tarikfasun


1 Answers

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();
like image 172
tarikfasun Avatar answered Sep 29 '22 00:09

tarikfasun