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

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

问题描述

我想使用 boto3 发布方法从 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'
        }
    }
)

它需要一个消息"参数和文档中描述的以下三个参数之一:

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 (string) -- 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 (string) -- 您要发送到的电话号码一条短信.使用 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、>消息、主题、消息结构、消息属性".

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' )

像魅力一样工作.我建议首先使用配置了您的 root 帐户凭据的 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天全站免登陆