| 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-managing-access-keys: |
| 12 |
|
| 13 |
######################## |
| 14 |
Managing IAM access keys |
| 15 |
######################## |
| 16 |
|
| 17 |
This Python example shows you how to manage the access keys of your users. |
| 18 |
|
| 19 |
The scenario |
| 20 |
============ |
| 21 |
|
| 22 |
Users need their own access keys to make programmatic calls to AWS from the Amazon Web Services (AWS) |
| 23 |
SDK for Python. To fill this need, you can create, modify, view, or rotate access keys |
| 24 |
(access key IDs and secret access keys) for IAM users. By default, when you create an access key, its |
| 25 |
status is Active, which means the user can use the access key for API calls. |
| 26 |
|
| 27 |
In this example, Python code is used to manage access keys in IAM. The code uses the AWS SDK for Python |
| 28 |
to manage IAM access keys using these methods of the IAM client class: |
| 29 |
|
| 30 |
* `create_access_key <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/iam.html#IAM.Client.create_access_key>`_. |
| 31 |
|
| 32 |
* `paginate(UserName='IAM_USER_NAME') <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/iam.html#IAM.Client.paginate>`_. |
| 33 |
|
| 34 |
* `get_access_key_last_used <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/iam.html#IAM.Client.get_access_key_last_used>`_. |
| 35 |
|
| 36 |
* `update_access_key <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/iam.html#IAM.Client.update_access_key>`_. |
| 37 |
|
| 38 |
* `delete_access_key <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/iam.html#IAM.Client.delete_access_key>`_. |
| 39 |
|
| 40 |
|
| 41 |
For more information about IAM access keys, see `Managing Access Keys <http://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_access-keys.html>`_ |
| 42 |
in the *IAM User Guide*. |
| 43 |
|
| 44 |
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>`_. |
| 45 |
|
| 46 |
Prerequisite tasks |
| 47 |
================= |
| 48 |
|
| 49 |
To set up and run this example, you must first configure your AWS credentials, as described in :doc:`quickstart`. |
| 50 |
|
| 51 |
Create access keys for a user |
| 52 |
============================= |
| 53 |
|
| 54 |
Create a new AWS secret access key and corresponding AWS access key ID for the specified user. The |
| 55 |
default status for new keys is :code:`Active`. |
| 56 |
|
| 57 |
The example below shows how to: |
| 58 |
|
| 59 |
* Create a new AWS access key using |
| 60 |
`create_access_key <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/iam.html#IAM.Client.create_access_key>`_. |
| 61 |
|
| 62 |
Example |
| 63 |
------- |
| 64 |
|
| 65 |
.. code-block:: python |
| 66 |
|
| 67 |
import boto3 |
| 68 |
|
| 69 |
# Create IAM client |
| 70 |
iam = boto3.client('iam') |
| 71 |
|
| 72 |
# Create an access key |
| 73 |
response = iam.create_access_key( |
| 74 |
UserName='IAM_USER_NAME' |
| 75 |
) |
| 76 |
|
| 77 |
print(response['AccessKey']) |
| 78 |
|
| 79 |
List a user's access keys |
| 80 |
========================= |
| 81 |
|
| 82 |
List information about the access key IDs associated with the specified IAM user. If there are none, |
| 83 |
the action returns an empty list. |
| 84 |
|
| 85 |
If the UserName field is not specified, the UserName is determined implicitly based on the AWS access |
| 86 |
key ID used to sign the request. Because this action works for access keys under the AWS account, |
| 87 |
you can use this action to manage root credentials even if the AWS account has no associated users. |
| 88 |
|
| 89 |
The example below shows how to: |
| 90 |
|
| 91 |
* List a user's access keys using |
| 92 |
`paginate(UserName='IAM_USER_NAME') <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/iam.html#IAM.Client.paginate>`_. |
| 93 |
|
| 94 |
For more information about paginators see, :doc:`paginators` |
| 95 |
|
| 96 |
Example |
| 97 |
------- |
| 98 |
|
| 99 |
.. code-block:: python |
| 100 |
|
| 101 |
import boto3 |
| 102 |
|
| 103 |
# Create IAM client |
| 104 |
iam = boto3.client('iam') |
| 105 |
|
| 106 |
# List access keys through the pagination interface. |
| 107 |
paginator = iam.get_paginator('list_access_keys') |
| 108 |
for response in paginator.paginate(UserName='IAM_USER_NAME'): |
| 109 |
print(response) |
| 110 |
|
| 111 |
|
| 112 |
Get the access key last used |
| 113 |
============================ |
| 114 |
|
| 115 |
Get information about when the specified access key was last used. The information includes the |
| 116 |
date and time of last use, along with the AWS service and region that were specified in the last request |
| 117 |
made with that key. |
| 118 |
|
| 119 |
The example below shows how to: |
| 120 |
|
| 121 |
* Get the access key last used using |
| 122 |
`get_access_key_last_used <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/iam.html#IAM.Client.get_access_key_last_used>`_. |
| 123 |
|
| 124 |
Example |
| 125 |
------- |
| 126 |
|
| 127 |
.. code-block:: python |
| 128 |
|
| 129 |
import boto3 |
| 130 |
|
| 131 |
|
| 132 |
# Create IAM client |
| 133 |
iam = boto3.client('iam') |
| 134 |
|
| 135 |
# Get last use of access key |
| 136 |
response = iam.get_access_key_last_used( |
| 137 |
AccessKeyId='ACCESS_KEY_ID' |
| 138 |
) |
| 139 |
|
| 140 |
print(response['AccessKeyLastUsed']) |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
Update access key status |
| 145 |
======================== |
| 146 |
|
| 147 |
Change the status of the specified access key from Active to Inactive, or vice versa. This action |
| 148 |
can be used to disable a user's key as part of a key rotation work flow. |
| 149 |
|
| 150 |
The example below shows how to: |
| 151 |
|
| 152 |
* Change the status of an access key to :code:`Active` using |
| 153 |
`update_access_key <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/iam.html#IAM.Client.update_access_key>`_. |
| 154 |
|
| 155 |
Example |
| 156 |
------- |
| 157 |
|
| 158 |
.. code-block:: python |
| 159 |
|
| 160 |
import boto3 |
| 161 |
|
| 162 |
# Create IAM client |
| 163 |
iam = boto3.client('iam') |
| 164 |
|
| 165 |
# Update access key to be active |
| 166 |
iam.update_access_key( |
| 167 |
AccessKeyId='ACCESS_KEY_ID', |
| 168 |
Status='Active', |
| 169 |
UserName='IAM_USER_NAME' |
| 170 |
) |
| 171 |
|
| 172 |
|
| 173 |
Delete an access key |
| 174 |
==================== |
| 175 |
|
| 176 |
Delete the access key pair associated with the specified IAM user. |
| 177 |
|
| 178 |
If you do not specify a user name, IAM determines the user name implicitly based on the AWS access |
| 179 |
key ID signing the request. Because this action works for access keys under the AWS account, you can |
| 180 |
use this action to manage root credentials even if the AWS account has no associated users. |
| 181 |
|
| 182 |
The example below shows how to: |
| 183 |
|
| 184 |
* Delete an access key using |
| 185 |
`delete_access_key <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/iam.html#IAM.Client.delete_access_key>`_. |
| 186 |
|
| 187 |
Example |
| 188 |
------- |
| 189 |
|
| 190 |
.. code-block:: python |
| 191 |
|
| 192 |
import boto3 |
| 193 |
|
| 194 |
# Create IAM client |
| 195 |
iam = boto3.client('iam') |
| 196 |
|
| 197 |
# Delete access key |
| 198 |
iam.delete_access_key( |
| 199 |
AccessKeyId='ACCESS_KEY_ID', |
| 200 |
UserName='IAM_USER_NAME' |
| 201 |
) |
| 202 |
|
| 203 |
|
| 204 |
|