🔍 Overview

In this article, we’ll walk through using AWS Lambda to automate:

  • S3 Cross-Region Replication (CRR) monitoring and control,
  • EC2 instance start/stop operations, and
  • Email notifications using SNS (Simple Notification Service).

By the end, you’ll have a practical automation flow using Lambda functions and SNS to control key AWS services.


☁️ Why Automate with Lambda + SNS?

  • Serverless & scalable
  • Reduced operational overhead
  • Real-time alerts & monitoring
  • Cost-effective

🧰 Prerequisites

  • AWS Account
  • IAM Role with permissions for:
    • Lambda, SNS, S3, EC2
  • Basic understanding of Python (for Lambda)
  • An email subscribed to an SNS topic

📁 S3 CRR Monitoring with Lambda

🔧 Use Case:

Detect new object uploads in source bucket and log CRR status.

🔗 Setup:

  1. Create a Source and Destination Bucket
  2. Enable CRR on Source Bucket
  3. Set up Event Notification to trigger Lambda on s3:ObjectCreated:*

🧠 Lambda Code (Python):

import boto3
import json

def lambda_handler(event, context):
    s3 = boto3.client('s3')
    sns = boto3.client('sns')
    for record in event['Records']:
        bucket = record['s3']['bucket']['name']
        key = record['s3']['object']['key']

        message = f"New object {key} added to {bucket}. Check CRR status."
        print(message)

        sns.publish(
            TopicArn='arn:aws:sns:your-region:your-account-id:YourTopic',
            Message=message,
            Subject='[CRR Alert] S3 Object Created'
        )

    return {
        'statusCode': 200,
        'body': json.dumps('Notification sent!')
    }

🖥️ EC2 Instance Control with Lambda

🔧 Use Case:

Start/Stop EC2 instance at specific schedule or on demand with alert.

🧠 Lambda Code:

import boto3

ec2 = boto3.client('ec2')
sns = boto3.client('sns')

def lambda_handler(event, context):
    instance_id = 'i-xxxxxxxxxxxxxxx'
    action = 'start'  # or 'stop'

    if action == 'start':
        ec2.start_instances(InstanceIds=[instance_id])
        message = f"Started EC2 instance {instance_id}"
    else:
        ec2.stop_instances(InstanceIds=[instance_id])
        message = f"Stopped EC2 instance {instance_id}"

    sns.publish(
        TopicArn='arn:aws:sns:your-region:your-account-id:YourTopic',
        Message=message,
        Subject='[EC2 Alert]'
    )

    return {'message': message}

📣 Setting Up SNS Email Notifications

  1. Create a new SNS topic
  2. Add an email subscription
  3. Confirm email from your inbox
  4. Use TopicArn in Lambda

🔒 IAM Role & Permissions

Make sure your Lambda execution role has:

{
  "Effect": "Allow",
  "Action": [
    "sns:Publish",
    "s3:GetObject",
    "ec2:StartInstances",
    "ec2:StopInstances"
  ],
  "Resource": "*"
}

🧪 Testing & Logging

  • Use CloudWatch Logs to monitor Lambda output.
  • Trigger Lambda manually or via events.
  • Watch for email notifications to verify SNS integration.

✅ Final Thoughts

You’ve now built a powerful automation setup using AWS Lambda that can:

  • Track and notify S3 CRR operations,
  • Control EC2 instances,
  • And keep you in the loop via SNS email alerts.

Keep building, keep automating. You’re one step closer to architecting secure and efficient cloud systems

🙌 Let’s Connect!

Feel free to reach out or drop questions. I’d love to help!
🔹 GitHub: LeonardKachi
🔹 LinkedIn: onyedikachi-obidiegwu
🔹 Twitter/X: leonard_kachi