加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python-2.7 – 如何在AWS Lambda函数中使用boto3通过AWS SNS发送

发布时间:2020-12-16 23:12:24 所属栏目:Python 来源:网络整理
导读:我想使用boto3发布方法从AWS Lambda函数发送短信,通过短信通知用户问题.我的lambda函数是用 Python编写的,我使用的是boto3模块.我的lambda函数具有SNS的完全权限.我有这个代码, sns = boto3.client('sns')sns.publish( PhoneNumber = '+11234567890',Message
我想使用boto3发布方法从AWS Lambda函数发送短信,通过短信通知用户问题.我的lambda函数是用 Python编写的,我使用的是boto3模块.我的lambda函数具有SNS的完全权限.我有这个代码,
sns = boto3.client('sns')
sns.publish(
    PhoneNumber = '+11234567890',Message = 'Simple text message'
)

根据boto3 documentation,publish方法接受以下参数,

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不是可能的参数.我怀疑我缺少一些明显简单的东西,但可以使用一些帮助.

我知道还有其他途径发送短信,如电子邮件网关和其他供应商提供的解决方案,如Twilio,但我想追求基于SNS的路由,并了解我在哪里出错.

解决方法

其实你的例子看起来不错这是我试过的
import boto3
sns = boto3.client('sns')
number = '+17702233322'
sns.publish(PhoneNumber = number,Message='example text message' )

像一个魅力一样工作我建议先使用配置有root帐户凭据的awscli,然后使用代码进行测试.一旦它的工作创建一个新用户,只需要你所需的权限,或者将其应用到实例角色.

您需要创建一个允许SNS:在资源上发布的策略:*(允许发送给所有人)或资源:’17702233322′(允许文本到特定的数字).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读