This project demonstrates a serverless application using AWS services. The application consists of an API Gateway, AWS Lambda functions, and DynamoDB for data storage.
Venkatesh Pattem - DevOps Engineer with a passion for serverless architecture and cloud computing.
- AWS CLI installed and configured
- AWS SAM CLI installed
- Python 3.8 or later
- Docker (for local testing)
app/
: Contains the Lambda function code.tests/
: Contains the unit tests for the application.infra/
: Infrastructure as code (IaC) templates and deployment scripts.
git clone https://github.com/venpattem/aws-serverless-app.git
cd aws-serverless-app
Copy the example environment file and update it with your configuration.
cp .env.example .env
Open the .env
file and update it with your AWS credentials and other necessary information:
AWS_ACCESS_KEY_ID=your_access_key_id
AWS_SECRET_ACCESS_KEY=your_secret_access_key
TABLE_NAME=SampleDynamoDBTable
AWS_REGION=us-east-1
Install the dependencies for both the application and the infrastructure.
# Application dependencies
pip install -r app/requirements.txt
# Infrastructure dependencies
pip install -r infra/requirements.txt
Navigate to the infra/
directory and deploy the application using the AWS SAM CLI.
cd infra
./deploy.sh
Run unit tests to ensure the application works correctly.
pytest tests/
The template.yaml
file in the infra/
directory defines the AWS resources for the serverless application.
The deploy.sh
script automates the deployment process.
#!/bin/bash
# Build the application
sam build
# Deploy the application
sam deploy --guided
This is the main Lambda function code. It handles API requests and interacts with DynamoDB.
import json
import boto3
import os
def lambda_handler(event, context):
dynamodb = boto3.resource('dynamodb', region_name=os.environ['AWS_REGION'])
table = dynamodb.Table(os.environ['TABLE_NAME'])
# Example operation: Put item into DynamoDB
response = table.put_item(
Item={
'id': '123',
'data': 'Hello from Lambda!'
}
)
return {
'statusCode': 200,
'body': json.dumps('Hello from Lambda!')
}
Unit tests for the Lambda function.
import pytest
from app import app
def test_lambda_handler():
event = {}
context = {}
response = app.lambda_handler(event, context)
assert response['statusCode'] == 200
assert response['body'] == '"Hello from Lambda!"'
To delete the resources created by the stack, run:
aws cloudformation delete-stack --stack-name your-stack-name
This project provides a basic structure for an AWS serverless application with clear instructions on setup, deployment, and testing. Feel free to modify and expand it according to your needs.