AWS Lambda调用SQS SendMessage时X-Amzn-Trace-Id头部无效错误及修复
AWS Lambda调用SQS SendMessage报X-Amzn-Trace-Id无效错误的解决方案
该问题已修复,修复来自botocore的commit:315c8353228fa03e2d613a4f3d94119ed9c3c6b0
问题场景
- 基于Python 3.9的AWS Lambda函数,向标准SQS队列发送消息
- 通过AWS控制台「测试」功能或EventBridge定时任务触发时,调用
send_message接口报错 - 本地IDE运行相同代码无异常
代码示例
sqs.send_message( QueueUrl=queue_url, DelaySeconds=0, MessageAttributes={ 'Bucket': { 'DataType': 'String', 'StringValue': bucket } }, MessageBody=( 'Test') ) )
错误信息
[ERROR] ClientError: An error occurred (InvalidParameterValue) when calling the SendMessage operation: The request has a 'X-Amzn-Trace-Id' HTTP header which is reserved for AWS X-Ray trace header and has an invalid value 'Root%3D1-62e53a62-32fe44f063cc71297ba219f1%3BParent%3D161b7ec516b8d267%3BSampled%3D0' Traceback (most recent call last): File "/var/task/main.py", line 27, in lambda_handler sqs.queue_message(config['queue_url'], File "/var/task/sqs.py", line 9, in queue_message response = sqs.send_message( File "/var/task/botocore/client.py", line 508, in _api_call return self._make_api_call(operation_name, kwargs) File "/var/task/botocore/client.py", line 915, in _make_api_call raise error_class(parsed_response, operation_name)
使用的依赖版本
boto3==1.24.41 jmespath~=1.0.1 s3transfer~=0.6.0 brotlicffi~=1.0.9.2 cryptography~=37.0.4 six~=1.16.0 python-dateutil~=2.8.2 urllib3==1.26.11 awscrt==0.13.14 certifi~=2022.6.15 requests~=2.28.1
Lambda函数的SQS权限
"Action": [ "sqs:SendMessageBatch", "sqs:SendMessage", "sqs:ListQueueTags", "sqs:ListDeadLetterSourceQueues", "sqs:GetQueueUrl", "sqs:GetQueueAttributes", "sqs:ChangeMessageVisibilityBatch", "sqs:ChangeMessageVisibility" ], "Effect": "Allow", "Resource": "arn:aws:sqs:eu-central-1:<redacted>:test-function", "Sid": ""
解决方案
该错误是botocore处理X-Ray追踪头时的编码bug导致的,已在上述commit中修复。解决方法:
- 升级
botocore到包含该修复的版本(如>=1.27.42) - 或直接升级
boto3到对应兼容版本(如>=1.24.42),boto3会自动依赖匹配的botocore版本
内容的提问来源于stack exchange,提问作者Zucchini
相关产品推荐
相关产品推荐

