我想使用 boto3 发布方法从 AWS Lambda 函数发送 SMS 消息,以通过 SMS 通知用户问题。我的 lambda 函数是用 Python 编写的,我使用的是 boto3 模块。我的 lambda 函数对 SNS 拥有完全的权利。我有这个代码,
sns = boto3.client('sns')
sns.publish(
PhoneNumber = '+11234567890',
Message = 'Simple text message'
)
根据 boto3 documentation ,发布方法接受以下参数,
response = client.publish(
TopicArn='string',
TargetArn='string',
PhoneNumber='string',
Message='string',
Subject='string',
MessageStructure='string',
MessageAttributes={
'string': {
'DataType': 'string',
'StringValue': 'string',
'BinaryValue': b'bytes'
}
}
)
它需要一个“消息”参数和文档中描述的以下三个参数之一:
TopicArn (string) -- The topic you want to publish to.
If you don't specify a value for the TopicArn parameter, you must specify a value for the PhoneNumber or TargetArn parameters.
TargetArn (string) -- Either TopicArn or EndpointArn, but not both.
If you don't specify a value for the TargetArn parameter, you must specify a value for the PhoneNumber or TopicArn parameters.
PhoneNumber (string) -- The phone number to which you want to deliver an SMS message. Use E.164 format.
If you don't specify a value for the PhoneNumber parameter, you must specify a value for the TargetArn or TopicArn parameters.
当我的代码被执行时,会返回一个参数验证错误。它指出,
Unknown parameter in input: "PhoneNumber", must be one of: TopicArn, TargetArn, >Message, Subject, MessageStructure, MessageAttributes".
所以文档似乎表明PhoneNumber是一个有效的参数,但是在使用时,会发生错误,并且错误的反馈表明PhoneNumber不是一个可能的参数。我怀疑我遗漏了一些明显而简单的东西,但可以使用一些帮助。
我知道还有其他发送 SMS 消息的途径,例如电子邮件网关和其他供应商提供的解决方案,如 Twilio,但我想追求基于 SNS 的路由并了解我哪里出错了。
最佳答案
实际上,您的示例看起来正确。这是我尝试过的
import boto3
sns = boto3.client('sns')
number = '+17702233322'
sns.publish(PhoneNumber = number, Message='example text message' )
像魅力一样工作。我建议首先使用配置了您的 root 帐户凭据的 awscli,然后将代码用于测试驱动器。一旦它工作,要么创建一个具有您需要的权限的新用户,要么将其应用于实例角色。
您需要创建一个策略,允许 SNS:Publish on resource:*(允许向所有人发送短信)或资源:'+17702233322'(允许向特定号码发送短信)。
关于python-2.7 - 在 AWS Lambda 函数中使用 boto3 通过 AWS SNS 发送短信?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38358733/