简体   繁体   中英

Instance ID doesn't exist

I'm attempting to create an EC2 instance, the problem I run into is after I create the instance like this:

SOURCE_REGION = Region.getRegion(Regions.US_WEST_2);
ec2.setRegion(SOURCE_REGION);
ec2.setEndpoint("ec2.us-west-1.amazonaws.com");

RunInstancesRequest instancereq = new RunInstancesRequest();
instancereq.setInstanceType(instancearray.get(i).getInstanceType());
instancereq.setImageId(defaultAmi);
instancereq.setMinCount(1);
instancereq.setMaxCount(1);

ArrayList<String> groupid = new ArrayList<String>();  
groupid.add(secgroup.get(0).getGroupName());        

instancereq.setSecurityGroupIds(groupid);
instancereq.setKeyName("testkey");  

Placement place = new Placement();
place.setAvailabilityZone((String) target.getValueAt(row, column));   

instancereq.setPlacement("ec2.us-west-1.amazonaws.com");

RunInstancesResult instanceresult = ec2.runInstances(instancereq);

This code does exactly what I want, creates the instance in the correct region and AZ. I'm able to capture the name of the new instance ID instanceresult . However, then I execute the next lines in the same method (those tag variables contain strings, didn't include that) I get an error:

CreateTagsRequest createTagsRequest = new CreateTagsRequest();
createTagsRequest.withResources(newinstance.getInstanceId())
              .withTags(new Tag("Name", tagName)).withTags(new Tag("Application", tagApplication))
              .withTags(new Tag("Env", tagEnv)).withTags(new Tag("Function", tagFunction));

ec2.createTags(createTagsRequest);

The error I get is this:

Error Code: InvalidInstanceID.NotFound, AWS Error Message: The instance ID 'i-xxxxxxxx' does not exist

Now, I know for a fact it exists, not only did instanceresult tell me that, I can see the new instance in the AWS Console starting up.

I have no problems if I'm working with the default region, but I'm not doing something right when it comes to regions and endpoints. Any advice/guidance would be wonderful.

For Java SDK, the Default region is us-east-1 . so, if you are not specifying any region in your code, then you are polling us-east-1.

So, if your instance is in another region,then you have to pass the region parameter in your code.

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

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.

Also, from the same link, Here is a sample on howto set the region in Java:

For example, to configure the Amazon EC2 client to use the EU (Ireland) Region, use the following code:

AmazonEC2 ec2 = new AmazonEC2(myCredentials);
ec2.setEndpoint("https://ec2.eu-west-1.amazonaws.com");

After more research and testing, I realized the warning was in the API all along and I was just missing it.

This method is not threadsafe. Endpoints should be configured when the client is created and before any service requests are made. Changing it afterwards creates inevitable race conditions for any service requests in transit.

Instead of using a public static ec2 client object, which I was using and changing endpoints aa few times across threads, I switched a few of my classes to use a private ec2 client specific to the class I'm in and that seemed to fix the issue.

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