// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 /** * Before running this C++ code example, set up your development environment, * including your credentials. * * For more information, see the following documentation topic: * https://docs.aws.amazon.com/sdk-for-cpp/v1/developer-guide/getting-started.html. * * For information on the structure of the code examples and how to build and run the examples, see * https://docs.aws.amazon.com/sdk-for-cpp/v1/developer-guide/getting-started-code-examples.html. * * Purpose * * Demonstrates creating an access key for an IAM user. * */ // snippet-start:[iam.cpp.create_access_key.inc] #include #include #include #include #include "iam_samples.h" // snippet-end:[iam.cpp.create_access_key.inc] //! Creates an access key for an IAM user. /*! \sa createAccessKey() \param userName: User name for the access key. \param clientConfig: Aws client configuration. \return Aws::String: Access key ID or empty string if unsuccessful. */ // snippet-start:[iam.cpp.create_access_key.code] Aws::String AwsDoc::IAM::createAccessKey(const Aws::String &userName, const Aws::Client::ClientConfiguration &clientConfig) { Aws::IAM::IAMClient iam(clientConfig); Aws::IAM::Model::CreateAccessKeyRequest request; request.SetUserName(userName); Aws::String result; Aws::IAM::Model::CreateAccessKeyOutcome outcome = iam.CreateAccessKey(request); if (!outcome.IsSuccess()) { std::cerr << "Error creating access key for IAM user " << userName << ":" << outcome.GetError().GetMessage() << std::endl; } else { const auto &accessKey = outcome.GetResult().GetAccessKey(); std::cout << "Successfully created access key for IAM user " << userName << std::endl << " aws_access_key_id = " << accessKey.GetAccessKeyId() << std::endl << " aws_secret_access_key = " << accessKey.GetSecretAccessKey() << std::endl; result = accessKey.GetAccessKeyId(); } return result; } // snippet-end:[iam.cpp.create_access_key.code] /* * * main function * * Usage: 'run_create_access_key ' * */ #ifndef TESTING_BUILD int main(int argc, char **argv) { if (argc != 2) { std::cout << "Usage: run_create_access_key " << std::endl; return 1; } Aws::SDKOptions options; Aws::InitAPI(options); { Aws::Client::ClientConfiguration clientConfig; // Optional: Set to the AWS Region in which the bucket was created (overrides config file). // clientConfig.region = "us-east-1"; Aws::String user_name(argv[1]); AwsDoc::IAM::createAccessKey(user_name, clientConfig); } Aws::ShutdownAPI(options); return 0; } #endif // TESTING_BUILD