//snippet-sourcedescription:[AllocateAddress.java demonstrates how to allocate an elastic IP address for an EC2 instance.]
//snippet-keyword:[SDK for Java 2.0]
//snippet-keyword:[Code Sample]
//snippet-service:[ec2]
//snippet-sourcetype:[full-example]
//snippet-sourcedate:[2020/02/11]
//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.allocate_address.complete]

// snippet-start:[ec2.java2.allocate_address.import]
import software.amazon.awssdk.services.ec2.Ec2Client;
import software.amazon.awssdk.services.ec2.model.AllocateAddressRequest;
import software.amazon.awssdk.services.ec2.model.DomainType;
import software.amazon.awssdk.services.ec2.model.AllocateAddressResponse;
import software.amazon.awssdk.services.ec2.model.AssociateAddressRequest;
import software.amazon.awssdk.services.ec2.model.AssociateAddressResponse;
import software.amazon.awssdk.services.ec2.model.Ec2Exception;
// snippet-end:[ec2.java2.allocate_address.import]

/**
 * Allocates an elastic IP address for an EC2 instance
 */
public class AllocateAddress {

    public static void main(String[] args) {
        final String USAGE =
                "To run this example, supply an instance id that you can obtain from the AWS Console\n" +
                        "Ex: AllocateAddress <instance_id>\n";

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

        String instanceId = args[0];
        // snippet-start:[ec2.java2.allocate_address.main]
        Ec2Client ec2 = Ec2Client.create();

       try {
           AllocateAddressRequest allocateRequest = AllocateAddressRequest.builder()
                .domain(DomainType.VPC)
                .build();

           AllocateAddressResponse allocateResponse =
                ec2.allocateAddress(allocateRequest);

           String allocationId = allocateResponse.allocationId();

           AssociateAddressRequest associateRequest =
                AssociateAddressRequest.builder()
                        .instanceId(instanceId)
                        .allocationId(allocationId)
                        .build();

            AssociateAddressResponse associateResponse =
                ec2.associateAddress(associateRequest);

        // snippet-end:[ec2.java2.allocate_address.main]
            System.out.printf(
                "Successfully associated Elastic IP address %s " +
                        "with instance %s",
                associateResponse.associationId(),
                instanceId);
         } catch (Ec2Exception e) {
           e.getStackTrace();
        }
    }
}

// snippet-end:[ec2.java2.allocate_address.complete]

@@ -93,32 +93,39 @@
  address for an 
+Amazon 
 EC2 instance.%5D%0D%0A
@@ -214,18 +214,25 @@
 ervice:%5B
-ec
+Amazon EC
 2%5D%0D%0A//sn
@@ -291,17 +291,16 @@
 e:%5B2
-020/02/11
+/11/2020
 %5D%0D%0A/
@@ -354,18 +354,8 @@
 ght 
-2010-2020 
 Amaz
@@ -951,64 +951,8 @@
 c2;%0D
-%0A// snippet-start:%5Bec2.java2.allocate_address.complete%5D%0D
 %0A%0D%0A/
@@ -1004,16 +1004,63 @@
 mport%5D%0D%0A
+import software.amazon.awssdk.regions.Region;%0D%0A
 import s
@@ -2088,24 +2088,26 @@
 gs%5B0%5D;%0D%0A
+%0D%0A
         
 // snipp
@@ -2102,103 +2102,361 @@
     
-// snippet-start:%5Bec2.java2.allocate_address.main%5D%0D%0A        Ec2Client ec2 = Ec2Client.create();
+Region region = Region.US_EAST_1;%0D%0A        Ec2Client ec2 = Ec2Client.builder()%0D%0A                .region(region)%0D%0A                .build();%0D%0A%0D%0A        System.out.println(getAllocateAddress(ec2, instanceId));%0D%0A    %7D%0D%0A%0D%0A    // snippet-start:%5Bec2.java2.allocate_address.main%5D%0D%0A    public static String getAllocateAddress( Ec2Client ec2, String instanceId) %7B
 %0D%0A%0D%0A
@@ -3107,33 +3107,16 @@
 sponse =
-%0D%0A               
  ec2.ass
@@ -3149,26 +3149,24 @@
 uest);%0D%0A
-%0D%0A
         
 // snipp
@@ -3161,218 +3161,18 @@
     
-// snippet-end:%5Bec2.java2.allocate_address.main%5D%0D%0A            System.out.printf(%0D%0A                %22Successfully associated Elastic IP address %25s %22 +%0D%0A                        %22with instance %25s%22,%0D%0A               
+    return
  ass
@@ -3205,39 +3205,11 @@
 Id()
-,
+;
 %0D%0A
-                instanceId);
 %0D%0A  
@@ -3258,25 +3258,62 @@
     
-e.getStackTrac
+System.err.println(e.awsErrorDetails().errorMessag
 e()
+)
 ;%0D%0A 
@@ -3323,23 +3323,66 @@
     
-%7D
+   System.exit(1);
 %0D%0A    
+ 
-%7D%0D%0A%7D%0D%0A%0D%0A
+   %7D%0D%0A       return %22%22;%0D%0A        
 // s
@@ -3424,15 +3424,21 @@
 ess.
-complete%5D
+main%5D%0D%0A    %7D%0D%0A%7D
 %0D%0A

Update AllocateAddress.java