20

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.

Anand Tripathi
  • 14,556
  • 1
  • 47
  • 52
kelflanagan
  • 305
  • 1
  • 2
  • 8
  • Did this ultimately work for you? Your invocation seems right, as in it just worked for me – Atif Aug 19 '16 at 00:09
  • For future reference, I am receiving the same error with similar code that was working fine last week. I think this could be boto3 or AWS not handling an unrelated error properly. So the publish is failing when sent to AWS but is being seen as a bad parameter. But I'm not 100% sure that is the case. – TattdCodeMonkey May 07 '18 at 16:40

1 Answers1

34

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

Atif
  • 1,438
  • 16
  • 25
  • 1
    I didn't end up modifying my code at all; it worked as is. There must of been a boto3 update or something because where I use to get an invalid parameter error, I no longer receive anything, but goodness. Thanks for sending me back to my code to enable this feature. – kelflanagan Sep 17 '16 at 19:14
  • 1
    No worries. Yeah when I tried your code, it worked fine, so I figured something else must be the matter. Glad it worked in the end. – Atif Sep 19 '16 at 00:10
  • 1
    Hi @atifm I tried this code. I got 200 as the response code. But didn't received any text message. Can you suggest any possible reason or How to resolve this, please ? – Naveen May 27 '17 at 04:09
  • 1
    @Naveen The 200 means the api received your message. Check the logs to see if it sent or failed: http://docs.aws.amazon.com/sns/latest/dg/sms_stats.html – Atif May 31 '17 at 12:39
  • @Naveen Even I am facing the same issue. Did you resolve it? – sumanth shetty Dec 11 '20 at 10:37