Repository navigation
Expand file tree
/
Copy pathcreate_role.cpp
More file actions
103 lines (91 loc) · 3.03 KB
/
Copy pathcreate_role.cpp
File metadata and controls
103 lines (91 loc) · 3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// 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 <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"
//! 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 <roleName>'
*
*/
#ifndef TESTING_BUILD
int main(int argc, char **argv) {
if (argc != 2) {
std::cout << "run_create_role <roleName>" <<
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