Criar uma versão de política do IAM usando um AWS SDK
O exemplo de código a seguir mostra como criar uma versão de política do IAM.
- Python
-
- SDK para Python (Boto3).
-
Para saber mais sobre como configurar e executar esse exemplo, consulte o GitHub.
def create_policy_version(policy_arn, actions, resource_arn, set_as_default):
"""
Creates a policy version. Policies can have up to five versions. The default
version is the one that is used for all resources that reference the policy.
:param policy_arn: The ARN of the policy.
:param actions: The actions to allow in the policy version.
:param resource_arn: The ARN of the resource this policy version applies to.
:param set_as_default: When True, this policy version is set as the default
version for the policy. Otherwise, the default
is not changed.
:return: The newly created policy version.
"""
policy_doc = {
'Version': '2012-10-17',
'Statement': [
{
'Effect': 'Allow',
'Action': actions,
'Resource': resource_arn
}
]
}
try:
policy = iam.Policy(policy_arn)
policy_version = policy.create_version(
PolicyDocument=json.dumps(policy_doc), SetAsDefault=set_as_default)
logger.info(
"Created policy version %s for policy %s.",
policy_version.version_id, policy_version.arn)
except ClientError:
logger.exception("Couldn't create a policy version for %s.", policy_arn)
raise
else:
return policy_version
Para obter uma lista completa dos Guias do desenvolvedor do SDK da AWS e exemplos de código, consulte Usar o IAM com um AWS SDK. Este tópico também inclui informações sobre como começar e detalhes sobre versões anteriores do SDK.