Skip to content

ramjangatisetty/aws-serverless-s3-csv-lambda

Repository files navigation

AWS Serverless CSV Processor

This project is a serverless application that processes CSV files uploaded to an S3 bucket using AWS Lambda (Java with Spring Boot) and stores the data into a DynamoDB table. It also archives the original CSV and stores a JSON version.


📦 Prerequisites

  • AWS CLI (aws configure to set credentials)
  • AWS SAM CLI
  • Docker
  • Java 11 and Gradle

🗂 Project Structure

aws-serverless-s3-csv-lambda/
├── build.gradle
├── template.yaml
├── build.sh
└── src/main/java/com/letzautomate/
    ├── lambda/LambdaHandler.java
    ├── service/FileProcessorService.java
    ├── service/S3Archiver.java
    ├── service/JsonOutputService.java
    └── model/User.java

📝 SAM Template: template.yaml

Defines the S3 bucket, Lambda function, and DynamoDB table.

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Globals:
  Function:
    Timeout: 30
    Runtime: java11
    MemorySize: 1024
Resources:
  InputBucket:
    Type: AWS::S3::Bucket
    Properties:
      BucketName: s3-csv-user-processor-input

  CsvProcessingFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: .
      Handler: com.letzautomate.lambda.LambdaHandler
      Policies:
        - AmazonS3FullAccess
        - AmazonDynamoDBFullAccess
      Events:
        S3Upload:
          Type: S3
          Properties:
            Bucket: !Ref InputBucket
            Events: s3:ObjectCreated:*
      Environment:
        Variables:
          OUTPUT_BUCKET_NAME: !Ref InputBucket
          ARCHIVE_PREFIX: "archive/"
          OUTPUT_PREFIX: "output/"

  UserTable:
    Type: AWS::DynamoDB::Table
    Properties:
      BillingMode: PAY_PER_REQUEST
      AttributeDefinitions:
        - AttributeName: employeeId
          AttributeType: S
      KeySchema:
        - AttributeName: employeeId
          KeyType: HASH

🚀 Deployment Script: build.sh

#!/bin/bash

set -e
STACK_NAME="csv-lambda-dynamo"
REGION="us-east-1"
BUCKET_NAME="sam-artifacts-ramakrishna-csv-lambda"

if ! aws s3 ls "s3://$BUCKET_NAME" 2>&1 | grep -q 'NoSuchBucket'; then
  echo "✅ Bucket exists"
else
  aws s3 mb "s3://$BUCKET_NAME" --region "$REGION"
fi

sam build

sam deploy   --stack-name "$STACK_NAME"   --s3-bucket "$BUCKET_NAME"   --region "$REGION"   --capabilities CAPABILITY_IAM   --no-confirm-changeset

🧠 Main Logic

FileProcessorService does the following:

  1. Downloads CSV from S3.
  2. Parses it into User objects.
  3. Inserts them into DynamoDB.
  4. Saves a JSON file.
  5. Archives the CSV.

📤 To Trigger

Upload a file:

aws s3 cp users.csv s3://s3-csv-user-processor-input/

✅ Final Output

Component Status
Lambda Build
S3 Trigger
DynamoDB Write
Archiving
Logging

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published