Automated Reporting System

Published: (December 31, 2025 at 10:49 AM EST)
3 min read
Source: Dev.to

Source: Dev.to

Overview

  • Use AWS Lambda and other serverless tools to automate daily reporting.
  • The system will:
    • Collect data from multiple sources.
    • Generate a PDF report.
    • Send the report via email.
    • Trigger this process daily.

This guide assumes you have a basic understanding of AWS.

Step 1: Create an S3 Bucket

  1. Go to the S3 service.
  2. Click Create bucket.
  3. Name your bucket (e.g., daily-reports-s3-bucket).
  4. Keep other settings at their defaults and click Create.

The bucket will store the reports if needed.

Step 2: Verify an Email Address in SES

  1. Open Amazon SES.
  2. Navigate to Email Addresses under Identity Management.
  3. Click Verify a New Email Address and enter your email.
  4. Check your inbox and click the verification link.

Verification allows SES to send emails from this address.

Step 3: Write the Lambda Function

  1. Go to AWS Lambda and click Create function.
  2. Choose Author from scratch.
    • Function name: daily-report-lambda
    • Runtime: Python 3.9 (or later)
  3. Click Create function.

Code for the Lambda Function

Replace the default code with the following:

import boto3
from fpdf import FPDF
import os
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders

def lambda_handler(event, context):
    try:
        # Generate the PDF report
        pdf = FPDF()
        pdf.add_page()
        pdf.set_font("Arial", size=12)
        pdf.cell(200, 10, txt="Daily Report", ln=True, align="C")
        pdf.cell(200, 10, txt="This is your automated report.", ln=True)

        # Save the PDF
        report_path = "/tmp/daily_report.pdf"
        pdf.output(report_path)

        # Upload to S3 (Optional)
        s3 = boto3.client('s3')
        bucket_name = "daily-reports-s3-bucket"
        s3.upload_file(report_path, bucket_name, "daily_report.pdf")

        # Send email via SES with attachment using raw email
        ses = boto3.client('ses')
        msg = MIMEMultipart()
        msg['From'] = "sender_email@gmail.com"      # Replace with your SES‑verified email
        msg['To'] = "receiver_email@gmail.com"      # Replace with recipient email
        msg['Subject'] = "Daily Report"

        # Attach the text body
        body = MIMEText("Please find the attached report.", 'plain')
        msg.attach(body)

        # Attach the PDF report
        with open(report_path, 'rb') as file:
            part = MIMEBase('application', 'octet-stream')
            part.set_payload(file.read())
            encoders.encode_base64(part)
            part.add_header('Content-Disposition',
                            f'attachment; filename={os.path.basename(report_path)}')
            msg.attach(part)

        # Send the email using SES's send_raw_email
        response = ses.send_raw_email(
            Source=msg['From'],
            Destinations=[msg['To']],
            RawMessage={'Data': msg.as_string()}
        )

        return {"status": "success", "response": response}

    except Exception as e:
        return {"status": "error", "message": str(e)}

Step 4: Install Libraries Locally and Deploy (Optional)

The Lambda runtime does not include fpdf, so package it yourself:

pip install fpdf -t ./package
cd package
zip -r ../lambda_layer.zip .

Upload the resulting ZIP (lambda_layer.zip) to your Lambda function as a layer or directly as the function code.

Step 5: Set Up EventBridge Trigger

  1. Open EventBridge.
  2. Click Create rule.
    • Name: daily-report-trigger
    • Define pattern: Schedule
    • Cron expression: cron(0 9 * * ? *) (9:00 AM UTC daily)
  3. Under Target, select your Lambda function (daily-report-lambda).
  4. Click Create.

Step 6: Test the Lambda Function

  1. In the Lambda console, go to the Test tab.
  2. Create a new test event (you can leave the default template blank).
  3. Click Test.

Check the recipient inbox for the generated PDF report.

You’ve built a serverless reporting system! 🎉

Happy Cloud Learning

— Adeel Abbas

Back to Blog

Related posts

Read more »

The RGB LED Sidequest 💡

markdown !Jennifer Davishttps://media2.dev.to/dynamic/image/width=50,height=50,fit=cover,gravity=auto,format=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%...

Mendex: Why I Build

Introduction Hello everyone. Today I want to share who I am, what I'm building, and why. Early Career and Burnout I started my career as a developer 17 years a...