1

I am trying to programmatically list my ec2 instances via the aws java api.

public static void main(String[] args) {
    AWSCredentials awsCredentials = new BasicAWSCredentials(awsAccessKey, awsSecretKey);
    AmazonEC2AsyncClient amazonEC2AsyncClient = new AmazonEC2AsyncClient(awsCredentials);
    DescribeAvailabilityZonesRequest describeAvailabilityZonesRequest = new DescribeAvailabilityZonesRequest();
    Future<DescribeAvailabilityZonesResult> describeAvailabilityZonesResultFuture = amazonEC2AsyncClient.describeAvailabilityZonesAsync(describeAvailabilityZonesRequest);
    try {
        DescribeAvailabilityZonesResult describeAvailabilityZonesResult = describeAvailabilityZonesResultFuture.get();
        System.out.println(describeAvailabilityZonesResult);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

This prints something like:

{AvailabilityZones: [
    {ZoneName: us-east-1a,State: available,RegionName: us-east-1,Messages: []}, 
    {ZoneName: us-east-1b,State: available,RegionName: us-east-1,Messages: []}, 
    {ZoneName: us-east-1c,State: available,RegionName: us-east-1,Messages: []}]}

However, I know I have instance in us-west-2. When I add that to my api query via

describeAvailabilityZonesRequest.withZoneNames("us-west-2");

I get this error

java.util.concurrent.ExecutionException: com.amazonaws.AmazonServiceException: Status Code: 400, AWS Service: AmazonEC2, AWS Request ID: 38956486-2686-4466-b9e0-45ef12395676, AWS Error Code: InvalidParameterValue, 
AWS Error Message: Invalid availability zone: [us-west-2]
    at java.util.concurrent.FutureTask.report(FutureTask.java:122)
    at java.util.concurrent.FutureTask.get(FutureTask.java:188)
    at com.pledgeling.platform.system.SystemAdmin.main(SystemAdmin.java:25)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Caused by: com.amazonaws.AmazonServiceException: Status Code: 400, AWS Service: AmazonEC2, AWS Request ID: 38956486-2686-4466-b9e0-45ef12395676, AWS Error Code: InvalidParameterValue, AWS Error Message: Invalid availability zone: [us-west-2]
    at com.amazonaws.http.AmazonHttpClient.handleErrorResponse(AmazonHttpClient.java:767)
    at com.amazonaws.http.AmazonHttpClient.executeHelper(AmazonHttpClient.java:414)
    at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:228)
    at com.amazonaws.services.ec2.AmazonEC2Client.invoke(AmazonEC2Client.java:7918)

What should I do to fix this? Why are my us-west instances not showing up?

1 Answer 1

5

us-west-2 is not an availability zone, it's a region. us-west-2a is an availability zone. It looks like you're getting the error because, as your code is currently written, you're asking us-east-1 about an availability zone that doesn't exist there. The fix, though, is not just a matter of putting the final letter designator in the string.

EC2 is deliberately decentralized so that each region is independent of the others for survivability, and as such, each region has its own API endpoint. Your requests have to be sent to the appropriate endpoint for the region you want.

I'm not a Java person, so I'm not sure if the following is precisely the information you need, but it should at least get you pointed in the right direction.

Each AWS client can be configured to use a specific endpoint by calling the setEndpoint(String endpointUrl) method.

http://docs.aws.amazon.com/AWSSdkDocsJava/latest/DeveloperGuide/java-dg-region-selection.html

The link above also explains why you're currently talking to us-east-1 without actually specifying an endpoint.

The AWS SDK for Java uses the US East (Northern Virginia) Region as the default region if you do not specify a region in your code.

Sign up to request clarification or add additional context in comments.

3 Comments

Michael, thanks you very much for the answer and the link. Perfectly clear and right on.
I did this but still it always shows only us-east-1
EndpointConfiguration endpointConfiguration = new EndpointConfiguration("ec2.us-west-2.amazonaws.com","us-west-2"); ec2 = AmazonEC2ClientBuilder.standard() .withEndpointConfiguration(endpointConfiguration) .build();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.