SIGN IN SIGN UP

Welcome to the AWS Code Examples Repository. This repo contains code examples used in the AWS documentation, AWS SDK Developer Guides, and more. For more information, see the Readme.md file below.

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