Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions,
see Getting Started with Amazon Web Services in China
(PDF).
Use GetPolicyVersion
with an Amazon SDK or CLI
The following code examples show how to use GetPolicyVersion
.
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 examples:
- CLI
-
- Amazon CLI
-
To retrieve information about the specified version of the specified managed policy
This example returns the policy document for the v2 version of the policy whose ARN is arn:aws:iam::123456789012:policy/MyManagedPolicy
.
aws iam get-policy-version \
--policy-arn arn:aws:iam::123456789012:policy/MyPolicy
\
--version-id v2
Output:
{
"PolicyVersion": {
"Document": {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "iam:*",
"Resource": "*"
}
]
},
"VersionId": "v2",
"IsDefaultVersion": true,
"CreateDate": "2023-04-11T00:22:54+00:00"
}
}
For more information, see Policies and permissions in IAM in the Amazon IAM User Guide.
- PowerShell
-
- Tools for PowerShell
-
Example 1: This example returns the policy document for the v2
version of the policy whose ARN is arn:aws:iam::123456789012:policy/MyManagedPolicy
. The policy document in the Document
property is URL encoded and is decoded in this example with the UrlDecode
.NET method.
$results = Get-IAMPolicyVersion -PolicyArn arn:aws:iam::123456789012:policy/MyManagedPolicy -VersionId v2
$results
Output:
CreateDate Document IsDefaultVersion VersionId
---------- -------- ---------------- ---------
2/12/2015 9:39:53 AM %7B%0A%20%20%22Version%22%3A%20%222012-10... True v2
[System.Reflection.Assembly]::LoadWithPartialName("System.Web.HttpUtility")
$policy = [System.Web.HttpUtility]::UrlDecode($results.Document)
$policy
{
"Version": "2012-10-17",
"Statement":
{
"Effect": "Allow",
"Action": "*",
"Resource": "*"
}
}
- Python
-
- SDK for Python (Boto3)
-
def get_default_policy_statement(policy_arn):
"""
Gets the statement of the default version of the specified policy.
:param policy_arn: The ARN of the policy to look up.
:return: The statement of the default policy version.
"""
try:
policy = iam.Policy(policy_arn)
# To get an attribute of a policy, the SDK first calls get_policy.
policy_doc = policy.default_version.document
policy_statement = policy_doc.get("Statement", None)
logger.info("Got default policy doc for %s.", policy.policy_name)
logger.info(policy_doc)
except ClientError:
logger.exception("Couldn't get default policy statement for %s.", policy_arn)
raise
else:
return policy_statement
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.