Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 

Repository files navigation

File Backup System

An event-driven AWS serverless solution that automatically backs up files uploaded to an S3 bucket and sends real-time notifications via SNS.

Overview

When a new object is uploaded to the source S3 bucket, a Lambda function is triggered that:

  1. Copies the object into a dedicated backup S3 bucket, preserving the original key.
  2. Publishes a JSON notification to an SNS topic (if configured) containing the source bucket, object key, backup bucket, and a UTC timestamp.
  3. Skips objects that already exist in the backup bucket (idempotent).

Architecture

Source S3 Bucket
      │
      │ s3:ObjectCreated:*
      ▼
Lambda Function (dotnet8)
      ├──► Copy object ──► Backup S3 Bucket
      └──► Publish notification ──► SNS Topic

AWS Resources

Resource Type Description
Bucket AWS::S3::Bucket Source bucket — triggers the Lambda on new uploads
BackupBucket AWS::S3::Bucket Destination bucket — receives backup copies
NotificationTopic AWS::SNS::Topic SNS topic for backup notifications
S3Function AWS::Serverless::Function Lambda function (.NET 8) that performs the backup

Both S3 buckets use DeletionPolicy: Retain to prevent accidental data loss on stack deletion.

Project Structure

FileBackupSystem/
└── src/
    └── Modules/
        └── FileBackupSystemModule/
            ├── Function.cs                   # Lambda handler
            ├── FileBackupSystemModule.csproj  # .NET 8 project file
            ├── serverless.template            # AWS SAM / CloudFormation template
            └── aws-lambda-tools-defaults.json # Lambda Tools CLI defaults

Environment Variables

The Lambda function reads the following environment variables at startup:

Variable Required Description
BACKUP_BUCKET Yes Name of the S3 bucket to copy objects into
SNS_TOPIC_ARN No ARN of the SNS topic to publish notifications to

The SAM template wires these up automatically from the provisioned resources.

Prerequisites

# Install (if not already installed)
dotnet tool install -g Amazon.Lambda.Tools

# Or update to the latest version
dotnet tool update -g Amazon.Lambda.Tools

Deployment

Using Amazon.Lambda.Tools (Recommended)

dotnet lambda deploy-serverless is the recommended approach for .NET Lambda functions. It runs dotnet publish internally, ensuring all required runtime artifacts (including .deps.json) are correctly packaged before deployment.

cd src/Modules/FileBackupSystemModule
dotnet lambda deploy-serverless --stack-name file-backup-system --resolve-s3 true 

Using Visual Studio

Right-click the FileBackupSystemModule project in Solution Explorer and select Publish to AWS Lambda.

Using SAM CLI

Warning

sam build does not run dotnet publish for .NET Lambda functions. Deploying with sam build alone will produce an incomplete package that is missing the required .deps.json file, causing a Runtime.ExitError at cold start. Run dotnet publish first to work around this.

cd src/Modules/FileBackupSystemModule

# Publish the .NET project first so sam build picks up the correct artifacts
dotnet publish -c Release

sam build --template-file serverless.template
sam deploy --guided --template-file serverless.template

SAM Template Parameters

Parameter Default Description
BackupBucketName (auto-generated) Name of the backup S3 bucket. Leave blank to let CloudFormation generate a name.

Deployment Configuration

Default values for Lambda Tools CLI and Visual Studio are stored in:

Stack Outputs

After deployment, the CloudFormation stack exposes the following outputs:

Output Description
Bucket Name of the source S3 bucket
BackupBucket Name of the backup S3 bucket
S3FunctionArn ARN of the Lambda function
NotificationTopicArn ARN of the SNS notification topic

Teardown

Warning

Both S3 buckets (Bucket and BackupBucket) have DeletionPolicy: Retain in the template. They will not be deleted when the stack is removed — you must empty and delete them manually afterwards if no longer needed.

Using Amazon.Lambda.Tools

cd src/Modules/FileBackupSystemModule
dotnet lambda delete-serverless --stack-name file-backup-system --region eu-west-1

Using SAM CLI

cd src/Modules/FileBackupSystemModule
sam delete --stack-name file-backup-system --region eu-west-1

Manually deleting the retained S3 buckets

After the stack is deleted, remove the buckets via the AWS CLI:

# Repeat for both the source bucket and the backup bucket
aws s3 rm s3://<bucket-name> --recursive
aws s3api delete-bucket --bucket <bucket-name> --region eu-west-1

Tech Stack

  • Runtime: .NET 8 (AWS Lambda)
  • IaC: AWS SAM (CloudFormation)
  • Trigger: Amazon S3 event notifications
  • Storage: Amazon S3
  • Notifications: Amazon SNS
  • Region: eu-west-1 (configurable)

About

An event-driven AWS serverless solution that automatically backs up files uploaded to an S3 bucket and sends real-time notifications via SNS.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages