Send a Mass Text

Overview

Screenshot

In this section, a lambda function is created to send out an initial text to all registered guests (mass text) in the backend DynamoDB table. This function does not need two way communication so the API Gateway is not used. The lambda function will call Twilio's API through https directly from the function using Twilio credentials gathered in a previous section. A record of which guests have or have not been sent a text message will be tracked via the backend DynamoDB table.

CloudFormation (Required)

Step1: CloudFormation Quick Start

Click on the Launch Stack button in your desired region below. US West (Oregon) is the recommended region for this workshop if at re:Invent.

Important

Ensure that you are consistent with the selected region throughout this workshop. Deployments in multiple regions will lead to AWS services not communicating together.

Region Deploy
US West (Oregon) Deploy in us-west-2
US East (Ohio) - Coming Soon Deploy in us-east-2
US East (N. Virginia) - Coming Soon Deploy in us-east-1
EU (Ireland) - Coming Soon Deploy in eu-west-1

In a new tab you should be redirected to CloudFormations Quick create stack page as shown below.

Screenshot

For Stack name, the default reg-app-mass-text can be used or customized.

Screenshot

The default for the Parameters section will work as is.

  • Function Name - Name for the lambda function.
  • Backend Table Name - Must match the original DynamoDB tables created in previous section.
  • Frontend Table Name - Must match the original DynamoDB tables created in previous section.
  • Twilio Account Sid - The Twilio Account Sid captured in a previous section. Ensure no trailing/leading spaces.
  • Twilio Auth Token - The Twilio Auth Token captured in a previous section. Ensure no trailing/leading spaces.

Screenshot

On the next window, scroll to the bottom and under Capabilities check I acknowledge that AWS CloudFormation might create IAM resources. and click Create stack.

Screenshot

This CloudFormation template will take about a minute to complete. Ensure that CREATE_COMPLETE is shown before building any additional CloudFormation templates.

Send Mass Text (Required)

Step2: Inspect the Lambda Function

Take a quick look at the lambda function created in this step.

In the services tab from the AWS Console, go to Lambda.

Screenshot

On the left under AWS Lambda go to Functions.

Screenshot

Click on the lambda function created in the prior step. The default is reg-app-mass-text.

Screenshot

There is no need to go through the entire lambda function at this time, but there are a few important things to point out.

First are the environment variables section as shown below. You will notice that those where the variables entered during CloudFormation build.

# environment variables
twilio_account_sid = os.environ.get('TWILIO_ACCOUNT_SID')
twilio_auth_token = os.environ.get('TWILIO_AUTH_TOKEN')
backend_dbtable = os.environ.get('BACKEND_DBTABLE')

Another important part of the code is the Twilio url and parameters section. These are needed to send a message to the Twilio API which will then forward to SMS. Take a look at the code to find the source of the three parameters.

# insert Twilio Account SID into the REST API URL
populated_url = twilio_sms_url.format(twilio_account_sid)
post_params = {"To": to_number, "From": from_number, "Body": body}
Customize Option

If you would like to customize the text message that will be sent to your guests, find the variable named body in the lambda function. The output can be completely customized or simply replace the TODOs to your company and event name.

Step3: Send Mass Text to Guests

There are many ways to execute this lambda function. For simplicity, a simple test will be executed from the console.

Important

Once you Test this function, a text message will be sent to the phone number provided in a previous section. Do not test the lambda function if you do not own (and have not been given consent) to send/receive a SMS message from the provided phone number. If you do own the number or have consent then please proceed.

On the top right click Test to invoke the function. Due to this being the time the function is tested, it needs to be configured. The default Hello World test is fine for this function. Give the test an Event name and click Create.

Again on the top right, click Test.

Screenshot

Under Execution result click the dropdown Details

Screenshot

You should see an output like below.

"{\"SMSOutNumber\": \"1\", \"SMSErrorNumber\": \"0\", \"SMSSkippedNumber\": \"4\"}"

Ensure that you have at least 1 SMSOutNumber before proceeding.

Recieve Mass Text (Required)

Step4: Check your phone

Check your phone. You should have reviced a text from your twilio number. Do Not reply just yet, let's set up the text bot and API so we can talk back and forth to Twilio.

Screenshot

Success

You have now completed sending the first text to your guests! This was done from a single lambda function and Twilio Credentials.