Automate text Message(SMS) notification using SNS and AWS lambda
Rashmitha v
Posted on July 13, 2024
Introduction:
Automating SMS notifications using AWS Lambda and SNS (Simple Notification Service) is a powerful way to keep users informed about important events or updates in your application. AWS Lambda allows you to run code without provisioning or managing servers, while SNS enables you to send messages to a large number of recipients simultaneously.
working:
It is a source where the file is uploaded, this s3 bucket is configurated to trigger AWS lambda. lambda contains a python code which which perform event reading mechanism and publish the fully framed messages to SNS topic.To the SNS topic we will make phone and become a subscriber to the SNS topic and msg is publishes that text will receive about the event.
Architecture
- AWS Amazon S3:
- provide the name to the bucket.
- create a bucket.
2.create a lambda function
- provide the function name.
- use the runtime as python 3.9
- use the existing role
to change the setting click the configurations
- change the memory size
- change the timeout to reduce the cost
- click save
- click trigger
- add trigger select 's3 bucket'
- click event types and select "all object create events".
- click add
paste the lambda code in the code source.
import boto3
topic_arn = ""
def send_sns(message, subject):
try:
client = boto3.client("sns")
result = client.publish(TopicArn=topic_arn, Message=message, Subject=subject)
if result['ResponseMetadata']['HTTPStatusCode'] == 200:
print(result)
print("Notification send successfully..!!!")
return True
except Exception as e:
print("Error occured while publish notifications and error is : ", e)
return True
def lambda_handler(event, context):
print("event collected is {}".format(event))
for record in event['Records'] :
s3_bucket = record['s3']['bucket']['name']
print("Bucket name is {}".format(s3_bucket))
s3_key = record['s3']['object']['key']
print("Bucket key name is {}".format(s3_key))
from_path = "s3://{}/{}".format(s3_bucket, s3_key)
print("from path {}".format(from_path))
message = "The file is uploaded at S3 bucket path {}".format(from_path)
subject = "Processes completion Notification"
SNSResult = send_sns(message, subject)
if SNSResult :
print("Notification Sent..")
return SNSResult
else:
return False
- deploy the code and test the code.
3.create the SNS topic
- click the standard SNS topic.
- provide a name to the SNS topic.
- create a topic
arn code is generated this code is pasted in the lambda function before deploying and testing the code.
4.create subscribers:
- provide the ARN
- select the protocol(SMS)
- SNS endpoint - phone number
- click on text messages(SMS)
- click add phone number in - 'sandbox destination phone number'
- add phone no. , select the country and verify.
the phone is number is verified.
- Subscribe Phone Numbers:
- create subscription
- choose topic.
- select sms phone number appears after verification.
upload a dummy file to the trigger point i.e S3 and notification pops as a text message sent to phone.
Posted on July 13, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
November 29, 2024