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 IAM policy to a user using an Amazon SDK
The following code examples show how to attach an IAM policy to a user.
- Python
-
- SDK for Python (Boto3)
-
def attach_policy(user_name, policy_arn):
"""
Attaches a policy to a user.
:param user_name: The name of the user.
:param policy_arn: The Amazon Resource Name (ARN) of the policy.
"""
try:
iam.User(user_name).attach_policy(PolicyArn=policy_arn)
logger.info("Attached policy %s to user %s.", policy_arn, user_name)
except ClientError:
logger.exception("Couldn't attach policy %s to user %s.", policy_arn, user_name)
raise
- Rust
-
- SDK for Rust
-
This documentation is for an SDK in preview release. The SDK is subject to change and should not be used in production.
pub async fn attach_user_policy(
client: &iamClient,
user_name: &str,
policy_arn: &str,
) -> Result<(), iamError> {
client
.attach_user_policy()
.user_name(user_name)
.policy_arn(policy_arn)
.send()
.await?;
Ok(())
}
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.