How can I get Public IP of my FARGATE ECS task wit

2020-03-21 09:41发布

问题:

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

回答1:

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();