0

I have created a cluster, resource, service inside a stack using Java AWS SDK. Now I need to fetch the Public IP of the task inside the cluster.

I have used ClientFormationCloud and am trying to fetch the IP using this client itself.

I have used the following code as of now:

    String awsRegion = System.getenv("AWS_DEFAULT_REGION");
    Region region = Region.of(awsRegion);
    
    CloudFormationClient cfClient = CloudFormationClient.builder().region(region)
            .credentialsProvider(EnvironmentVariableCredentialsProvider.create()).build();

    CloudFormationWaiter waiter = cfClient.waiter();
    Collection<Capability> capabilities = new ArrayList<Capability>();
    capabilities.add(Capability.CAPABILITY_NAMED_IAM);

    CreateStackRequest stackRequest = CreateStackRequest.builder().stackName(stackName).templateURL(location).capabilities(capabilities).roleARN(roleARN).onFailure(OnFailure.ROLLBACK).build();

    CreateStackResponse cfResponse = cfClient.createStack(stackRequest);

    DescribeStacksRequest stacksRequest = DescribeStacksRequest.builder().stackName(stackName).build();
    DescribeStacksResponse response = cfClient.describeStacks();
    WaiterResponse<DescribeStacksResponse> waiterResponse = waiter.waitUntilStackCreateComplete(stacksRequest);
    waiterResponse.matched().response().ifPresent(System.out::println);

'''

EDITED

Tried the following for ECS according to the first comment

'''

AmazonECS ecsClient = AmazonECSClientBuilder.standard().withRegion(System.getenv("AWS_DEFAULT_REGION")).withCredentials(new com.amazonaws.auth.EnvironmentVariableCredentialsProvider()).build();
ListTasksRequest request = new ListTasksRequest().withCluster("ClusterName");
ListTasksResult tasks = ecsClient.listTasks(request);
List<String> taskArns = tasks.getTaskArns();
        for (String taskArn : taskArns) {
            System.out.println(taskArn);
            DescribeTasksRequest dtr = new DescribeTasksRequest().withTasks(taskArn);
            DescribeTasksResult response = ecsClient.describeTasks(dtr);
        }

'''

The taskArn is getting printed: arn:aws:ecs:us-east-1:892372858130:task/ClusterName/2c72e43671ef4cc09f7816469696ee3e

But the last line of the code is giving the following error:

com.amazonaws.services.ecs.model.InvalidParameterException: Invalid identifier: Identifier is for cluster NaraCluster. Your cluster is default (Service: AmazonECS; Status Code: 400; Error Code: InvalidParameterException; Request ID: 45f8abfc-66d4-4808-8810-b6e314bb1b15; Proxy: null)

  • You need to describe the ECS service to get a list of tasks, then you need to describe the task to get the ENI, then you need to describe the ENI to get the IP. It is a long winded process. The last 2/3 are covered here: https://stackoverflow.com/questions/52946160/aws-java-sdk-get-public-ip-of-task – luk2302 Nov 15 '22 at 10:45
  • Edited the question, pls check @luk2302 – theAccidentalDeveloper Nov 16 '22 at 07:33

0 Answers0