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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With