Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create Amazon EC2 Instance with API

Does com.amazonaws.services.ec2 contain a method to create a brand new EC2 instance from an existing AMI? I'm looking to do this from the Java SDK, not the web management console.

like image 667
Dave Avatar asked Feb 14 '11 20:02

Dave


People also ask

Can I use API gateway with EC2?

You can create an API Gateway API with private integration to provide your customers access to HTTP/HTTPS resources within your Amazon Virtual Private Cloud (Amazon VPC). Such VPC resources are HTTP/HTTPS endpoints on an EC2 instance behind a Network Load Balancer in the VPC.

What is Amazon EC2 API?

Amazon Elastic Compute Cloud (Amazon EC2) is a web-based service that allows businesses to run application programs in the Amazon Web Services (AWS) public cloud.

How do we create an Amazon EC2 instance?

Open the Amazon EC2 console at https://console.aws.amazon.com/ec2/ . Choose Launch Instance. In Step 1: Choose an Amazon Machine Image (AMI), find an Amazon Linux 2 AMI at the top of the list and choose Select. In Step 2: Choose an Instance Type, choose Next: Configure Instance Details.


1 Answers

Here is a sample to create EC2 Instances with Amazon AWS SDK for Java :

// CONNECT TO EC2  InputStream credentialsAsStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("AwsCredentials.properties"); Preconditions.checkNotNull(credentialsAsStream, "File 'AwsCredentials.properties' NOT found in the classpath"); AWSCredentials credentials = new PropertiesCredentials(credentialsAsStream);  AmazonEC2 ec2 = new AmazonEC2Client(credentials); ec2.setEndpoint("ec2.eu-west-1.amazonaws.com");  // CREATE EC2 INSTANCES RunInstancesRequest runInstancesRequest = new RunInstancesRequest()     .withInstanceType("t1.micro")     .withImageId("ami-62201116")     .withMinCount(2)     .withMaxCount(2)     .withSecurityGroupIds("tomcat")     .withKeyName("xebia-france")     .withUserData(Base64.encodeBase64String(myUserData.getBytes())) ;  RunInstancesResult runInstances = ec2.runInstances(runInstancesRequest);  // TAG EC2 INSTANCES List<Instance> instances = runInstances.getReservation().getInstances(); int idx = 1; for (Instance instance : instances) {   CreateTagsRequest createTagsRequest = new CreateTagsRequest();   createTagsRequest.withResources(instance.getInstanceId()) //       .withTags(new Tag("Name", "travel-ecommerce-" + idx));   ec2.createTags(createTagsRequest);    idx++; } 

Source code (create RDS, EC2 and ELB instances) is available at http://code.google.com/p/xebia-france/source/browse/training/xebia-spring-travel/trunk/xebia-spring-travel-amazon-aws/src/main/java/fr/xebia/demo/amazon/aws/AmazonAwsInfrastructureMaker.java?spec=svn1781&r=1781

Hope this helps,

Cyrille

like image 67
2 revs, 2 users 72% Avatar answered Sep 21 '22 17:09

2 revs, 2 users 72%