Attach an inline policy to an IAM role using an Amazon SDK - Amazon Identity and Access Management
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Attach an inline policy to an IAM role using an Amazon SDK

The following code example shows how to attach an inline policy to an IAM role.

SDK for C++

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

bool AwsDoc::IAM::putRolePolicy( const Aws::String &roleName, const Aws::String &policyName, const Aws::String &policyDocument, const Aws::Client::ClientConfiguration &clientConfig) { Aws::IAM::IAMClient iamClient(clientConfig); Aws::IAM::Model::PutRolePolicyRequest request; request.SetRoleName(roleName); request.SetPolicyName(policyName); request.SetPolicyDocument(policyDocument); Aws::IAM::Model::PutRolePolicyOutcome outcome = iamClient.PutRolePolicy(request); if (!outcome.IsSuccess()) { std::cerr << "Error putting policy on role. " << outcome.GetError().GetMessage() << std::endl; } else { std::cout << "Successfully put the role policy." << std::endl; } return outcome.IsSuccess(); }
  • For API details, see PutRolePolicy in Amazon SDK for C++ API Reference.

For a complete list of Amazon SDK developer guides and code examples, see Using IAM with an Amazon SDK. This topic also includes information about getting started and details about previous SDK versions.