SIGN IN SIGN UP

Welcome to the AWS Code Examples Repository. This repo contains code examples used in the AWS documentation, AWS SDK Developer Guides, and more. For more information, see the Readme.md file below.

0 0 12 Java
// 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:
2022-10-14 09:27:14 -04:00
* https://docs.aws.amazon.com/sdk-for-cpp/v1/developer-guide/getting-started.html.
*
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.
*
* Purpose
*
* Demonstrates putting an inline permissions policy on an IAM role.
*
*/
#include <aws/core/Aws.h>
#include <aws/iam/IAMClient.h>
#include <aws/iam/model/PutRolePolicyRequest.h>
#include <iostream>
#include "iam_samples.h"
//! Puts an inline permissions policy on an IAM role.
/*!
2022-10-12 14:52:10 -04:00
\sa putRolePolicy()
\param roleName: The IAM role name.
\param policyName: The policy name.
\param policyDocument: The policy document JSON string.
\param clientConfig: Aws client configuration.
\return bool: Successful completion.
*/
// snippet-start:[iam.cpp.put_role_policy.code]
bool AwsDoc::IAM::putRolePolicy(
2022-10-12 14:52:10 -04:00
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);
2022-10-12 14:52:10 -04:00
if (!outcome.IsSuccess()) {
std::cerr << "Error putting policy on role. " <<
outcome.GetError().GetMessage() << std::endl;
2019-02-08 14:17:38 -08:00
}
2022-10-12 14:52:10 -04:00
else {
std::cout << "Successfully put the role policy." << std::endl;
}
return outcome.IsSuccess();
}
// snippet-end:[iam.cpp.put_role_policy.code]
/*
*
* main function
*
* Prerequisites: An existing IAM role.
*
* Usage: 'run_put_role_policy <roleName> <policyName>'
*
*/
#ifndef TESTING_BUILD
int main(int argc, char** argv)
{
2022-10-12 14:52:10 -04:00
if (argc != 3) {
std::cout << "Usage: run_put_role_policy <roleName> <policyName>" << std::endl;
return 1;
}
2019-02-08 14:17:38 -08:00
Aws::SDKOptions options;
Aws::InitAPI(options);
{
// Set these configuration values before running the program.
Aws::String roleName = argv[1]; // An existing IAM role,
Aws::String policyName = argv[2];
// Define a permissions policy that enables Amazon S3 ReadOnly access.
2019-02-08 14:17:38 -08:00
Aws::String permissionsPolicy = R"({
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"s3:Get*",
"s3:List*"
],
"Resource": "*"
}
]
})";
Aws::Client::ClientConfiguration clientConfig;
// Optional: Set to the AWS Region in which the bucket was created (overrides config file).
// clientConfig.region = "us-east-1";
2022-10-12 14:52:10 -04:00
AwsDoc::IAM::putRolePolicy(roleName, policyName, permissionsPolicy, clientConfig);
2019-02-08 14:17:38 -08:00
}
Aws::ShutdownAPI(options);
return 0;
}
#endif // TESTING_BUILD