使用 IAM Identity Center 命名配置文件 - Amazon Command Line Interface
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

使用 IAM Identity Center 命名配置文件

本主题介绍如何使用 Amazon CLI 通过 Amazon IAM Identity Center(IAM Identity Center)对用户进行身份验证,以获取用于运行 Amazon CLI 命令的凭证。

注意

您的凭证是临时还是自动刷新,这取决于您之前如何对配置文件进行配置。

先决条件

您配置了 IAM Identity Center 配置文件。有关更多信息,请参阅 IAM Identity Center 的具有自动身份验证刷新功能的令牌提供程序配置Amazon IAM Identity Center 的旧版不可刷新配置

登录和获取凭证

注意

登录过程可能会提示您允许 Amazon CLI 访问您的数据。由于 Amazon CLI 基于 SDK for Python 而构建,因此权限消息可能包含 botocore 名称的变体。

配置命名配置文件后,您可以调用它以从 Amazon 请求凭证。您必须检索和缓存一组凭证,然后才能运行 Amazon CLI 服务命令。要获取这些凭证,请运行以下命令。

$ aws sso login --profile my-dev-profile

Amazon CLI 打开原定设置浏览器并验证您的 IAM Identity Center 登录。

SSO authorization page has automatically been opened in your default browser. Follow the instructions in the browser to complete this authorization request. Successfully logged into Start URL: https://my-sso-portal.awsapps.com/start

如果您当前未登录 IAM Identity Center,则必须提供您的 IAM Identity Center 凭证。

如果 Amazon CLI 无法打开浏览器,则系统会提示您自行打开浏览器并输入指定的代码。

$ aws sso login --profile my-dev-profile Using a browser, open the following URL: https://device.sso.us-west-2.amazonaws.com/ and enter the following code: QCFK-N451

Amazon CLI 将您的原定设置浏览器打开(或您手动将选择的浏览器打开)到指定页面,然后输入提供的代码。然后,网页会提示您输入 IAM Identity Center 凭证。

缓存 IAM Identity Center 会话凭证。如果这些凭证是临时性的,则会包括过期时间戳,并且在凭证过期后,Amazon CLI 会请求您再次登录 IAM Identity Center。

如果您的 IAM Identity Center 凭证有效,则 Amazon CLI 使用它们安全地检索在配置文件中指定的用于 IAM 角色的 Amazon 凭证。

Welcome, you have successfully signed-in to the AWS-CLI.

在使用 aws sso login 命令的 --sso-session 参数登录时,您还可以指定使用哪个 sso-session 配置文件。

$ aws sso login --sso-session my-dev-session Attempting to automatically open the SSO authorization page in your default browser. If the browser does not open or you wish to use a different device to authorize this request, open the following URL: https://device.sso.us-west-2.amazonaws.com/ and enter the following code: QCFK-N451 Successfully logged into Start URL: https://cli-reinvent.awsapps.com/start

使用 IAM Identity Center 配置文件运行命令

您可以使用这些凭证通过关联的命名配置文件来调用 Amazon CLI 命令。以下示例显示该命令是在作为指定账户一部分的代入角色下运行的。

$ aws sts get-caller-identity --profile my-dev-profile { "UserId": "AROA12345678901234567:test-user@example.com", "Account": "123456789011", "Arn": "arn:aws:sts::123456789011:assumed-role/AWSPeregrine_readOnly_12321abc454d123/test-user@example.com" }

只要您登录 IAM Identity Center 并且这些缓存的凭证未过期,Amazon CLI 就会在需要时自动续订过期的 Amazon 凭证。但是,如果您的 IAM Identity Center 凭证过期,则必须通过再次登录您的 IAM Identity Center 账户来显式续订它们。

$ aws s3 ls --profile my-sso-profile Your short-term credentials have expired. Please sign-in to renew your credentials SSO authorization page has automatically been opened in your default browser. Follow the instructions in the browser to complete this authorization request.

退出您的 IAM Identity Center 会话

完成使用 IAM Identity Center 命名配置文件后,您可以选择不执行任何操作,而让 Amazon 临时凭证和您的 IAM Identity Center 凭证过期。但是,您也可以选择运行以下命令来立即删除 SSO 凭证缓存文件夹中的所有缓存凭证和基于 IAM Identity Center 凭证的所有 Amazon 临时凭证。这使得这些凭证不可用于将来的任何命令。

$ aws sso logout Successfully signed out of all SSO profiles.

如果稍后要使用 IAM Identity Center 命名配置文件之一来运行命令,则必须再次运行 aws sso login 命令(请参阅上一节)并指定要使用的配置文件。