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 8 Java
2022-10-03 09:54:27 -04:00
// 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.
2022-10-03 09:54:27 -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-10-03 09:54:27 -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-10-03 09:54:27 -04:00
* Purpose
*
2022-10-12 14:52:10 -04:00
* Demonstrate creating an IAM role.
2022-10-03 09:54:27 -04:00
*
*/
2022-10-03 09:54:27 -04:00
#include <aws/core/Aws.h>
#include <aws/iam/IAMClient.h>
#include <aws/iam/model/CreateRoleRequest.h>
#include <aws/iam/model/Role.h>
#include <iostream>
#include "iam_samples.h"
2022-10-12 14:52:10 -04:00
//! Creates an IAM role.
2022-10-03 09:54:27 -04:00
/*!
\sa createIamRole()
\param roleName: The role name.
\param policy: The role trust policy.
\param clientConfig: Aws client configuration.
\return bool: Successful completion.
*/
2022-10-12 14:52:10 -04:00
// snippet-start:[iam.cpp.create_iam_role.code]
2022-10-03 09:54:27 -04:00
bool AwsDoc::IAM::createIamRole(
const Aws::String &roleName,
const Aws::String &policy,
const Aws::Client::ClientConfiguration &clientConfig) {
2022-10-03 09:54:27 -04:00
Aws::IAM::IAMClient client(clientConfig);
Aws::IAM::Model::CreateRoleRequest request;
2022-10-03 09:54:27 -04:00
request.SetRoleName(roleName);
request.SetAssumeRolePolicyDocument(policy);
2022-10-03 09:54:27 -04:00
Aws::IAM::Model::CreateRoleOutcome outcome = client.CreateRole(request);
if (!outcome.IsSuccess()) {
std::cerr << "Error creating role. " <<
outcome.GetError().GetMessage() << std::endl;
}
else {
2022-10-03 09:54:27 -04:00
const Aws::IAM::Model::Role iamRole = outcome.GetResult().GetRole();
std::cout << "Created role " << iamRole.GetRoleName() << "\n";
std::cout << "ID: " << iamRole.GetRoleId() << "\n";
std::cout << "ARN: " << iamRole.GetArn() << std::endl;
2019-02-08 14:17:38 -08:00
}
return outcome.IsSuccess();
}
2022-10-03 09:54:27 -04:00
// snippet-end:[iam.cpp.create_iam_role.code]
2022-10-03 09:54:27 -04:00
/*
*
* main function
*
2022-10-12 14:52:10 -04:00
* Usage: 'run_create_role <roleName>'
2022-10-03 09:54:27 -04:00
*
*/
2022-10-03 09:54:27 -04:00
#ifndef TESTING_BUILD
int main(int argc, char **argv) {
if (argc != 2) {
2022-10-12 14:52:10 -04:00
std::cout << "run_create_role <roleName>" <<
2022-10-03 09:54:27 -04:00
std::endl;
return 1;
}
2019-02-08 14:17:38 -08:00
Aws::SDKOptions options;
Aws::InitAPI(options);
{
2022-10-03 09:54:27 -04:00
Aws::String roleName = argv[1];
// Define a role trust policy.
2019-02-08 14:17:38 -08:00
Aws::String roleTrustPolicy = R"({
"Version": "2012-10-17",
"Statement": {
"Effect": "Allow",
"Principal": {"Service": "ec2.amazonaws.com"},
"Action": "sts:AssumeRole"
}
})";
Aws::IAM::Model::Role iamRole;
2022-10-03 09:54:27 -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";
AwsDoc::IAM::createIamRole(roleName, roleTrustPolicy, clientConfig);
}
2019-02-08 14:17:38 -08:00
Aws::ShutdownAPI(options);
return 0;
}
2022-10-03 09:54:27 -04:00
#endif // TESTING_BUILD