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).
Use PutRolePolicy with an Amazon SDK or CLI
The following code examples show how to use PutRolePolicy.
Action examples are code excerpts from larger programs and must be run in context. You can see this action in
context in the following code examples:
- .NET
-
- Amazon SDK for .NET
-
/// <summary>
/// Update the inline policy document embedded in a role.
/// </summary>
/// <param name="policyName">The name of the policy to embed.</param>
/// <param name="roleName">The name of the role to update.</param>
/// <param name="policyDocument">The policy document that defines the role.</param>
/// <returns>A Boolean value indicating the success of the action.</returns>
public async Task<bool> PutRolePolicyAsync(string policyName, string roleName, string policyDocument)
{
var request = new PutRolePolicyRequest
{
PolicyName = policyName,
RoleName = roleName,
PolicyDocument = policyDocument
};
var response = await _IAMService.PutRolePolicyAsync(request);
return response.HttpStatusCode == HttpStatusCode.OK;
}
- C++
-
- SDK for C++
-
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();
}
- CLI
-
- Amazon CLI
-
To attach a permissions policy to an IAM role
The following put-role-policy command adds a permissions policy to the role named Test-Role.
aws iam put-role-policy \
--role-name Test-Role \
--policy-name ExamplePolicy \
--policy-document file://AdminPolicy.json
This command produces no output.
The policy is defined as a JSON document in the AdminPolicy.json file. (The file name and extension do not have significance.)
To attach a trust policy to a role, use the update-assume-role-policy command.
For more information, see Modifying a role in the Amazon IAM User Guide.
- JavaScript
-
- SDK for JavaScript (v3)
-
import { PutRolePolicyCommand, IAMClient } from "@aws-sdk/client-iam";
const examplePolicyDocument = JSON.stringify({
Version: "2012-10-17",
Statement: [
{
Sid: "VisualEditor0",
Effect: "Allow",
Action: [
"s3:ListBucketMultipartUploads",
"s3:ListBucketVersions",
"s3:ListBucket",
"s3:ListMultipartUploadParts",
],
Resource: "arn:aws:s3:::amzn-s3-demo-bucket",
},
{
Sid: "VisualEditor1",
Effect: "Allow",
Action: [
"s3:ListStorageLensConfigurations",
"s3:ListAccessPointsForObjectLambda",
"s3:ListAllMyBuckets",
"s3:ListAccessPoints",
"s3:ListJobs",
"s3:ListMultiRegionAccessPoints",
],
Resource: "*",
},
],
});
const client = new IAMClient({});
/**
*
* @param {string} roleName
* @param {string} policyName
* @param {string} policyDocument
*/
export const putRolePolicy = async (roleName, policyName, policyDocument) => {
const command = new PutRolePolicyCommand({
RoleName: roleName,
PolicyName: policyName,
PolicyDocument: policyDocument,
});
const response = await client.send(command);
console.log(response);
return response;
};
- PowerShell
-
- Tools for PowerShell V4
-
Example 1: This example creates an inline policy named FedTesterRolePolicy and embeds it in the IAM role FedTesterRole. If an inline policy with the same name already exists, then it is overwritten. The JSON policy content comes from the file FedTesterPolicy.json. Note that you must use the -Raw parameter to successfully process the content of the JSON file.
Write-IAMRolePolicy -RoleName FedTesterRole -PolicyName FedTesterRolePolicy -PolicyDocument (Get-Content -Raw FedTesterPolicy.json)
- Tools for PowerShell V5
-
Example 1: This example creates an inline policy named FedTesterRolePolicy and embeds it in the IAM role FedTesterRole. If an inline policy with the same name already exists, then it is overwritten. The JSON policy content comes from the file FedTesterPolicy.json. Note that you must use the -Raw parameter to successfully process the content of the JSON file.
Write-IAMRolePolicy -RoleName FedTesterRole -PolicyName FedTesterRolePolicy -PolicyDocument (Get-Content -Raw FedTesterPolicy.json)
For a complete list of Amazon SDK developer guides and code examples, see
Using this service with an Amazon SDK.
This topic also includes information about getting started and details about previous SDK versions.