2022-10-03 09:54:27 -04:00
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
2019-02-08 13:51:58 -08:00
/**
2022-10-07 15:58:01 -04:00
* 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
*
2019-02-08 13:51:58 -08:00
*/
2022-10-03 09:54:27 -04:00
2022-10-07 15:58:01 -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 (
2022-10-07 15:58:01 -04:00
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 ;
2019-02-08 13:51:58 -08:00
2022-10-03 09:54:27 -04:00
request . SetRoleName ( roleName ) ;
request . SetAssumeRolePolicyDocument ( policy ) ;
2019-02-08 13:51:58 -08:00
2022-10-03 09:54:27 -04:00
Aws : : IAM : : Model : : CreateRoleOutcome outcome = client . CreateRole ( request ) ;
2022-10-07 15:58:01 -04:00
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
}
2019-02-08 13:51:58 -08:00
2022-10-07 15:58:01 -04:00
return outcome . IsSuccess ( ) ;
2019-02-08 13:51:58 -08:00
}
2022-10-03 09:54:27 -04:00
// snippet-end:[iam.cpp.create_iam_role.code]
2019-02-08 13:51:58 -08:00
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
*
2019-02-08 13:51:58 -08:00
*/
2022-10-03 09:54:27 -04:00
# ifndef TESTING_BUILD
2022-10-07 15:58:01 -04:00
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 ] ;
2022-10-07 15:58:01 -04:00
2022-10-20 15:00:56 -04:00
// 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 ;
2019-02-08 13:51:58 -08:00
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 ) ;
2022-10-07 15:58:01 -04:00
}
2019-02-08 14:17:38 -08:00
Aws : : ShutdownAPI ( options ) ;
return 0 ;
2019-02-08 13:51:58 -08:00
}
2022-10-07 15:58:01 -04:00
2022-10-03 09:54:27 -04:00
# endif // TESTING_BUILD