Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS JAVA SDK get public IP of Task

I have a small problem. I am starting a Task (docker container) via the JAVA SDK. This is working great.

But now I want to grab the public IP via the SDK and don't know how.

here is my existing code so far.

RunTaskRequest request = new RunTaskRequest()
                .withCluster("JuiceShop")
                .withTaskDefinition("startJuiceShop:1")
                .withNetworkConfiguration(networkConfiguration)
                .withLaunchType("FARGATE");
RunTaskResult response = client.runTask(request);

The response contains the Container but the network devices aren't attached yet. Is there a simple way to get the public IPV4?

like image 541
splitiii Avatar asked Oct 23 '18 09:10

splitiii


People also ask

How can I know my ECS public IP?

Based on the existing ECS apis, there is no direct API to get the IP of the instance where the task started. You will need to use describeContainerInstances API of ecs to get the physical id of the instance and then call ec2 APIs to get the IP of the instance where the task was started.

How do I find the IP address of an EC2 instance in Java?

This may be achieved with AWS Java SDK. And this is how you do it: // Getting instance Id String instanceId = EC2MetadataUtils. getInstanceId(); // Getting EC2 private IP String privateIP = EC2MetadataUtils.

What is the difference between elastic IP and public IP?

An Elastic IP address is a public IPv4 address, which is reachable from the internet. If your instance does not have a public IPv4 address, you can associate an Elastic IP address with your instance to enable communication with the internet.


1 Answers

You will need to make multiple AWS API calls to get Public IPv4 address. Here are the steps.

  1. Once you perform taskRun operation. Keep taskFullArn from Output.
  2. With above taskArn and cluster name, make describeTasks operation call. https://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/ecs/AmazonECS.html#describeTasks-com.amazonaws.services.ecs.model.DescribeTasksRequest-.

Example -

AmazonECS client = AmazonECSClientBuilder.standard().build();
DescribeTasksRequest request = new DescribeTasksRequest().withTasks("c5cba4eb-5dad-405e-96db-71ef8eefe6a8");
DescribeTasksResult response = client.describeTasks(request);
  1. Above API will give you a response with network attachment details.

"attachments": [ { "id": "xxxxx-d02c-4a9d-ae79-xxxxxxx", "type": "ElasticNetworkInterface", "status": "ATTACHED", "details": [ { "name": "subnetId", "value": "subnet-xxxxx" }, { "name": "networkInterfaceId", "value": "eni-e5aa89a3" }, { "name": "macAddress", "value": "xxxxx" }, { "name": "privateIPv4Address", "value": "172.31.94.215" } ] } ],

  1. Take networkInterfaceId from above API response and make following call.
  2. Call AWS EC2 describeNetworkInterfaces. https://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/ec2/AmazonEC2Client.html#describeNetworkInterfaces-com.amazonaws.services.ec2.model.DescribeNetworkInterfacesRequest-

Example -

AmazonEC2 client = AmazonEC2ClientBuilder.standard().build();
DescribeNetworkInterfacesRequest request = new DescribeNetworkInterfacesRequest().withNetworkInterfaceIds("eni-e5aa89a3");
DescribeNetworkInterfacesResult response = client.describeNetworkInterfaces(request);
  1. Above should give DescriberNetworkInterfaceResult with PublicIp of container. https://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/ec2/model/NetworkInterfaceAssociation.html#getPublicIp--

{ "NetworkInterfaces": [ { "Association": { "IpOwnerId": "amazon", "PublicDnsName": "ec2-52-xx-xx-xx.compute-1.amazonaws.com", "PublicIp": "52.xx.xx.xx" } ] }

  1. Note - You will need to perform Step-2 untill task is up and running, otherwise you will not get desired result. So probably, sleep for couple of seconds after runTask and see if Task is up and running then perform remaining steps.
like image 63
Imran Avatar answered Oct 19 '22 00:10

Imran