work with us

Automate AWS EC2 Backups to S3 Using Lambda – Step-by-Step Guide

 

Raise your hand if you’ve ever lost sleep worrying about forgetting to back up your EC2 instance.  Let’s fix that with automated AWS backups that work while you sleep!

🚀 Why Automate Backups?

  • 🚨 Disaster Avoidance: Survive “Oops, I deleted the wrong thing” moments
  • ⏰ Reclaim Your Time: No more 2 AM manual backups
  • 📉 Cost Control: S3 + auto-delete = budget-friendly
  • 🔒 Audit-Ready: Proof of backups without spreadsheet chaos

🛠️ What You’ll Need

  • AWS account (Free Tier works!)
  • One EC2 instance you care about
  • 10 minutes and coffee ☕

🔧 Step-by-Step Setup

Step 1: Create Your S3 Backup Vault

  1. Go to S3 Console
  2. Click Create bucket
  3. Name: ec2-backup-vault-[your-initials]
  4. Keep Block Public Access ENABLED
  5. Click Create bucket

Step 2: Create IAM Role

  1. Go to IAM Roles
  2. Create role → Choose Lambda
  3. Attach policies:
    • AmazonEC2FullAccess
    • AmazonS3FullAccess
  4. Name: LambdaBackupMaster

Step 3: Create Lambda Function


import boto3
from datetime import datetime

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

def lambda_handler(event, context):
    instance_id = 'i-0123456789abcdef0'  # ← Replace!
    volume_id = 'vol-0123456789abcdef0'  # ← Replace!
    
    snapshot = ec2.create_snapshot(
        VolumeId=volume_id,
        Description=f"Autobackup {instance_id} - {datetime.utcnow().strftime('%Y-%m-%d')}"
    )
    
    s3.put_object(
        Bucket='ec2-backup-vault-[your-initials]',  # ← Your bucket name
        Key=f'backup-log/{instance_id}-{datetime.utcnow().isoformat()}.txt',
        Body=f"Backup created at {datetime.utcnow()}:\n{str(snapshot)}"
    )
    
    return {'status': '💪 Backup successful. Go enjoy your day.'}

Step 4: Schedule Daily Backups

  1. In Lambda, click Add trigger
  2. Choose EventBridge (CloudWatch Events)
  3. Create new rule: DailyBackupAlarm
  4. Schedule: rate(1 day)

Step 5: Auto-Delete Old Backups (Optional)

  1. Go to S3 bucket → Management
  2. Create lifecycle rule: 30-DayCleanup
  3. Set expiration: 30 days

🔒 Secure IAM Policy (Bonus)


{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "SnapshotCreateAndS3Log",
      "Effect": "Allow",
      "Action": [
        "ec2:CreateSnapshot",
        "ec2:DescribeVolumes"
      ],
      "Resource": "*"
    },
    {
      "Sid": "WriteToSpecificBucket",
      "Effect": "Allow",
      "Action": "s3:PutObject",
      "Resource": "arn:aws:s3:::ec2-backup-vault-[your-initials]/*"
    }
  ]
}

🎉 You’re Done! Now:

  1. Test your Lambda function
  2. Check S3 for backup logs
  3. Verify EC2 snapshots

 

🚀 How to Set Up CI/CD Pipeline with GitHub Actions for Your Web App (2025 Guide)

Imagine pushing code to your repo… and bam! — it’s tested, built, and deployed automatically. That’s what CI/CD is for. And the best part? GitHub Actions is free for most teams and works straight from your repo.

🔧 What You’ll Need

Tool Purpose
GitHub repo Your code lives here
Hosting (like Vercel, Netlify, DigitalOcean) Where your app gets deployed
SSH access or deploy token For production deployment (if not using Netlify)

🧑‍💻 Step-by-Step: GitHub Actions for CI/CD

✅ Step 1: Create Workflow File

mkdir -p .github/workflows
touch deploy.yml

✅ Step 2: Add Your Workflow Script

name: 🚀 Deploy to Production

on:
  push:
    branches:
      - main

jobs:
  deploy:
    runs-on: ubuntu-latest

    steps:
      - name: 🧹 Checkout Code
        uses: actions/checkout@v3

      - name: 🛠️ Set up Node
        uses: actions/setup-node@v3
        with:
          node-version: '18'

      - name: 📦 Install Dependencies
        run: npm install

      - name: 🧪 Run Tests
        run: npm test

      - name: 🚚 Deploy to Server
        uses: appleboy/ssh-action@master
        with:
          host: ${{ secrets.SERVER_HOST }}
          username: ${{ secrets.SERVER_USER }}
          key: ${{ secrets.SERVER_SSH_KEY }}
          script: |
            cd /var/www/myapp
            git pull origin main
            npm install
            pm2 restart app

🔐 Step 3: Set GitHub Secrets

Go to your GitHub repo → Settings → Secrets and Variables → Actions and add:

  • SERVER_HOST – your server IP
  • SERVER_USER – e.g. ubuntu
  • SERVER_SSH_KEY – your private key (no passphrase)

🎉 What Happens Now?

Any time you push code to main:

  • GitHub pulls code
  • Installs dependencies
  • Runs tests
  • SSH connects and deploys

💡 Bonus: Using Vercel or Netlify?

Even easier. Just connect your GitHub repo, and it auto-deploys on push. No SSH needed!

❓ Common Questions

🤔 What if my build takes too long?
Use actions/cache to speed it up. GitHub gives 2,000 free CI/CD minutes/month.
💥 What if my server has downtime?
Use PM2 or Docker rolling deployments for zero-downtime updates.
🛡️ Is GitHub Actions secure?
Yes — just store sensitive values using GitHub secrets.

🧠 TL;DR Summary

How do I set up CI/CD using GitHub Actions?

  1. Create .github/workflows/deploy.yml
  2. Add build + deploy steps
  3. Use SSH action or connect to Netlify
  4. Push code → auto-deploy. Boom. ✅

🧰 Need Help?

At Synapse Tech Solution, we help startups and dev teams:

  • Set up full DevOps pipelines
  • Automate deployments
  • Optimize costs on AWS, GCP, or DigitalOcean

👉 Visit synapsetechsolution.com
👉 Or drop us a message — we love solving tech puzzles!