[英]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: 我正在尝试创建一个EC2实例,我遇到的问题是在我创建这样的实例之后:
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. 这段代码正是我想要的,在正确的区域和AZ中创建实例。 I'm able to capture the name of the new instance ID instanceresult
. 我能够捕获新实例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. 现在,我知道它存在的事实,不仅是instanceresult
告诉我,我可以看到AWS控制台中的新实例启动。
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
. 对于Java SDK,默认区域为us-east-1
。 so, if you are not specifying any region in your code, then you are polling us-east-1. 所以,如果您没有在代码中指定任何区域,那么您正在轮询us-east-1。
So, if your instance is in another region,then you have to pass the region parameter in your code. 因此,如果您的实例位于其他区域,则必须在代码中传递region参数。
From http://docs.aws.amazon.com/AWSSdkDocsJava/latest/DeveloperGuide/java-dg-region-selection.html : 来自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. 如果您未在代码中指定区域,则AWS SDK for Java将使用美国东部(弗吉尼亚北部)区域作为默认区域。
Also, from the same link, Here is a sample on howto set the region in Java: 另外,从同一个链接,下面是如何在Java中设置区域的示例:
For example, to configure the Amazon EC2 client to use the EU (Ireland) Region, use the following code: 例如,要配置Amazon EC2客户端以使用EU(爱尔兰)区域,请使用以下代码:
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. 经过更多的研究和测试,我意识到警告始终在API中 ,我只是错过了它。
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. 我没有使用我正在使用的公共静态ec2客户端对象,而是在线程中多次更改端点,而是将我的一些类切换为使用特定于我所在类的私有ec2客户端,这似乎解决了问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.