在AWS Lambda函数中使用boto3通过AWS SNS发送SMS? [英] Send an SMS via AWS SNS using boto3 in an AWS Lambda function?

查看:317
本文介绍了在AWS Lambda函数中使用boto3通过AWS SNS发送SMS?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用boto3 publish方法从AWS Lambda函数发送SMS消息,以通过SMS通知用户问题.我的lambda函数是用Python编写的,并且我正在使用boto3模块.我的lambda函数具有SNS的完整权限.我有这段代码,

I would like to send an SMS message from an AWS Lambda function using the boto3 publish method to notify the user of issues via SMS. My lambda function is written in Python and I am using the boto3 module. My lambda function has full rights to SNS. I have this code,

sns = boto3.client('sns')
sns.publish(
    PhoneNumber = '+11234567890',
    Message = 'Simple text message'
)

根据boto3 文档,则publish方法接受以下参数,

According to the boto3 documentation, the publish method accepts the following parameters,

response = client.publish(
    TopicArn='string',
    TargetArn='string',
    PhoneNumber='string',
    Message='string',
    Subject='string',
    MessageStructure='string',
    MessageAttributes={
        'string': {
            'DataType': 'string',
            'StringValue': 'string',
            'BinaryValue': b'bytes'
        }
    }
)

它需要一个"Message"参数和文档中所述的以下三个参数之一:

It requires a "Message" parameter and one of the following three parameters as described in the docs:

TopicArn(字符串)-您要发布到的主题.

TopicArn (string) -- The topic you want to publish to.

如果未为TopicArn参数指定值,则必须 为PhoneNumber或TargetArn参数指定一个值.

If you don't specify a value for the TopicArn parameter, you must specify a value for the PhoneNumber or TargetArn parameters.

TargetArn(字符串)-TopicArn或EndpointArn,但不能同时使用.

TargetArn (string) -- Either TopicArn or EndpointArn, but not both.

如果未为TargetArn参数指定值,则必须 为PhoneNumber或TopicArn参数指定一个值.

If you don't specify a value for the TargetArn parameter, you must specify a value for the PhoneNumber or TopicArn parameters.

PhoneNumber(字符串)-您要传送到的电话号码 一条短信.使用E.164格式.

PhoneNumber (string) -- The phone number to which you want to deliver an SMS message. Use E.164 format.

如果未为PhoneNumber参数指定值,则必须 为TargetArn或TopicArn参数指定一个值.

If you don't specify a value for the PhoneNumber parameter, you must specify a value for the TargetArn or TopicArn parameters.

执行我的代码时,将返回参数验证错误.它指出,

When my code is executed a parameter validation error is returned. It states,

输入:"PhoneNumber"中的未知参数,必须为:TopicArn, TargetArn,>消息,主题,MessageStructure,MessageAttributes".

Unknown parameter in input: "PhoneNumber", must be one of: TopicArn, TargetArn, >Message, Subject, MessageStructure, MessageAttributes".

因此,文档似乎表明PhoneNumber是有效参数,但使用该参数时会发生错误,并且来自错误的反馈表明PhoneNumber不是可能的参数.我怀疑我缺少明显而简单的内容,但可以使用一些帮助.

So the documentation seems to indicate that PhoneNumber is a valid parameter, but when used, an error occurs and the feedback from the error indicates that PhoneNumber is not a possible parameter. I suspect I am missing something obvious and simple, but could use some help.

我知道还有其他发送SMS消息的途径,例如电子邮件网关和其他供应商提供的解决方案,例如Twilio,但是我想遵循基于SNS的路线并了解我哪里出了问题.

I know there are other avenues to send SMS messages such as email gateways and other vendor supplied solutions like Twilio, but I would like to pursue the SNS based route and understand where I have gone wrong.

推荐答案

实际上您的示例看起来正确.这是我尝试过的

Actually your example looks right. Here is what I tried

import boto3
sns = boto3.client('sns')
number = '+17702233322'
sns.publish(PhoneNumber = number, Message='example text message' )

像魅力一样工作.我建议先使用配置有您的根帐户凭据的awscli,然后将代码用于测试驱动器.工作正常后,要么仅创建具有所需权限的新用户,要么将其应用于实例角色.

Worked like a charm. I recommend using awscli configured with your root account credentials first and take the code for a test drive. Once its working either create a new user with just the rights you need, or apply it to an Instance role.

您需要创建一个允许SNS的策略:在资源上发布:*(允许向所有人发短信)或资源:"+ 17702233322"(允许向特定数字发短信).

You need to create a policy that allows SNS:Publish on resource:* (allow texting to everyone) or resource: '+17702233322' (allow text to a specific number).

这篇关于在AWS Lambda函数中使用boto3通过AWS SNS发送SMS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆