简体   繁体   中英

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?

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.

The task metadata endpoint will let you fetch network information about your container. If you're attaching an EIP you'll have to use the IPv4 from the container to find the EIP with the SDK.

Looks like there's a metadata service v3 now!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM