图片ID' [ami-fd9cecc7]'不存在

时间:2015-12-13 12:25:12

标签: java amazon-web-services amazon-ec2

我想远程使用这个Java代码创建EC2实例:

public void testEC2ServiceInRegion() throws Exception
    {

        String launchInstance = launchInstance();
        System.out.println("Status " + launchInstance);
    }

    public String launchInstance()
    {
        BasicAWSCredentials bawsc = new BasicAWSCredentials(
            "AKIAIUY1KF4KZV3DAL21", "Onv+nq33tUkiLl1Ib2H9JtIB732QMEesh01Jl73L");

        AmazonEC2 ec2 = new AmazonEC2Client(bawsc);
        System.out.println("\n\nLAUNCH INSTANCE\n\n");

        try
        {
            // Construct a RunInstancesRequest.
            RunInstancesRequest request = new RunInstancesRequest();
            request.setImageId("ami-fd9cecc7"); // the AMI ID, ami-fd9cecc7 is Amazon Linux AMI 2015.03 (HVM)
            request.setInstanceType("t2.micro");    // instance type
            request.setKeyName("desktop");      // the keypair
//          request.setSubnetId("subnet-2dc0d459"); // the subnet
//          ArrayList list = new ArrayList();
//          list.add("sg-efcc248a");            // security group, call add() again to add more than one
//          request.setSecurityGroupIds(list);
            request.setMinCount(1); // minimum number of instances to be launched
            request.setMaxCount(1); // maximum number of instances to be launched

            // Pass the RunInstancesRequest to EC2.
            RunInstancesResult  result  = ec2.runInstances(request);
            String instanceId = result.getReservation().getInstances().get(0).getInstanceId();

            // Return the first instance id in this reservation.
            // So, don't launch multiple instances with this demo code.
            System.out.println("Launching instance " + instanceId);
            return instanceId;
        } catch (Exception e)
        {
            // Simple exception handling by printing out error message and stack trace
            System.out.println(e.getMessage());
            e.printStackTrace();
            return "ERROR";
        }
    }

但是我得到了这个错误代码:

The image id '[ami-fd9cecc7]' does not exist (Service: AmazonEC2; Status Code: 400; Error Code: InvalidAMIID.NotFound; Request ID: f85433c1-df4f-4105-bfe3-6f900eca6b70)
com.amazonaws.AmazonServiceException: The image id '[ami-fd9cecc7]' does not exist (Service: AmazonEC2; Status Code: 400; Error Code: InvalidAMIID.NotFound; Request ID: f85433c1-df4f-4105-bfe3-6f900eca6b70)
    at com.amazonaws.http.AmazonHttpClient.handleErrorResponse(AmazonHttpClient.java:1275)
    at com.amazonaws.http.AmazonHttpClient.executeOneRequest(AmazonHttpClient.java:873)
    at com.amazonaws.http.AmazonHttpClient.executeHelper(AmazonHttpClient.java:576)
    at com.amazonaws.http.AmazonHttpClient.doExecute(AmazonHttpClient.java:362)

你能给我一些解决方法如何解决这个代码,还是有另一种选择?

你能推荐一些我可以使用的工作解决方案吗?

2 个答案:

答案 0 :(得分:3)

AMI ami-fd9cecc7位于悉尼(东南2区)。

执行代码时,请确保在悉尼(ap-southeast-2)区域运行代码。默认情况下,它可能在Virginia(us-east-1)中运行。您可以通过代码更改或配置更改来指定区域。

如果您希望代码在Virginia(或悉尼以外的任何区域)执行,则需要从该区域中找到不同的AMI,以用作EC2实例的基本映像。

答案 1 :(得分:1)

您需要在创建AmazonEC2Client时设置Region。

示例:

Region usWest2 = Region.getRegion(Regions.US_WEST_2);
ec2.setRegion(usWest2);