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