Python编写的Lambda函数调用AWS Pinpoint接口出现超时如何解决?
问题修复方案
修正后的代码
import json import boto3 import traceback # 客户端初始化放在函数外可复用连接,减少冷启动耗时 client = boto3.client('pinpoint') def trigger(event, context): print("---------1----------------") payload = { 'TraceId': 'sadfasdf', 'Context': { 'string': 'string' }, # 使用模板发送时不需要填RawContent,保留空配置即可 'MessageConfiguration': { 'GCMMessage': {} }, 'TemplateConfiguration': { 'PushTemplate': { 'Name': 'coll-1-en' } }, 'Users': { "id": { "Substitutions":{ "subject": ["string"], "id": ["120913"] } } } } try: print("---------2----------------") response = client.send_users_messages( # 此处请替换为你Pinpoint项目的真实ID ApplicationId='替换为实际的Pinpoint项目ID', SendUsersMessageRequest=payload ) print("---------3----------------") # boto3返回的是已解析的字典,不需要调用read、decode方法 print(response) return response except Exception as e: # 打印完整异常栈,方便定位问题 print(f"调用错误:{str(e)}") print(traceback.format_exc()) return False
超时问题排查方向
- 网络配置问题:如果Lambda部署在VPC环境中,默认没有公网访问权限,无法调用公网的Pinpoint服务。两种解决方案:1. 为Lambda所在私有子网配置NAT网关,开通公网访问能力;2. 在VPC内创建Pinpoint的Interface VPC端点,实现内网访问Pinpoint服务。
- IAM权限问题:检查Lambda执行角色的权限策略,是否包含
mobiletargeting:SendUsersMessages操作权限,且资源规则匹配你的Pinpoint项目ARN。你原有代码直接吞掉了所有异常,就算有权限错误也不会输出日志,修正后的代码会打印完整错误信息,可直接在CloudWatch日志中查看具体报错。 - 参数配置问题:确认
ApplicationId为Pinpoint项目的正确ID,避免填错项目标识导致请求异常。
内容的提问来源于stack exchange,提问作者Javier Gallego
相关产品推荐
相关产品推荐

