如何解决调用 AssumeRole 操作时发生错误 (InvalidClientTokenId):请求中包含的安全令牌无效
首先我想指出这与 this similar question 不是同一个问题。我已经浏览了所有答案,但没有一个对我的情况有实际帮助。
我有一个必须在容器内执行的 Python 脚本(我们将把它作为封装的解决方案提供给一些客户在本地运行),我必须在其中使用 boto3 发送电子邮件。
这是它的代码:
from os import stat
import boto3
from botocore.config import Config
import botocore.exceptions
class EmailHandler:
def __init__(self,business_rules,integrations,credentials,event):
self.business_rules = business_rules
self.integrations = integrations
self.credentials = credentials
self.event = event
def send_message(self,message):
region_name = 'us-east-1'
role_arn = self.credentials['role_arn']
my_config = Config(
region_name = region_name,signature_version = 'v4',retries = {
'max_attempts': 10,'mode': 'standard'
},)
sts = boto3.client('sts',aws_access_key_id=self.credentials['AWS_ACCESS_KEY_ID'],aws_secret_access_key=self.credentials['AWS_ACCESS_KEY_ID'])
response = sts.assume_role(
RoleArn=role_arn,RoleSessionName = 'SES_operation'
)
tempAccessKeyId = response['Credentials']['AccessKeyId']
tempSecretAccessKey = response['Credentials']['SecretAccessKey']
tempSessionToken = response['Credentials']['SessionToken']
client = boto3.client('ses',aws_access_key_id = tempAccessKeyId,aws_secret_access_key = tempSecretAccessKey,aws_session_token = tempSessionToken,config=my_config)
subject = self.event['alert_message']
to_email = self.credentials['email_receivers']
status = 900
state = 'failure'
data = {'message': ''}
try:
response = client.send_email(
Source= self.credentials['source_email'],Destination= {
'ToAddresses': [
to_email,]
},Message={
'Subject': {
'Data': subject,},'Body': {
'Text': {
'Data': message,'Html': {
'Data': '<p>'+ message +'</p>'
}
}
},SourceArn = self.credentials['source_identity'],)
state = 'success'
status = 200
data = {'message': response.MessageId }
except botocore.exceptions.ClientError as error:
data = {'message': error.response['Error']['Message']}
raise error
return(status,state,data)
如您所见,我正在尝试担任角色,以便我可以使用提供的会话令牌为 SES 创建客户端。
问题是每当执行此处理程序时我都会收到以下错误:
调用 AssumeRole 操作时发生错误 (InvalidClientTokenId):请求中包含的安全令牌无效。
但我没有提供任何令牌,因为我的目标正是使用此方法调用来获取它。 我错过了什么吗?
PS:正如您在代码中看到的那样,我无法使用配置文件,凭据是从外部来源传递的。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。