s?
Let’s imagine we want to build a To-Do List Application where users can:
- Add tasks to their list.
- View all tasks.
- Mark tasks as completed.
We’ll use the following architecture:
- API Gateway to handle HTTP requests.
- Lambda Functions to process business logic.
- DynamoDB to store task data.
Step 1: Setting Up DynamoDB
First, we need a database to store our tasks. DynamoDB is an excellent choice because it scales automatically and provides low-latency access.
Creating a DynamoDB Table
- Open the AWS Management Console and navigate to DynamoDB .
- Click Create Table .
- Table Name :
TodoList - Primary Key :
id(String)
- Table Name :
- Enable Auto Scaling for read/write capacity units to ensure the table scales based on demand.
Sample Table Structure
| id (Primary Key) | task_name | status |
|---|---|---|
| 1 | Buy groceries | Pending |
| 2 | Read a book | Completed |
Step 2: Creating Lambda Functions
Next, we’ll create Lambda functions to handle CRUD operations for our To-Do List application.
Lambda Function: Create Task
This function will insert a new task into the TodoList table.
import json
import boto3
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('TodoList')
def lambda_handler(event, context):
# Extract task details from the event
task_name = event['task_name']
# Generate a unique ID for the task
import uuid
task_id = str(uuid.uuid4())
# Insert the task into DynamoDB
table.put_item(
Item={
'id': task_id,
'task_name': task_name,
'status': 'Pending'
}
)
return {
'statusCode': 200,
'body': json.dumps({'message': 'Task created successfully!', 'task_id': task_id})
}
Lambda Function: Get All Tasks
This function retrieves all tasks from the TodoList table.
import json
import boto3
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('TodoList')
def lambda_handler(event, context):
# Scan the DynamoDB table
response = table.scan()
# Return the list of tasks
return {
'statusCode': 200,
'body': json.dumps(response['Items'])
}
Lambda Function: Update Task Status
This function updates the status of a task (e.g., mark as completed).
import json
import boto3
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('TodoList')
def lambda_handler(event, context):
# Extract task ID and new status from the event
task_id = event['id']
new_status = event['status']
# Update the task in DynamoDB
table.update_item(
Key={'id': task_id},
UpdateExpression='SET #status = :new_status',
ExpressionAttributeNames={'#status': 'status'},
ExpressionAttributeValues={':new_status': new_status}
)
return {
'statusCode': 200,
'body': json.dumps({'message': 'Task updated successfully!'})
}
Step 3: Configuring API Gateway
Now that we have our Lambda functions, we’ll expose them via API Gateway.
Steps to Set Up API Gateway
- Open the AWS Management Console and navigate to API Gateway .
- Click Create API and select HTTP API .
- Define the following routes:
- POST /tasks : Maps to the “Create Task” Lambda function.
- GET /tasks : Maps to the “Get All Tasks” Lambda function.
- PUT /tasks/{id} : Maps to the “Update Task Status” Lambda function.
- Deploy the API and note the endpoint URL.
Step 4: Testing the Application
Once everything is set up, you can test the application using tools like Postman or cURL .
Example Requests
- Create a Task
curl -X POST https://<api-id>.execute-api.<region>.amazonaws.com/tasks \
-H "Content-Type: application/json" \
-d '{"task_name": "Buy groceries"}'
Get All Tasks
curl -X GET https://<api-id>.execute-api.<region>.amazonaws.com/tasks
Update Task Status
curl -X PUT https://<api-id>.execute-api.<region>.amazonaws.com/tasks/<task-id> \
-H "Content-Type: application/json" \
-d '{"status": "Completed"}'
Benefits of This Architecture
- Scalability : DynamoDB and Lambda automatically scale to handle varying loads.
- Cost Efficiency : You only pay for the compute time and storage you use.
- Low Maintenance : AWS manages the underlying infrastructure, reducing operational overhead.
Enjoy the cloud 😁
Osama