3

I want to send a welcome message (SMS) to phone number of my app's user when they will sign up using their phone number. I couldn't find official documentation for this particular task.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Ssubrat Rrudra
  • 870
  • 8
  • 20

3 Answers3

3

Amazon lets you do this. Assuming you're using Cognito for sign-up, you'll want to use the post-confirmation Cognito lambda trigger.

  1. Set up your SNS account via the AWS Console, to send SMS messages. Send yourself a test message via the console.

  2. Run amplify auth update

  3. When it gets to the question Do you want to configure Lambda Triggers for Cognito?, answer Yes and choose the Post Confirmation trigger

  4. You need to grant SNS (SMS) permissions to the lambda. Update the PostConfirmation-cloudformation-template.json file to add a new statement under Resources.lambdaexecutionpolicy.Properties.PolicyDocument.Statement:

    {
    "Resources": {
        "lambdaexecutionpolicy": {
            "Properties": {
                "PolicyDocument": {
                    "Statement": [
                        {
                            "Effect": "Allow",
                            "Action": "sns:*",
                            "Resource": "*"
                        }
                    ]
                    ...
                }
            ...
            }
        ...
        }  
    ...
    }
    ...
    }
    
  5. Use this code for the trigger:

    var aws = require('aws-sdk');
    
    var sms = new aws.SNS();
    
    exports.handler = (event, context, callback) => {
      console.log(event);
    
      if (event.request.userAttributes.phone_number) {
        sendSMS(event.request.userAttributes.phone_number, "Congratulations " + event.userName + ", you have been confirmed: ", function (status) {
    
          // Return to Amazon Cognito
          callback(null, event);
        });
      } else {
        // Nothing to do, the user's phone number is unknown
        callback(null, event);
      }
    };
    
    function sendSMS(to, message, completedCallback) {
      const params = {
        Message: message, /* required */
        PhoneNumber: to
      };
      sns.publish(params, function (err, data) {
        if (err) {
          console.log(err, err.stack); // an error occurred
        } else {
          console.log(data);
        }
        completedCallback("SMS Sent");
      })
    };
    
cobberboy
  • 5,598
  • 2
  • 25
  • 22
1

Not sure if sending SMS is a service, Amazon Amplify provides.

But you could use a service like Twilio to send SMS (and much more) to phones.

Markus S.
  • 2,602
  • 13
  • 44
1

AWS Amplify can help you setting up SMS, Email and Push notifications to your users by integrating with Amazon Pinpoint. Take a look at the documentation here: https://aws-amplify.github.io/docs/js/push-notifications.

Amazon Pinpoint allows you to create user segmentation, message templates, campaigns (with A/B testing and canary as well), Journeys (for email only so far), and so many more other things. You can integrate it and configure it using AWS Amplify, but some of those features I've mentioned are still not supported by AWS Amplify and you will have to either use the AWS Console to configure or use the AWS SDK to integrate with your app. You can leverage the AWS Amplify Auth module in order to get a valid Cognito token which will allow you to interact with Amazon Pinpoint directly.

Paulo Aragão
  • 169
  • 1
  • 4