切换到 IAM 角色 (Amazon API)
角色 指定可用于访问 Amazon 资源的一组权限。在这种意义上,它类似于 IAM 用户。主体(用户或应用程序)将担任角色以获得临时权限,从而执行所需任务并与 Amazon 资源交互。角色可以在您自己的账户中或任何其他 Amazon Web Services 账户 中。有关角色、其权益以及如何创建和配置角色的更多信息,请参阅IAM 角色和创建 IAM 角色。要了解在担任角色时使用的各种方法,请参阅使用 IAM 角色。
不会累积您的 IAM 用户和担任的任何角色的权限。一次只有一组权限处于活动状态。在担任某个角色时,您将临时放弃以前的用户或角色权限并使用为该角色分配的权限。退出该角色后,您的原始权限将自动恢复。
要担任角色,应用程序需调用 Amazon STS AssumeRole
API 操作并传递角色的 ARN 以供使用。该操作将使用临时凭证创建一个新会话。此会话与用于该角色的基于身份的策略具有相同的权限。
在调用 AssumeRole
时,您可以选择传递内联或托管会话策略。会话策略是高级策略,在以编程方式为角色或联合身份用户创建临时凭证会话时,这些策略将作为参数进行传递。您可以使用 Policy
参数传递单个 JSON 内联会话策略文档。您可以使用 PolicyArns
参数指定最多 10 个托管会话策略。生成的会话的权限是实体的基于身份的策略与会话策略的交集。如果需要为其他人提供角色的临时凭证,会话策略是非常有用的。他们可以在后续的 Amazon API 调用中使用角色的临时凭证来访问拥有该角色的账户中的资源。您使用会话策略授予的权限不能超过基于身份的策略允许的权限。要了解有关 Amazon 如何确定角色的有效权限的更多信息,请参阅策略评估逻辑。

在以 IAM 用户或已使用角色的 externally authenticated user(外部验证的用户(SAML 或 OIDC)身份登录后,您可以调用 AssumeRole
。您还可以使用角色链,它使用一个角色担任另一个角色。在以 Amazon Web Services 账户根用户身份登录时,您无法担任角色。
默认情况下,您的角色会话持续 1 小时。在使用 Amazon STS AssumeRole*
API 操作担任该角色时,您可以为 DurationSeconds
参数指定一个值。该值的范围在 900 秒 (15 分钟) 到角色的最大会话持续时间设置之间。要了解如何查看您的角色的最大值,请参阅查看角色的最大会话持续时间设置。
如果使用角色链,您的会话限制为最多 1 小时。如果您随后使用 DurationSeconds
参数提供大于 1 小时的值,操作将失败。
为了安全起见,管理员可以查看 Amazon CloudTrail 日志以了解已在 Amazon 中执行操作的人员。您的管理员可能会要求您在代入角色时指定源身份或角色会话名称。有关更多信息,请参阅 sts:SourceIdentity 和 sts:RoleSessionName:
以下 Python 示例使用 Amazon 的 Boto3 接口 (Amazon SDK for Python (Boto) V3AssumeRole
。它还介绍了如何使用 AssumeRole
返回的临时安全凭证列出拥有该角色的账户中的所有 Amazon S3 存储桶。
import boto3 # The calls to Amazon STS AssumeRole must be signed with the access key ID # and secret access key of an existing IAM user or by using existing temporary # credentials such as those from another role. (You cannot call AssumeRole # with the access key for the root account.) The credentials can be in # environment variables or in a configuration file and will be discovered # automatically by the boto3.client() function. For more information, see the # Python SDK documentation: # http://boto3.readthedocs.io/en/latest/reference/services/sts.html#client # create an STS client object that represents a live connection to the # STS service sts_client = boto3.client('sts') # Call the assume_role method of the STSConnection object and pass the role # ARN and a role session name. assumed_role_object=sts_client.assume_role( RoleArn="arn:aws:iam::
account-of-role-to-assume
:role/name-of-role
", RoleSessionName="AssumeRoleSession1" ) # From the response that contains the assumed role, get the temporary # credentials that can be used to make subsequent API calls credentials=assumed_role_object['Credentials'] # Use the temporary credentials that AssumeRole returns to make a # connection to Amazon S3 s3_resource=boto3.resource( 's3', aws_access_key_id=credentials['AccessKeyId
'], aws_secret_access_key=credentials['SecretAccessKey'
], aws_session_token=credentials['SessionToken'
], ) # Use the Amazon S3 resource object that is now configured with the # credentials to access your S3 buckets. for bucket in s3_resource.buckets.all(): print(bucket.name)