How to trigger AWS Lambda by SMS?

Mohammed Lutfalla
Apr 19 · 6 min read
Image for post
Image for post

In my last article I have placed a teaser that whether you can trigger Lambda by SMS. Today, We are going to do that! I am really excited to share this with you.

NOTE: This article resources are not fully covered in the free tier. Also, you cannot do it directly as there are some resources you will have to raise a ticket to get and adjust services limitations.

So, I have been waiting for over a month to finish this article and I postponed it because of the workload that we faced with working remotely and waiting to get the ticket resolved by AWS support team. Which by the way, they were really helpful even in the free plan support.

What are the involved parts?

There are three main involved part in this event, Customer Engagement, Application Integration and compute services to make this happen.

Customer Engagement:

Since we want to trigger a function via SMS, you will need a service or tool to get information from the user. Otherwise, how the function will get triggered?

In this part, we will use AWS Pinpoint. This service enables you to engage with customers through different channels like emails and transactional SMSs. Also, you can validate phone numbers if they are real too!

Application Integration:

We are working with SMS, which leads us to work with SNS. SNS is a service that enables you to organize and manage SMS process. Also, it can trigger Lambda too. You got the idea right?

Compute:

Since we want to do some computing processes for the SMS content, we will need a computing unit. The best and cheapest option is Lambda. Which is the reason for this article.

Diagram:

Image for post
Image for post

Straight forward scenario, We will contact Pinpoint through SMS, the message will be passed to SNS, which will be responsible for triggering Lambda. No rocket science here.

I am here to show you how-to not to describe the theory behind it. So, shall be begin?

1- Request a long/short code from Pinpoint:

Since we want to send an SMS to Pinpoint, it is required to have a code. To obtain one, Please follow the steps from the documentation here.

One point I want to bring attention to is some countries has both short and long code. But, as happened for me, which living in Kingdom of Bahrain, we have only long code, so far.

NOTE: It took a while for me to get the code as I was a basic support plan user and there is no default, fast way to obtain this code in my region. Apply for it in advance.

2- Create SNS topic to handle Pinpoint messages:

As we mentioned in the beginning, there is no way to invoke Lambda directly from Pinpoint, creating SNS topic is a must for this purpose.

  • From Services, look for SNS and click on it.
Image for post
Image for post
  • Click on create topic.
Image for post
Image for post
  • We are done with SNS!

3- Prepare IAM role for Lambda:

We will work with Lambda, and for that reason, let us make a proper role to consume SNS messages.

  • Open IAM console and click on Roles.
Image for post
Image for post
  • In attach permission policies, search for SNS.
Image for post
Image for post
  • Finish the steps by given the role a descriptive name.

4- Lambda Time!!:

Now, we can prepare the function that will consume the received SMS. Let’s start:

  • From Services, click on Lambda
Image for post
Image for post
  • In designer part, click on Add Trigger.
Image for post
Image for post
  • In trigger configuration, search for SNS and then look for the SNS topic we created earlier.
Image for post
Image for post
  • Done! This function will be triggered whenever SNS topic receives messages.

5- Pinpoint Configuration:

So, last step is here. Since we prepared all the resources, we need to configure the part the will trigger all the previous configurations.

  • From Services, click on Pinpoint.
Image for post
Image for post
  • Create a new project.
Image for post
Image for post
  • In SMS and voice page, under Number settings, click on the long/short code you have.

NOTE: This is the code you will get after you asked for in the first step.

Image for post
Image for post
  • Go all the way down until you see Two-way SMS. Click on it.
Image for post
Image for post
  • Done! easy right?

How to test?

So, you have everything in place and we need to trigger the function. Just send an SMS to the long/short code you have.

Image for post
Image for post

To validate that the function got triggered, check Lambda logs.

Image for post
Image for post

Any useful use-case for this event?

I was thinking, what benefit I will get from triggering Lambda this way, and I guess I have a pretty good reason.

Imagine that you have a marketing dashboard in an EC2 that it gets turned off after working hours. One day, a colleague from marketing department called you and asked you to turn it on for few hours as he has something urgent. Imagine with me opening your laptop if you have it with you, connect it to Wify, open the console, login, don’t forget you enabled MFA, ext… Why can’t you have a Lambda function that when you send a short SMS, will do the job for you! Of course, you can validate the number is yours when you try to trigger it.

Summary:

AWS Lambda is really great innovation. It amaze me every time I try a new thing to do with. Triggering a computing unit via SMS opens a whole new world of options and possibilities for sys admins, businesses and who like fun projects like me.

Until the next time, don’t forget to wash your hand and stay safe..

Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

Sign up for Best Stories

By Dev Genius

The best stories sent monthly to your email. Take a look

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Mohammed Lutfalla

Written by

An indiviual who looks way far to the future. Tech is the core of my soul and football is what makes me alive.

Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

Mohammed Lutfalla

Written by

An indiviual who looks way far to the future. Tech is the core of my soul and football is what makes me alive.

Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app