Skip to content

attach_user_policy

attach_user_policy(
    user_name: userNameType,
    policy_arn: arnType,
    *,
    config_overrides: Optional[IAMClientConfig] = None,
) -> None

Attaches the specified managed policy to the specified user.

You use this operation to attach a managed policy to a user. To embed an inline policy in a user, use PutUserPolicy .

As a best practice, you can validate your IAM policies. To learn more, see Validating IAM policies in the IAM User Guide.

For more information about policies, see Managed policies and inline policies in the IAM User Guide.

Parameters:

  • user_name (userNameType) –

    The name (friendly name, not ARN) of the IAM user to attach the policy to.

    This parameter allows (through its regex pattern) a string of characters consisting of upper and lowercase alphanumeric characters with no spaces. You can also include any of the following characters: _+=,.@-

  • policy_arn (arnType) –

    The Amazon Resource Name (ARN) of the IAM policy you want to attach.

    For more information about ARNs, see Amazon Resource Names (ARNs) in the Amazon Web Services General Reference.

Raises:

  • InvalidInputException –

    The request was rejected because an invalid or out-of-range value was supplied for an input parameter.

  • LimitExceededException –

    The request was rejected because it attempted to create resources beyond the current Amazon Web Services account limits. The error message describes the limit exceeded.

  • NoSuchEntityException –

    The request was rejected because it referenced a resource entity that does not exist. The error message describes the resource.

  • PolicyNotAttachableException –

    The request failed because Amazon Web Services service role policies can only be attached to the service-linked role for that service.

  • ServiceFailureException –

    The request processing has failed because of an unknown error, exception or failure.

  • UnknownServiceError –

    The service returned an error code this client does not model.

Examples:

To attach a managed policy to an IAM user The following command attaches the AWS managed policy named AdministratorAccess to the IAM user named Alice.

>>> client.attach_user_policy(user_name='Alice', policy_arn='arn:aws:iam::aws:policy/AdministratorAccess')
Source code in docs_src/capo_iam/_services/iam.py
def attach_user_policy(
    self,
    user_name: "capo_iam.types.user_name_type.userNameType",
    policy_arn: "capo_iam.types.arn_type.arnType",
    *,
    config_overrides: Optional[IAMClientConfig] = None,
) -> None:
    """<p>Attaches the specified managed policy to the specified user.</p> <p>You use this operation to attach a <i>managed</i> policy to a user. To embed an inline policy in a user, use <a href="https://docs.aws.amazon.com/IAM/latest/APIReference/API_PutUserPolicy.html"> <code>PutUserPolicy</code> </a>.</p> <p>As a best practice, you can validate your IAM policies. To learn more, see <a href="https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies_policy-validator.html">Validating IAM policies</a> in the <i>IAM User Guide</i>.</p> <p>For more information about policies, see <a href="https://docs.aws.amazon.com/IAM/latest/UserGuide/policies-managed-vs-inline.html">Managed policies and inline policies</a> in the <i>IAM User Guide</i>.</p>

    Args:
        user_name: <p>The name (friendly name, not ARN) of the IAM user to attach the policy to.</p> <p>This parameter allows (through its <a href="http://wikipedia.org/wiki/regex">regex pattern</a>) a string of characters consisting of upper and lowercase alphanumeric characters with no spaces. You can also include any of the following characters: _+=,.@-</p>
        policy_arn: <p>The Amazon Resource Name (ARN) of the IAM policy you want to attach.</p> <p>For more information about ARNs, see <a href="https://docs.aws.amazon.com/general/latest/gr/aws-arns-and-namespaces.html">Amazon Resource Names (ARNs)</a> in the <i>Amazon Web Services General Reference</i>.</p>

    Raises:
        capo_iam.errors.invalid_input_exception.InvalidInputException: <p>The request was rejected because an invalid or out-of-range value was supplied for an input parameter.</p>
        capo_iam.errors.limit_exceeded_exception.LimitExceededException: <p>The request was rejected because it attempted to create resources beyond the current Amazon Web Services account limits. The error message describes the limit exceeded.</p>
        capo_iam.errors.no_such_entity_exception.NoSuchEntityException: <p>The request was rejected because it referenced a resource entity that does not exist. The error message describes the resource.</p>
        capo_iam.errors.policy_not_attachable_exception.PolicyNotAttachableException: <p>The request failed because Amazon Web Services service role policies can only be attached to the service-linked role for that service.</p>
        capo_iam.errors.service_failure_exception.ServiceFailureException: <p>The request processing has failed because of an unknown error, exception or failure.</p>
        capo_iam.errors.UnknownServiceError: The service returned an error code this client does not model.

    Examples:
        To attach a managed policy to an IAM user
        The following command attaches the AWS managed policy named AdministratorAccess to the IAM user named Alice.

        >>> client.attach_user_policy(user_name='Alice', policy_arn='arn:aws:iam::aws:policy/AdministratorAccess')
    """

    def _handler(
        req: "OperationRequest[capo_iam.types.attach_user_policy_request.AttachUserPolicyRequest]",
    ) -> OperationResponse[None]:
        import capo_iam._operations.aws_identity_management_v20100508.attach_user_policy

        output, http_response = (
            capo_iam._operations.aws_identity_management_v20100508.attach_user_policy.attach_user_policy(
                req.options, req.input
            )
        )
        return OperationResponse(output=output, response=http_response)

    interceptors_, options_ = self.operation_options(
        config_overrides, OperationLogging("AttachUserPolicy")
    )
    input_: capo_iam.types.attach_user_policy_request.AttachUserPolicyRequest = {
        "user_name": user_name,
        "policy_arn": policy_arn,
    }

    response = execute_pipeline(
        OperationRequest(input=input_, options=options_),
        handler=_handler,
        interceptors=list(interceptors_),
    )
    response.response.close()
    return response.output