11

I want to create EC2 Instance using this Java code remotely:

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";
        }
    }

But I get this error code:

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)

Can you propose me some solution how to fix this code or there is a alternative?

Can you recommend me some working solution which I can use?

Peter Penzov
  • 1,126
  • 134
  • 430
  • 808

3 Answers3

12

The AMI ami-fd9cecc7 exists in the Sydney (ap-southeast-2) region.

When you are executing your code, make sure that you are running it in the Sydney (ap-southeast-2) region. By default, it may run in Virginia (us-east-1). You may be able to specify the region by a code change or by a configuration change.

If you want your code to execute in Virginia (or any region other than Sydney), then you need to find a different AMI from that region to use as the base image for your EC2 instance.

Matt Houser
  • 33,983
  • 6
  • 70
  • 88
2

You need to set Region while creating AmazonEC2Client.

Example:

Region usWest2 = Region.getRegion(Regions.US_WEST_2);
ec2.setRegion(usWest2);
techiepark
  • 343
  • 2
  • 7
  • 23
0

I had everything configured correctly but failed to realise the AMI I created 10 minutes ago was still in 'Pending' status. Go to AMI dashboard and check:

enter image description here

If the AMI you're trying to use is 'Pending', try again when it's Available.

FYI it took about 12 minutes to become available. But it may take longer if the AMI is large or old.

I attempted the exact same configs immediately after it became 'Available' and it worked immediately:

enter image description here

stevec
  • 41,291
  • 27
  • 223
  • 311