How to Send SMS Messages Using AWS Lambda, SNS, and Python
As a associate system administrator I worked on Redhat Linux servers, including user management, permissions, services, and performance monitoring Automated routine administrative tasks using Bash scripting and cron jobs, reducing manual effort by ~30% I am aws certified sysops administrator and Google Certified Cloud Engineer. Determined to transition my career into cloud architect /Cloud Support role
Introduction
This article explains how to send and receive SMS messages using AWS services. You will learn how to:
Receive SMS messages and log their content
Reply automatically to incoming messages (SMS echo)
Send scheduled SMS messages
All examples use AWS Lambda, SNS, and Amazon Pinpoint, with Python 3 as the programming language.
For simplicity, this guide uses the AWS Management Console (ClickOps). In real production environments, these resources should be managed using tools like Terraform, CloudFormation, CI/CD pipelines, and version control.
Why Use AWS Lambda for SMS?
AWS Lambda is a serverless compute service that lets you run code without managing servers.
Key benefits:
No server provisioning or maintenance
Fast deployment
Very low cost (first 1 million requests per month are free)
Scales automatically
Lambda works well for lightweight tasks such as processing SMS messages.
About SMS and Cost Considerations
SMS (Short Message Service) is widely supported but relatively expensive compared to internet-based messaging.
Typical costs (US region):
Receive SMS: ~$0.0075 per message
Send SMS: ~$0.00645 per message
For example:
Sending 1 SMS per day to 1,000 users can cost nearly $200/month
SMS is best used for:
One-time passwords (OTP)
Account verification
Critical alerts
Receiving SMS Messages
Step 1: Create a Lambda Function
Log in to AWS Console
Open AWS Lambda
Click Create Function
Choose:
Runtime: Python 3
Permissions: Create a new role with basic Lambda permissions
Create the function
Step 2: Add SNS as a Trigger
In the Lambda Designer, add SNS as a trigger
Allow SNS to invoke the Lambda function
Save the function
SNS will deliver incoming SMS messages to your Lambda function.
Step 3: Write Lambda Code to Log Messages
This basic function extracts:
Sender phone number
SMS text content and logs them to CloudWatch.
import json
def lambda_handler(event, context): message = json.loads(event['Records'][0]['Sns']['Message']) text = message['messageBody'] number = message['originationNumber']
return { 'statusCode': 200, 'body': json.dumps( f"Phone number: {number}\nMessage Text: {text}" ) }
Step 4: Test the Function
You can test the function without real SMS by creating a Lambda test event using the SNS template and providing a sample message payload.
Sending SMS Messages
To send SMS messages, AWS uses Amazon Pinpoint.
Step 1: Create a Pinpoint Project
Open Amazon Pinpoint
Create a new project
Enable SMS and Voice
Request a long code (dedicated phone number)
Long codes cost around $1 per month, so delete them when testing is complete.
Step 2: Update IAM Permissions
Add Pinpoint permissions to the Lambda execution role so the function can send SMS messages.
Step 3: Create an SMS Echo Function
This function replies with the same text it receives.
import json
import boto3
pinpoint = boto3.client('pinpoint')
def lambda_handler(event, context): message = json.loads(event['Records'][0]['Sns']['Message'])
pinpoint.send_messages( ApplicationId='YOUR_PINPOINT_APPLICATION_ID',
MessageRequest={ 'Addresses': { message['originationNumber']:
{'ChannelType': 'SMS'} },
'MessageConfiguration': { 'SMSMessage': { 'Body': message['messageBody'],
'MessageType': 'PROMOTIONAL' } } } )
This sends the reply using the Pinpoint long code.
Sending Scheduled SMS Messages:
You can also send SMS messages on a schedule using CloudWatch Eventbridge.
Daily reminders
Alerts
Notifications
Example
import json
import boto3
import random
pinpoint = boto3.client('pinpoint')
def lambda_handler(event, context): messages = [ "Sorry, I’m stuck in traffic.", "Running late due to car trouble.", "Had a family emergency this morning.", "Delayed due to work last night." ]
pinpoint.send_messages( ApplicationId='YOUR_PINPOINT_APPLICATION_ID', MessageRequest={ 'Addresses': { 'BOSS_PHONE_NUMBER': {'ChannelType': 'SMS'} }, 'MessageConfiguration': { 'SMSMessage': { 'Body': random.choice(messages), 'MessageType': 'PROMOTIONAL' } } } )
Trigger this function using a scheduled CloudWatch rule.
Summary:
In this guide, you learned how to use AWS services to send and receive SMS messages.
Services Used:
AWS Lambda – serverless function execution
SNS – receiving SMS messages
Amazon Pinpoint – sending SMS and managing phone numbers
IAM – permissions and security
CloudWatch – logs, monitoring, and scheduling