//snippet-sourcedescription:[CreateInstance.java demonstrates how to create an EC2 instance.]
//snippet-keyword:[SDK for Java 2.0]
//snippet-keyword:[Code Sample]
//snippet-service:[ec2]
//snippet-sourcetype:[full-example]
//snippet-sourcedate:[11/02/2020]
//snippet-sourceauthor:[scmacdon]
/*
 * Copyright 2010-2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License").
 * You may not use this file except in compliance with the License.
 * A copy of the License is located at
 *
 *  http://aws.amazon.com/apache2.0
 *
 * or in the "license" file accompanying this file. This file is distributed
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */
package com.example.ec2;
// snippet-start:[ec2.java2.create_instance.complete]

// snippet-start:[ec2.java2.create_instance.import]
import software.amazon.awssdk.services.ec2.Ec2Client;
import software.amazon.awssdk.services.ec2.model.InstanceType;
import software.amazon.awssdk.services.ec2.model.RunInstancesRequest;
import software.amazon.awssdk.services.ec2.model.RunInstancesResponse;
import software.amazon.awssdk.services.ec2.model.Tag;
import software.amazon.awssdk.services.ec2.model.CreateTagsRequest;
import software.amazon.awssdk.services.ec2.model.Ec2Exception;
// snippet-end:[ec2.java2.create_instance.import]

/**
 * Creates an EC2 instance
 */
public class CreateInstance {
    public static void main(String[] args) {
        final String USAGE =
                "To run this example, supply an instance name and AMI image id\n" +
                        "Both values can be obtained from the AWS Console\n" +
                        "Ex: CreateInstance <instance-name> <ami-image-id>\n";

        if (args.length != 2) {
            System.out.println(USAGE);
            System.exit(1);
        }

        String name = args[0];
        String amiId = args[1];

        // snippet-start:[ec2.java2.create_instance.main]
        Ec2Client ec2 = Ec2Client.create();

        RunInstancesRequest runRequest = RunInstancesRequest.builder()
                .imageId(amiId)
                .instanceType(InstanceType.T1_MICRO)
                .maxCount(1)
                .minCount(1)
                .build();

        RunInstancesResponse response = ec2.runInstances(runRequest);

        String instanceId = response.instances().get(0).instanceId();

        Tag tag = Tag.builder()
                .key("Name")
                .value(name)
                .build();

        CreateTagsRequest tagRequest = CreateTagsRequest.builder()
                .resources(instanceId)
                .tags(tag)
                .build();

        try {
            ec2.createTags(tagRequest);
            System.out.printf(
                    "Successfully started EC2 instance %s based on AMI %s",
                    instanceId, amiId);
       
        } catch (Ec2Exception e) {
            System.err.println(e.getMessage());
            System.exit(1);
        }
        // snippet-end:[ec2.java2.create_instance.main]
        System.out.println("Done!");
    }
}
// snippet-end:[ec2.java2.create_instance.complete]

@@ -68,24 +68,31 @@
 o create an 
+Amazon 
 EC2 instance
@@ -185,18 +185,25 @@
 ervice:%5B
-ec
+Amazon EC
 2%5D%0D%0A//sn
@@ -261,13 +261,12 @@
 te:%5B
-11/02
+2/15
 /202
@@ -304,16 +304,18 @@
 acdon%5D%0D%0A
+%0D%0A
 /*%0D%0A * C
@@ -327,18 +327,8 @@
 ght 
-2010-2020 
 Amaz
@@ -924,63 +924,8 @@
 c2;%0D
-%0A// snippet-start:%5Bec2.java2.create_instance.complete%5D%0D
 %0A%0D%0A/
@@ -976,16 +976,63 @@
 mport%5D%0D%0A
+import software.amazon.awssdk.regions.Region;%0D%0A
 import s
@@ -2112,102 +2112,428 @@
     
-// snippet-start:%5Bec2.java2.create_instance.main%5D%0D%0A        Ec2Client ec2 = Ec2Client.create();
+Region region = Region.US_WEST_2;%0D%0A        Ec2Client ec2 = Ec2Client.builder()%0D%0A                .region(region)%0D%0A                .build();%0D%0A%0D%0A        String instanceId = createEC2Instance(ec2,name, amiId) ;%0D%0A        System.out.println(%22The instance ID is %22+instanceId);%0D%0A    %7D%0D%0A%0D%0A    // snippet-start:%5Bec2.java2.create_instance.main%5D%0D%0A   public static String createEC2Instance(Ec2Client ec2,String name, String amiId ) %7B
 %0D%0A%0D%0A
@@ -2847,26 +2847,24 @@
 nRequest);%0D%0A
-%0D%0A
         Stri
@@ -3417,23 +3417,48 @@
 miId);%0D%0A
+%0D%0A
        
+   return instanceId;%0D%0A
 %0D%0A      
@@ -3520,19 +3520,39 @@
 intln(e.
-get
+awsErrorDetails().error
 Message(
@@ -3665,97 +3665,26 @@
     
-System.out.println(%22Done!%22);%0D%0A    %7D%0D%0A%7D%0D%0A// snippet-end:%5Bec2.java2.create_instance.complete%5D
+return %22%22;%0D%0A    %7D%0D%0A%7D
 %0D%0A

Update CreateInstance.java