amazon-web-services - 请求中包含的安全 token 已过期

标签 amazon-web-services boto3

我有一个脚本可以从 Cloudwatch 中提取大量指标用于我们自己的内部报告。

该脚本迭代特定区域中的所有 EC2 实例,并询问过去 2 周的 5 个 cloudwatch 指标(所有可用的统计数据)(每 5 天间隔 5 分钟,正好是 1440 配额)。我正在使用一个假设的 session :

session = Session(aws_access_key_id=AWS_ACCESS_KEY_ID, aws_secret_access_key=AWS_SECRET_ACCESS_KEY, region_name=regionName)
sts = session.client('sts')
response = sts.assume_role(
    RoleArn=arn, # External role arn
    RoleSessionName='role-name',
    ExternalId='<some-id-here>',
)
tempAccessKeyId = response['Credentials']['AccessKeyId']
tempSecretAccessKey = response['Credentials']['SecretAccessKey']
tempSessionToken = response['Credentials']['SessionToken']
assumedSession = Session(
    aws_access_key_id=tempAccessKeyId,
    aws_secret_access_key=tempSecretAccessKey,
    aws_session_token=tempSessionToken,
    region_name=regionName)

在运行脚本时,我遇到了这个异常:
botocore.exceptions.ClientError: An error occurred (ExpiredToken) when calling the GetMetricStatistics operation: The security token included in the request is expired

有没有办法确保 token 在运行脚本时不会过期?我正在使用 boto3。

最佳答案

您正在使用的假设角色方法返回 临时安全凭证。以下内容摘自official documentation :

The temporary security credentials are valid for the duration that you specified when calling AssumeRole , which can be from 900 seconds (15 minutes) to 3600 seconds (1 hour). The default is 1 hour.



由于您没有使用 DurationSeconds关键字参数,返回的凭据在默认 1 小时内有效。您必须确保获得新凭据才能在 1 小时后提出请求。以下内容来自Temporary Security Credentials official documentation :

When (or even before) the temporary security credentials expire, the user can request new credentials, as long as the user requesting them still has permissions to do so.

关于amazon-web-services - 请求中包含的安全 token 已过期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35685729/

相关文章:

amazon-web-services - 为什么 Dynamodb.put 在 Lambda 函数中不起作用?返回空值。如何调试?

amazon-web-services - 具有证书 DNS 验证的 CloudFormation 堆栈托管区域删除

amazon-web-services - AWS CloudFormation 输出到 JenkinsFile 管道

linux - 如何在不重新启动 AWS EC2 的情况下获得新的公共(public) IP

aws-lambda - 从 aws api 网关执行 lambda 的脚本权限

amazon-web-services - 如何使用boto3在不同账户的安全组之间添加安全组规则?

python - 为什么 AWS 告诉我 BucketAlreadyExists 却没有?

mongodb - 使用先前的序列号或时间戳运行时,从 Kinesis 读取给出空记录

python - Boto3、AWS Lambda - 每次调用的结果复合

amazon-web-services - 重新启动AWS Lambda函数以清除缓存