28

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.

1

3 Answers 3

69

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

Sign up to request clarification or add additional context in comments.

Comments

11

RunInstances is the method, it should be in the SDK.

2 Comments

Thank you so much. I'm also need an ability to distinguish instances I start from all of my other ones. Do you know of a good way to do this?
You have a few options. Easiest way is to add a tag to your instances created in code (you're limited to 10 total tags per instance, though) with CreateTags; these are then returned from DescribeInstances along with your instances (and also viewable in the web console)
0
 var launchRequest = new RunInstancesRequest()
                {
                    ImageId = amiID,
                    InstanceType = ConfigurationManager.AppSettings["AwsInstanceType"],
                    MinCount = 1,
                    MaxCount = 1,
                    KeyName = keyPairName,
                    SecurityGroupIds = groups,
                    SubnetId = ConfigurationManager.AppSettings["AwsSubnetId"],

                };
                RunInstancesResponse runInstancesResponse = amazonEc2client.RunInstances(launchRequest);
                var InstanceId = runInstancesResponse.Reservation.Instances[0].InstanceId;
                var trequest = new CreateTagsRequest();
                trequest.Resources=new List<string>(){InstanceId};
                List<Tag> tags=new List<Tag>();
                Tag tag=new Tag("Name","TestCodeFinal");
                tags.Add(tag);
                trequest.Tags = tags;
                amazonEc2client.CreateTags(trequest);
                Reservation reservation = runInstancesResponse.Reservation;

1 Comment

the above is C# and not Java.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.