How to trigger AWS Lambda by SMS?
Mohamed Latfalla
Posted on April 18, 2020
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:
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.
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.
- Click on create topic.
- Fill the name for the topic and keep the default values the same.
- 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.
- In attach permission policies, search for SNS.
- Choose Read-Only Access from the list.
- 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.
- In designer part, click on Add Trigger.
- In trigger configuration, search for SNS and then look for the SNS topic we created earlier.
- 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.
- Create a new project.
- Skip Project Features for now.
- In Project Dashboard, click on Settings -> SMS and Voice.
- 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.
- 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.
- 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.
To validate that the function got triggered, check Lambda logs.
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..
Posted on April 18, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.