简体   繁体   中英

AWS Java SDK EC2 Describe Instance throws error

Here I am trying to list the available instances. But when I run the code I am getting error message as "Unable to find a region via the region provider chain. Must provide an explicit region in the builder or setup environment to supply a region."

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.ec2.AmazonEC2;
import com.amazonaws.services.ec2.AmazonEC2ClientBuilder;
import com.amazonaws.services.ec2.model.DescribeInstancesRequest;
import com.amazonaws.services.ec2.model.DescribeInstancesResult;
import com.amazonaws.services.ec2.model.Instance;
import com.amazonaws.services.ec2.model.Reservation;
@Controller
public class EC2InstancesController {

@RequestMapping(value="/getAllInstances", produces = {"application/json"}, 
            consumes = {"application/json"}, method = RequestMethod.GET)
    public List<Instance> getAllInstances(){

        final AmazonEC2 ec2 = AmazonEC2ClientBuilder.defaultClient();

        boolean done = false;

        while(!done) {
            DescribeInstancesRequest request = new DescribeInstancesRequest();

            DescribeInstancesResult response = ec2.describeInstances(request);

            for(Reservation reservation : response.getReservations()) {
                for(Instance instance : reservation.getInstances()) {
                    System.out.printf(
                        "Found reservation with id %s, " +
                        "AMI %s, " +
                        "type %s, " +
                        "state %s " +
                        "and monitoring state %s",
                        instance.getInstanceId(),
                        instance.getImageId(),
                        instance.getInstanceType(),
                        instance.getState().getName(),
                        instance.getMonitoring().getState());
                }
            }
            request.setNextToken(response.getNextToken());

            if(response.getNextToken() == null) {
                done = true;
            }
        }
        return null;
    }
}

When I try to hit this endpoint from POSTMAN I am getting the following error

{
    "timestamp": 1498107927641,
    "status": 500,
    "error": "Internal Server Error",
    "exception": "com.amazonaws.SdkClientException",
    "message": "Unable to find a region via the region provider chain. Must provide an explicit region in the builder or setup environment to supply a region.",
    "path": "/getAllInstances"
}

I also added the following code in the above, but still getting same error message from POSTMAN rest client.

Region region = Region.getRegion(Regions.US_EAST_1);
        ec2.setRegion(region);

Can someone help me out in this. Thanks..

Which Version of JAVA AWS SDK?

From Aws SDK 1.11.113 setRegion is depreceated.

https://aws.amazon.com/blogs/developer/client-constructors-now-deprecated/

So you need to set region in AWS Credentials or you need to get region using Method getCurrentRegion()

ref: http://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/regions/Regions.html

它通过将默认的“客户端”构建器更改为标准构建器而为我工作,

final AmazonEC2 ec2 = AmazonEC2ClientBuilder.standard().withRegion(Regions.AP_SOUTH_1).build();

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