| 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-policies: |
| 12 |
|
| 13 |
######################### |
| 14 |
Working with IAM policies |
| 15 |
######################### |
| 16 |
|
| 17 |
This Python example shows you how to create and get IAM policies and attach and detach IAM policies from roles. |
| 18 |
|
| 19 |
The scenario |
| 20 |
============ |
| 21 |
|
| 22 |
You grant permissions to a user by creating a policy, which is a document that lists the actions |
| 23 |
that a user can perform and the resources those actions can affect. Any actions or resources that are |
| 24 |
not explicitly allowed are denied by default. Policies can be created and attached to users, groups |
| 25 |
of users, roles assumed by users, and resources. |
| 26 |
|
| 27 |
In this example, Python code used to manage policies in IAM. The code uses the |
| 28 |
Amazon Web Services (AWS) SDK for Python to create and delete policies as well as attaching and |
| 29 |
detaching role policies using these methods of the IAM client class: |
| 30 |
|
| 31 |
* `create_policy <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/iam.html#IAM.Client.create_policy>`_. |
| 32 |
|
| 33 |
* `get_policy <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/iam.html#IAM.Client.get_policy>`_. |
| 34 |
|
| 35 |
* `attach_role_policy <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/iam.html#IAM.Client.attach_role_policy>`_. |
| 36 |
|
| 37 |
* `detach_role_policy <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/iam.html#IAM.Client.detach_role_policy>`_. |
| 38 |
|
| 39 |
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>`_. |
| 40 |
|
| 41 |
For more information about IAM policies, see `Overview of Access Management: Permissions and Policies <http://docs.aws.amazon.com/IAM/latest/UserGuide/introduction_access-management.html>`_ |
| 42 |
in the IAM User Guide. |
| 43 |
|
| 44 |
Prerequisite tasks |
| 45 |
================= |
| 46 |
|
| 47 |
To set up and run this example, you must first configure your AWS credentials, as described in :doc:`quickstart`. |
| 48 |
|
| 49 |
Create an IAM policy |
| 50 |
==================== |
| 51 |
|
| 52 |
Create a new managed policy for your AWS account. |
| 53 |
|
| 54 |
This operation creates a policy version with a version identifier of :code:`v1` and sets :code:`v1` |
| 55 |
as the policy's default version. For more information about policy versions, see |
| 56 |
`Versioning for Managed Policies <http://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies_managed-versioning.html>`_ |
| 57 |
in the *IAM User Guide*. |
| 58 |
|
| 59 |
The example below shows how to: |
| 60 |
|
| 61 |
* Create a new managed policy using |
| 62 |
`create_policy <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/iam.html#IAM.Client.create_policy>`_. |
| 63 |
|
| 64 |
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>`_. |
| 65 |
|
| 66 |
Example |
| 67 |
------- |
| 68 |
|
| 69 |
.. code-block:: python |
| 70 |
|
| 71 |
import json |
| 72 |
|
| 73 |
import boto3 |
| 74 |
|
| 75 |
# Create IAM client |
| 76 |
iam = boto3.client('iam') |
| 77 |
|
| 78 |
# Create a policy |
| 79 |
my_managed_policy = { |
| 80 |
"Version": "2012-10-17", |
| 81 |
"Statement": [ |
| 82 |
{ |
| 83 |
"Effect": "Allow", |
| 84 |
"Action": "logs:CreateLogGroup", |
| 85 |
"Resource": "RESOURCE_ARN" |
| 86 |
}, |
| 87 |
{ |
| 88 |
"Effect": "Allow", |
| 89 |
"Action": [ |
| 90 |
"dynamodb:DeleteItem", |
| 91 |
"dynamodb:GetItem", |
| 92 |
"dynamodb:PutItem", |
| 93 |
"dynamodb:Scan", |
| 94 |
"dynamodb:UpdateItem" |
| 95 |
], |
| 96 |
"Resource": "RESOURCE_ARN" |
| 97 |
} |
| 98 |
] |
| 99 |
} |
| 100 |
response = iam.create_policy( |
| 101 |
PolicyName='myDynamoDBPolicy', |
| 102 |
PolicyDocument=json.dumps(my_managed_policy) |
| 103 |
) |
| 104 |
print(response) |
| 105 |
|
| 106 |
Get an IAM policy |
| 107 |
================= |
| 108 |
|
| 109 |
Get information about the specified managed policy, including the policy's default version and |
| 110 |
the total number of IAM users, groups, and roles to which the policy is attached. To get the |
| 111 |
list of the specific users, groups, and roles that the policy is attached to, use the |
| 112 |
:code:`list_entities_for_policy` API. This API returns metadata about the policy. To get the actual policy |
| 113 |
document for a specific version of the policy, use :code:`get_policy_version` API. |
| 114 |
|
| 115 |
This API gets information about managed policies. To get information about an inline policy |
| 116 |
that is embedded with an IAM user, group, or role, use the :code:`get_user_policy`, :code:`get_group_policy`, |
| 117 |
or :code:`get_role_policy` API. |
| 118 |
|
| 119 |
The example below shows how to: |
| 120 |
|
| 121 |
* Get information about a managed policy using |
| 122 |
`get_policy <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/iam.html#IAM.Client.get_policy>`_. |
| 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 a policy |
| 136 |
response = iam.get_policy( |
| 137 |
PolicyArn='arn:aws:iam::aws:policy/AWSLambdaExecute' |
| 138 |
) |
| 139 |
print(response['Policy']) |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
Attach a managed role policy |
| 144 |
============================ |
| 145 |
|
| 146 |
When you attach a managed policy to a role, the managed policy becomes part of the role's permission |
| 147 |
(access) policy. You cannot use a managed policy as the role's trust policy. The role's trust policy |
| 148 |
is created at the same time as the role, using :code:`create_role`. You can update a role's trust policy using |
| 149 |
:code:`update_assume_role_policy`. |
| 150 |
|
| 151 |
Use this API to attach a managed policy to a role. To embed an inline policy in a role, use :code:`put_role_policy`. |
| 152 |
|
| 153 |
The example below shows how to: |
| 154 |
|
| 155 |
* Attach a managed policy to an IAM role. using |
| 156 |
`attach_role_policy <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/iam.html#IAM.Client.attach_role_policy>`_. |
| 157 |
|
| 158 |
Example |
| 159 |
------- |
| 160 |
|
| 161 |
.. code-block:: python |
| 162 |
|
| 163 |
import boto3 |
| 164 |
|
| 165 |
# Create IAM client |
| 166 |
iam = boto3.client('iam') |
| 167 |
|
| 168 |
# Attach a role policy |
| 169 |
iam.attach_role_policy( |
| 170 |
PolicyArn='arn:aws:iam::aws:policy/AmazonDynamoDBFullAccess', |
| 171 |
RoleName='AmazonDynamoDBFullAccess' |
| 172 |
) |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
Detach a managed role policy |
| 177 |
============================ |
| 178 |
|
| 179 |
Detach the specified managed policy from the specified role. |
| 180 |
|
| 181 |
A role can also have inline policies embedded with it. To delete an inline policy, use the |
| 182 |
:code:`delete_role_policy` API. For information about policies, see |
| 183 |
`Managed Policies and Inline Policies <http://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies_managed-vs-inline.html>`_ |
| 184 |
in the *IAM User Guide*. |
| 185 |
|
| 186 |
The example below shows how to: |
| 187 |
|
| 188 |
* Detach a managed role policy using |
| 189 |
`detach_role_policy <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/iam.html#IAM.Client.detach_role_policy>`_. |
| 190 |
|
| 191 |
Example |
| 192 |
------- |
| 193 |
|
| 194 |
.. code-block:: python |
| 195 |
|
| 196 |
import boto3 |
| 197 |
|
| 198 |
# Create IAM client |
| 199 |
iam = boto3.client('iam') |
| 200 |
|
| 201 |
# Detach a role policy |
| 202 |
iam.detach_role_policy( |
| 203 |
PolicyArn='arn:aws:iam::aws:policy/AmazonDynamoDBFullAccess', |
| 204 |
RoleName='AmazonDynamoDBFullAccess' |
| 205 |
) |
| 206 |
|