Use AttachUserPolicy
with an Amazon SDK or CLI
The following code examples show how to use AttachUserPolicy
.
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 example:
- CLI
-
- Amazon CLI
-
To attach a managed policy to an IAM user
The following
attach-user-policy
command attaches the Amazon managed policy namedAdministratorAccess
to the IAM user namedAlice
.aws iam attach-user-policy \ --policy-arn
arn:aws:iam::aws:policy/AdministratorAccess
\ --user-nameAlice
This command produces no output.
For more information, see Managed policies and inline policies
in the Amazon IAM User Guide. -
For API details, see AttachUserPolicy
in Amazon CLI Command Reference.
-
- PowerShell
-
- Tools for PowerShell V4
-
Example 1: This example attaches the Amazon managed policy named
AmazonCognitoPowerUser
to the IAM userBob
. The user is immediately affected by the permissions defined in the latest version of that policy.Register-IAMUserPolicy -UserName Bob -PolicyArn arn:aws:iam::aws:policy/AmazonCognitoPowerUser
-
For API details, see AttachUserPolicy
in Amazon Tools for PowerShell Cmdlet Reference (V4).
-
- Tools for PowerShell V5
-
Example 1: This example attaches the Amazon managed policy named
AmazonCognitoPowerUser
to the IAM userBob
. The user is immediately affected by the permissions defined in the latest version of that policy.Register-IAMUserPolicy -UserName Bob -PolicyArn arn:aws:iam::aws:policy/AmazonCognitoPowerUser
-
For API details, see AttachUserPolicy
in Amazon Tools for PowerShell Cmdlet Reference (V5).
-
- Python
-
- SDK for Python (Boto3)
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository
. 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
-
For API details, see AttachUserPolicy in Amazon SDK for Python (Boto3) API Reference.
-
- Ruby
-
- SDK for Ruby
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository
. # Attaches a policy to a user # # @param user_name [String] The name of the user # @param policy_arn [String] The Amazon Resource Name (ARN) of the policy # @return [Boolean] true if successful, false otherwise def attach_policy_to_user(user_name, policy_arn) @iam_client.attach_user_policy( user_name: user_name, policy_arn: policy_arn ) true rescue Aws::IAM::Errors::ServiceError => e @logger.error("Error attaching policy to user: #{e.message}") false end
-
For API details, see AttachUserPolicy in Amazon SDK for Ruby API Reference.
-
- Rust
-
- SDK for Rust
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository
. 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 API details, see AttachUserPolicy
in Amazon SDK for Rust API reference.
-
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.