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?
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.
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.
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.
You will need to make multiple AWS API calls to get Public IPv4 address. Here are the steps.
Example -
AmazonECS client = AmazonECSClientBuilder.standard().build();
DescribeTasksRequest request = new DescribeTasksRequest().withTasks("c5cba4eb-5dad-405e-96db-71ef8eefe6a8");
DescribeTasksResult response = client.describeTasks(request);
"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" } ] } ],
Example -
AmazonEC2 client = AmazonEC2ClientBuilder.standard().build();
DescribeNetworkInterfacesRequest request = new DescribeNetworkInterfacesRequest().withNetworkInterfaceIds("eni-e5aa89a3");
DescribeNetworkInterfacesResult response = client.describeNetworkInterfaces(request);
{ "NetworkInterfaces": [ { "Association": { "IpOwnerId": "amazon", "PublicDnsName": "ec2-52-xx-xx-xx.compute-1.amazonaws.com", "PublicIp": "52.xx.xx.xx" } ] }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With