☁️ AWS - Email notification system
Tags: terraform, aws, python, devops
Published: 2024-06-17 20:13:55

#Technologies:
☁️ AWS: 🪣 S3 bucket, ⚡️ lambda function, 🔔 SNS, 📬 SQS
🤖 IaC: 🏗️ Terraform
🔄 CD: 🛠️ GitLab


Project resume: Email notification system developed in AWS. Once a file is uploaded to an s3 bucket it triggers an event which calls a lambda function and notifies service subscribers that a file has been uploaded.

## 📌Requirements

- AWS [account](https://aws.amazon.com/free/)
- [VsCode](https://code.visualstudio.com/download)
- [GitLab](https://gitlab.com/users/sign_in)

## 🎯Workflow

1️⃣ Source code 📄
2️⃣ Lambda function ⚡️
3️⃣ Deploy to AWS through GitLab 🚀

---

## 1️⃣ Source code 📄
[Gitlab project](https://gitlab.com/sharker2/AWS-Projects/-/tree/main/Notification-system?ref_type=heads)

**Structure**
```
terraform
└── terraform
    ├── bucket
    │   ├── main.tf
    │   └── output.tf
    │   └── variables.tf
    ├── lambda
    │   ├── main.tf
    │   └── output.tf
    │   └── variables.tf
    │   └── lambda_assume_role_policy.json
    │   └── lambda_policy.json
    │   └── lambda_function.py
    │   └── lambda_function.zip
    ├── sns
    │   ├── main.tf
    │   └── output.tf
    │   └── variables.tf
    ├── sqs
    │   ├── main.tf
    │   └── output.tf
    │   └── variables.tf
    ├── main.tf
    └── providers.tf
```
It is organized into modules dedicated to different AWS services, each designed to serve a specific purpose and facilitate the management and scalability of the cloud environment.In addition, using Terraform to orchestrate these modules ensures a consistent and controlled deployment of the infrastructure, aligned with best practices for configuration management and agile development in the cloud.

## 2️⃣ Lambda function ⚡️

```python
import json
import boto3

# Initialize AWS clients
s3_client = boto3.client('s3')
sns_client = boto3.client('sns')
sqs_client = boto3.client('sqs')

def lambda_handler(event, context):
    # Define SNS topic ARN and SQS queue URL
    sns_topic_arn = 'arn:aws:sns:us-east-2:087243254862:notifcationsystem-bucket'
    sqs_queue_url = 'https://sqs.us-east-2.amazonaws.com/087243254862/notification'

    # Process S3 event records
    for record in event['Records']:
        # Print the entire event for debugging purposes
        print(event)

        # Extract S3 bucket and object information from the event record
        s3_bucket = record['s3']['bucket']['name']
        s3_key = record['s3']['object']['key']
        
        # Example: Prepare metadata to send to SQS
        metadata = {
            'bucket': s3_bucket,
            'key': s3_key,
            'timestamp': record['eventTime']
        }
        
        # Send metadata to SQS queue
        sqs_response = sqs_client.send_message(
            QueueUrl=sqs_queue_url,
            MessageBody=json.dumps(metadata)
        )
        
        # Example: Prepare notification message to send to SNS
        notification_message = f"New file uploaded to S3 bucket '{s3_bucket}' with key '{s3_key}'"
        
        # Publish notification message to SNS topic
        sns_response = sns_client.publish(
            TopicArn=sns_topic_arn,
            Message=notification_message,
            Subject="File Upload Notification"
        )

    # Return a success response
    return {
        'statusCode': 200,
        'body': json.dumps('Processing complete')
    }

```
3️⃣ GitLab workflow 🚀

- Add pipeline

![Click pipeline](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gtohuu18oh7o1h1gtm66.png)

- 🔑Add access key from AWS into secrets in GitLab(Create a User for this purpose)

- ▶️ Run

![running pipeline](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ainli13x389z5nz0bmv6.png)



---
<a href="https://linkedin.com/in/lesterdprez" rel="nofollow"><img src="https://camo.githubusercontent.com/d94940866c98cb4fca5783c4e8ac95776d2f52df6bbf3d5ab9e30d76836f30ae/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c696e6b6564496e2d2532333030373742352e7376673f6c6f676f3d6c696e6b6564696e266c6f676f436f6c6f723d7768697465" alt="LinkedIn" data-canonical-src="https://img.shields.io/badge/LinkedIn-%230077B5.svg?logo=linkedin&amp;logoColor=white" style="max-width: 100%;"></a>