Skip to main content

Creating a Serverless Text Message Reminder Service in AWS using Lambda

Hands-On Lab

 

Photo of

Training Architect

Length

01:00:00

Difficulty

Beginner

In this hands-on lab, we will deploy a serverless reminder service using AWS Lambda. Specifically, we'll deploy a custom Lambda function (with Python code provided) that sends periodic reminders to your phone via text messages. Download the Python code here.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Creating a Serverless Text Message Reminder Service in AWS using Lambda

In this live AWS environment, we will deploy a serverless text message reminder service using AWS Lambda. We'll deploy a custom Lambda function (with the Python code provided) that sends periodic reminders to a phone via text messages. To do this, we will set up the function and then connect a CloudWatch trigger to it so that it sends reminders on a specific schedule.

Before we begin, let's get the Python code from here and log in to our instance using the cloud_user credentials provided by the lab. Make sure you are in us-east-1 (Northern Virginia) when working on this activity.

Introduction to the AWS Environment

To begin, use the Services tab and select Lambda from the Compute section. We ought to have a second browser tab open for the Simple Notification Service (SNS) page. To open it, use the Services dropdown and open Simple Notification Service from the Application Integration section in a new tab or window. With these pages up and ready to go, we can begin.

Create a Lambda Function and SNS Topic

In the Lambda screen, select Create a Function. Choose Author from scratch. In the Function Name section, let's call it MyLambda. Then let's select Python 3.6 from the Runtime dropdown. Leave the Execution role set to Use an existing role, and in the (Existing role dropdown, choose the one that was created for us called LambdaRuntimeRole.

With everything set, select Create function. On the lower part of the next page, we will find a Function code section. Over on the GitHub page, let's click the Raw button. We'll land on a page that's completely blank, except for the Python code. Copy that. Back over in the Lambda page, delete the information in the lambda_function tab, then paste in the GitHub code.

With our information pasted, we need to update the Handler section. By default, we see lambda_function.lambda_handler. We need to change this to lambda_function.handler, as the def line in our Python code only says handler. Click Save.

We need to make one more change to the code. On the line that says PhoneNumber, replace the number with our phone number. We need to make sure we have the country code and area code present. This is so we get a text message to our phone to double check that our reminder is working correctly. Once we've entered that, select Save and then Test.

On the Configure test event window, we need to create a test event. Use the Hello World template, give the event the name TestEvent, and leave the rest of the default information. Click Create. On our function page, select the Test button again. We will get a text message with the message designated in our Function code section:

    Message=(
        'Hello! This is your reminder to '
        'schedule an AWS Certification Exam!'

With that message, we know that we have created our Lambda function correctly, and can move on to the next portion of the lab.

Set up a CloudWatch Scheduled Event Trigger

Now, we need to update the code we put into the Function code section. We no longer want the message to only send to our phone number, but to a Topic Amazon Resource Name (ARN).

Create an Amazon Resource Name

Remember that other tab we opened for the Simple Notification Service? This is where we will start to use it. We may land on a sort of "Greeting" page instead of going right to the SNS dashboard. If we do, we can just click the Get started button. On the SNS dashboard page, select Create topic. Enter SMSTopic for both a Topic name and Display name, then select Create a topic. On the Topic details: SMSTopic page, copy the entire string from the Topic ARN. This string is unique, so ours will be different than what's in the Linux Academy training video.

Switch back over to our Lambda tab, and replace the PhoneNumber line with a new TopicARN line, along with the the string of information we just copied. It should look something like this when we're done:

def handler(event,context):
    sns.publish(
        TopicArn='arn:aws:sns:us-east-1:OURUNIQUENUMBER:SMSTopic',
        Message=(
            'Hello! This is your reminder to '
            'schedule an AWS Certification Exam!'

Select Save.

Create an SMS Subscription.

Now, we need to create a subscription for this information to go to. To do that, we need to go back to the AWS SNS tab. Here, in the Subscriptions section, select Create subscription. Paste the Topic ARN in (the same ID we just pasted into our Python code), and set the Protocol section to SMS. In the Endpoint section, we need to enter in our phone number. Make sure we have the country and area codes, and dashes between the numbers. For example: 1-800-555-5555. After entering a number, select Create Subscription.

Once we're back out on the Subscriptions page, click on the Topic ARN in the new subscription we created. On the next page, make sure our subscription is selected, then click on the Publish to Topic button. In here, add information to the Subject line ("Hi" will work) and a Message, like "Hello." We can leave Message format alone. Once we select Publish Message, this will send a message to our phone.

Test our SMS Subscription

Now that we have everything set up, we can test out our SMS. First, to make sure we are getting it from our subscription, add "- From SNS Subscription" to our message. Back in the Lambda page, add it to the Message line:

        Message=(
            'Hello! This is your reminder to '
            'schedule an AWS Certification Exam! - From SNS Subscription'

This will be added to the end of the message just to make sure we have the correct information. Click Save.

Add a Trigger

With the knowledge that our function is working, we need to add a trigger. This is what will schedule our Lambda function to send out the reminder.

In the Designer section (near the top of our Lambda page), select CloudWatch Events from the list of triggers on the left. It should show up on the right-hand side. Below that, in the Configure triggers section, select Create a new rule from the Rule dropdown. Name the rule Minute and set the Rule Description to Every minute. Continue by making sure the Schedule expression is selected. Then enter the expression rate(1 minute). We are using a shorter timeframe so that we can test the rule quickly. Next, make sure Enable Trigger is checked, and then click Add.

Back on our function page, select Save. A section for CloudWatch Events appears in the lower part of the screen, with the Minute rule we created. Make sure that the slider is set to Enabled. Once we save, wait for the message. Once we get the message from the function, disable the rule. If we don't do this, we will continue to get message every minute until the lab ends.

Conclusion

In this lab, we created a Lambda function that allowed us to send SMS messages to our phone, an SMS subscription to use with that function, and a CloudWatch trigger that sent that message to us every minute. Let's make sure to use this new knowledge to our advantage, and set reminders on when to start our next lab! Or for our favorite T.V. show.

Congratulations on completing the lab!