How to trigger AWS Lambda by SMS?
Tags: lambda, aws, sms, serverless
Published: 2020-04-18 21:39:30

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's 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. Did you get 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:#

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/9ca9fvckx4iemawbjejf.png)

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 we 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](https://docs.aws.amazon.com/pinpoint/latest/userguide/channels-sms-awssupport-long-code.html).

One point I want to bring attention to is some countries have 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.
- Open SNS console and from the left panel, select topics.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/2yijxvig9axt4muxij2v.png)

- Click on create topic.
- Fill the name for the topic and keep the default values the same.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/r3mmxetfjk54n5uz9e71.png)

- 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.
- Click on Create Role.
- Select Lambda from the use cases list and click next.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/od9a13wx6ohzw5czrd0e.png)

- In attach permission policies, search for SNS.
- Choose Read-Only Access from the list.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/7139kq1otbjc8eb4daro.png)

- 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
- In Dashboard, click on Create Function button.
- Fill the needed information and do not forget to select an existing role, which is the one we created.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/0wcg5o7i805uvp0mi55h.png)

- In designer part, click on Add Trigger.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/5zz55cb5jpla26h5kgbl.png)

- In trigger configuration, search for SNS and then look for the SNS topic we created earlier.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/tunpc35dl5n63zp21w75.png)

- 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.
- In Pinpoint main page, click on Manage Projects.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/xj5gx6jgoedtkug6kb6x.png)

- Create a new project.
- Skip Project Features for now.
- In Project Dashboard, click on Settings -> SMS and Voice.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/n7ueyazx85eo652mnong.png)

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

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/u6eqtl5r3i27nw8i1ig5.png)

- Go all the way down until you see Two-way SMS. Click on it.
- First thing, enable it.
- In incoming messages destination, choose an existing SNS topic, and select the one we created earlier.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/0epqpydorkrufud5jpvp.png)

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

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/qo61j8l8og08fib0bbkl.jpeg)

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

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/qc1amqytiy5qrrgnsr6t.png)

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