| 1 |
.. Copyright 2010-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 |
|
| 3 |
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 |
| 4 |
International License (the "License"). You may not use this file except in compliance with the |
| 5 |
License. A copy of the License is located at http://creativecommons.org/licenses/by-nc-sa/4.0/. |
| 6 |
|
| 7 |
This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, |
| 8 |
either express or implied. See the License for the specific language governing permissions and |
| 9 |
limitations under the License. |
| 10 |
|
| 11 |
.. _aws-boto3-iam-examples-managing-users: |
| 12 |
|
| 13 |
****************** |
| 14 |
Managing IAM users |
| 15 |
****************** |
| 16 |
|
| 17 |
This Python example shows you how to create a user, list users, update a user name and delete a user. |
| 18 |
|
| 19 |
The scenario |
| 20 |
============ |
| 21 |
|
| 22 |
In this example Python code is used to create and manage users in IAM. The code uses the |
| 23 |
Amazon Web Services (AWS) SDK for Python to manage users using these methods of the |
| 24 |
IAM client class: |
| 25 |
|
| 26 |
* `create_user <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/iam.html#IAM.Client.create_user>`_ |
| 27 |
|
| 28 |
* `get_paginator('list_users') <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/iam.html#IAM.Client.get_paginator>`_. |
| 29 |
|
| 30 |
* `update_user <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/iam.html#IAM.Client.update_user>`_. |
| 31 |
|
| 32 |
* `delete_user <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/iam.html#IAM.Client.delete_user>`_. |
| 33 |
|
| 34 |
All the example code for the Amazon Web Services (AWS) SDK for Python is available `here on GitHub <https://github.com/awsdocs/aws-doc-sdk-examples/tree/master/python/example_code>`_. |
| 35 |
|
| 36 |
For more information about IAM users, see `IAM Users <http://docs.aws.amazon.com/IAM/latest/UserGuide/id_users.html>`_ |
| 37 |
in the *IAM User Guide*. |
| 38 |
|
| 39 |
Prerequisite tasks |
| 40 |
================= |
| 41 |
|
| 42 |
To set up and run this example, you must first configure your AWS credentials, as described in :doc:`quickstart`. |
| 43 |
|
| 44 |
Create a user |
| 45 |
============= |
| 46 |
|
| 47 |
Create a new IAM user for your AWS account. |
| 48 |
|
| 49 |
For information about limitations on the number of IAM users you can create, see |
| 50 |
`Limitations on IAM Entities <http://docs.aws.amazon.com/IAM/latest/UserGuide/reference_iam-limits.html>`_ |
| 51 |
in the *IAM User Guide*. |
| 52 |
|
| 53 |
The example below shows how to: |
| 54 |
|
| 55 |
* Create a new IAM user using |
| 56 |
`create_user <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/iam.html#IAM.Client.create_user>`_. |
| 57 |
|
| 58 |
Example |
| 59 |
------- |
| 60 |
|
| 61 |
.. code-block:: python |
| 62 |
|
| 63 |
import boto3 |
| 64 |
|
| 65 |
# Create IAM client |
| 66 |
iam = boto3.client('iam') |
| 67 |
|
| 68 |
# Create user |
| 69 |
response = iam.create_user( |
| 70 |
UserName='IAM_USER_NAME' |
| 71 |
) |
| 72 |
|
| 73 |
print(response) |
| 74 |
|
| 75 |
List users in your account |
| 76 |
========================== |
| 77 |
|
| 78 |
List the IAM users. |
| 79 |
|
| 80 |
The example below shows how to: |
| 81 |
|
| 82 |
* List the IAM users using |
| 83 |
`get_paginator('list_users') <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/iam.html#IAM.Client.get_paginator>`_. |
| 84 |
|
| 85 |
For more information about paginators see, :doc:`paginators` |
| 86 |
|
| 87 |
Example |
| 88 |
------- |
| 89 |
|
| 90 |
.. code-block:: python |
| 91 |
|
| 92 |
import boto3 |
| 93 |
|
| 94 |
# Create IAM client |
| 95 |
iam = boto3.client('iam') |
| 96 |
|
| 97 |
# List users with the pagination interface |
| 98 |
paginator = iam.get_paginator('list_users') |
| 99 |
for response in paginator.paginate(): |
| 100 |
print(response) |
| 101 |
|
| 102 |
Update a user's name |
| 103 |
==================== |
| 104 |
|
| 105 |
Update the name and/or the path of the specified IAM user. |
| 106 |
|
| 107 |
To change a user's name or path, you must use the AWS CLI, Tools for Windows PowerShell, or AWS API. |
| 108 |
There is no option in the console to rename a user. For information about the permissions that you |
| 109 |
need in order to rename a user, see |
| 110 |
`Delegating Permissions to Administer IAM Users, Groups, and Credentials <http://docs.aws.amazon.com/IAM/latest/UserGuide/id_users_manage.html#id_users_renaming>`_ |
| 111 |
in the *IAM User Guide*. |
| 112 |
|
| 113 |
The example below shows how to: |
| 114 |
|
| 115 |
* Update an IAM user name using |
| 116 |
`update_user <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/iam.html#IAM.Client.update_user>`_. |
| 117 |
|
| 118 |
Example |
| 119 |
------- |
| 120 |
|
| 121 |
.. code-block:: python |
| 122 |
|
| 123 |
import boto3 |
| 124 |
|
| 125 |
# Create IAM client |
| 126 |
iam = boto3.client('iam') |
| 127 |
|
| 128 |
# Update a user name |
| 129 |
iam.update_user( |
| 130 |
UserName='IAM_USER_NAME', |
| 131 |
NewUserName='NEW_IAM_USER_NAME' |
| 132 |
) |
| 133 |
|
| 134 |
|
| 135 |
Delete a user |
| 136 |
============= |
| 137 |
|
| 138 |
Delete the specified IAM user. The user must not belong to any groups or have any access keys, signing |
| 139 |
certificates, or attached policies. |
| 140 |
|
| 141 |
The example below shows how to: |
| 142 |
|
| 143 |
* Delete an IAM user name using |
| 144 |
`delete_user <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/iam.html#IAM.Client.delete_user>`_. |
| 145 |
|
| 146 |
Example |
| 147 |
------- |
| 148 |
|
| 149 |
.. code-block:: python |
| 150 |
|
| 151 |
import boto3 |
| 152 |
|
| 153 |
# Create IAM client |
| 154 |
iam = boto3.client('iam') |
| 155 |
|
| 156 |
# Delete a user |
| 157 |
iam.delete_user( |
| 158 |
UserName='IAM_USER_NAME' |
| 159 |
) |
| 160 |
|