//snippet-sourcedescription:[CreateAccessKey.java demonstrates how to create an access key for an IAM user.]
//snippet-keyword:[SDK for Java 2.0]
//snippet-keyword:[Code Sample]
//snippet-service:[iam]
//snippet-sourcetype:[full-example]
//snippet-sourcedate:[03/02/2020]
//snippet-sourceauthor:[scmacdon-aws]
/*
 * Copyright 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.iam;
// snippet-start:[iam.java2.create_access_key.complete]
// snippet-start:[iam.java2.create_access_key.import]
import software.amazon.awssdk.services.iam.model.CreateAccessKeyRequest;
import software.amazon.awssdk.services.iam.model.CreateAccessKeyResponse;

import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.iam.IamClient;
import software.amazon.awssdk.services.iam.model.IamException;
// snippet-end:[iam.java2.create_access_key.import]

/**
 * Creates an access key for an IAM user
 */
public class CreateAccessKey {

    public static void main(String[] args) {

        final String USAGE =
                "To run this example, supply an IAM user that you can obtain from the AWS Console\n" +
                        "Ex: CreateAccessKey <user>\n";

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

        String user = args[0];

        // snippet-start:[iam.java2.create_access_key.main]
        Region region = Region.AWS_GLOBAL;
        IamClient iam = IamClient
                .builder()
                .region(region)
                .build();

        try {

            CreateAccessKeyRequest request = CreateAccessKeyRequest.builder()
                .userName(user).build();

            CreateAccessKeyResponse response = iam.createAccessKey(request);

            System.out.println("Created access key: " + response.accessKey());
            // snippet-end:[iam.java2.create_access_key.main]

        } catch (IamException e) {
            System.err.println(e.awsErrorDetails().errorMessage());
            System.exit(1);
        }
        System.out.println("Done");
    }
}
// snippet-end:[iam.java2.create_access_key.complete]

@@ -193,19 +193,23 @@
 ervice:%5B
-iam
+AWS IAM
 %5D%0D%0A//sni
@@ -933,63 +933,8 @@
 m;%0D%0A
-// snippet-start:%5Biam.java2.create_access_key.complete%5D
 %0D%0A//
@@ -1135,18 +1135,16 @@
 ponse;%0D%0A
-%0D%0A
 import s
@@ -1718,32 +1718,34 @@
 1) %7B%0D%0A          
+  
 System.out.print
@@ -1752,24 +1752,26 @@
 ln(USAGE);%0D%0A
+  
           Sy
@@ -1836,69 +1836,8 @@
 %0D%0A%0D%0A
-        // snippet-start:%5Biam.java2.create_access_key.main%5D%0D%0A
     
@@ -1993,32 +1993,283 @@
    .build();%0D%0A%0D%0A
+        String keyId = createIAMAccessKey(iam, user);%0D%0A        System.out.println(%22The Key Id is %22 +keyId);%0D%0A    %7D%0D%0A%0D%0A    // snippet-start:%5Biam.java2.create_access_key.main%5D%0D%0A    public static String createIAMAccessKey(IamClient iam,String user) %7B%0D%0A%0D%0A
         try %7B%0D%0A%0D
@@ -2263,26 +2263,24 @@
      try %7B%0D%0A
-%0D%0A
             
@@ -2464,27 +2464,24 @@
 (request);%0D%0A
-%0D%0A 
            S
@@ -2484,50 +2484,21 @@
    S
-ystem.out.println(%22Created access key: %22 +
+tring keyId =
  res
@@ -2514,16 +2514,29 @@
 essKey()
+.accessKeyId(
 );%0D%0A    
@@ -2546,58 +2546,21 @@
     
- // snippet-end:%5Biam.java2.create_access_key.main%5D
+return keyId;
 %0D%0A%0D%0A
@@ -2716,34 +2716,17 @@
     
-System.out.println(%22Done%22)
+return %22%22
 ;%0D%0A 
@@ -2731,19 +2731,20 @@
 %0A    %7D%0D%0A
-%7D%0D%0A
+    
 // snipp
@@ -2783,15 +2783,14 @@
 key.
-complete%5D
+main%5D%0D%0A%7D
 %0D%0A

Update CreateAccessKey.java