Automate text Message(SMS) notification using SNS and AWS lambda
Published: 2024-07-13 20:23:05

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jm119qs4bpcfx9vcqzfj.png)

1. **AWS Amazon S3:**

- provide the name to the bucket.
- create a bucket.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2vp4nrt3uvyz9objac5v.jpg)

2.**create a lambda function**

- provide the function name.
- use the runtime as python 3.9
- use the existing role


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k1no6s30ya9blk1aj41s.jpg)

to change the setting click the configurations 

- change the memory size
- change the timeout to reduce the cost
- click save

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/72ak6717bdhhu04yuibl.jpg)

- click trigger
- add trigger select 's3 bucket' 
- click event types and select "all object create events".
- click add

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z2bk9hfhdtin01hb5af4.jpg)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k9jsjkrijrgqlxiei6k1.jpg)


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)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x30m6rfzg9640qgst94g.jpg)


- click add phone number in - 'sandbox destination phone number'
- add phone no. , select the country and verify.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xjmo6jseoyfothc1gux0.jpg)

the phone is number is verified.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pj90pw78zgwnvvv9660d.jpg)

5. **Subscribe Phone Numbers:**

- create subscription

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/133c0wagg1r731m78vsc.jpg)


- choose topic.
- select sms phone number appears after verification.
 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5wkmrllxtzxbev94aw35.jpg)

upload a dummy file to the trigger point i.e S3 and notification pops as a text message sent to phone.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y1vfqwe6i4u7mp3xni88.jpg)