Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send an SMS via AWS SNS using boto3 in an AWS Lambda function?

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

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 (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.

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

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

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.

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.

like image 620
kelflanagan Avatar asked Jul 13 '16 17:07

kelflanagan


People also ask

How do I send a text on boto3?

Step 2: Send your message import boto3 # Create an SNS client client = boto3. client( "sns", aws_access_key_id="YOUR ACCES KEY", aws_secret_access_key="YOUR SECRET KEY", region_name="us-east-1" ) # Send your sms message. client. publish( PhoneNumber="+12223334444", Message="Hello World!" )

Can you invoke a Lambda function using AWS SNS notification?

Amazon SNS and AWS Lambda are integrated so you can invoke Lambda functions with Amazon SNS notifications. When a message is published to an SNS topic that has a Lambda function subscribed to it, the Lambda function is invoked with the payload of the published message.


Video Answer


1 Answers

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

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.

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

like image 144
Atifm Avatar answered Sep 18 '22 14:09

Atifm