python-2.7 - 在 AWS Lambda 函数中使用 boto3 通过 AWS SNS 发送短信?

标签 python-2.7 sms amazon-sns boto3 aws-lambda

我想使用 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/

相关文章:

java - 如何将我的位置(经度和纬度)作为回复短信发送给其他人?

java - 收到消息时通知如何工作

python - 从 Bigquery 中读取几行作为辅助输入,得到 None

linux - 将 pandas 加载到 Slackware 14 上时遇到问题

python - Pytest中如何控制增量测试用例

amazon-web-services - AWS : Saving every SNS message

aws-lambda - 警报通知中可用的 AWS Cloudwatch 日志过滤参数

python - 正则表达式查找搜索词并将结果放入另一个数据文件中?

sms - 连接电脑和手机 - 阅读短信并使用它?

node.js - 从 lambda 向 sns 主题发布消息