This project deploys a serverless REST API on AWS using AWS CDK (Cloud Development Kit) in Python. The API leverages API Gateway, Lambda functions, and DynamoDB to provide a simple REST interface for managing employee records.
- API Gateway: Exposes a REST API with CORS enabled, allowing GET and POST methods.
- Lambda Function: Processes API requests to add new records or fetch existing records by ID.
- DynamoDB Table: Stores employee data, with an auto-generated unique ID for each record.
app.py
: Entry point for the CDK application.PyRestApiStack
: Defines the AWS resources and connections between them.services/index.py
: Lambda function handler and supporting functions.
- DynamoDB Table: Named
EmplTablePy
, withid
as the partition key, and provisioned for on-demand billing. - Lambda Function: Named
EmplLambda
, using Python 3.11 runtime. Configured with environment variables to access the DynamoDB table and usesindex.handler
as the entry point. - API Gateway: Exposes the REST API with two endpoints:
- POST /empl: Adds a new employee record.
- GET /empl?id={id}: Fetches an employee record by
id
.
Base URL: https://51pl8svxec.execute-api.us-east-1.amazonaws.com/prod/
-
POST /empl
Add a new employee record.- Endpoint:
POST https://51pl8svxec.execute-api.us-east-1.amazonaws.com/prod/empl
- Request Body (JSON):
{ "name": "John Doe", "position": "Software Engineer", "department": "Engineering" }
- Response (JSON):
{ "id": "generated-unique-id" }
- Endpoint:
-
GET /empl?id={id}
Fetch an employee record by ID.- Endpoint:
GET https://51pl8svxec.execute-api.us-east-1.amazonaws.com/prod/empl?id={id}
- Query Parameter:
id
(UUID of the employee record) - Response (JSON):
{ "id": "generated-unique-id", "name": "John Doe", "position": "Software Engineer", "department": "Engineering" }
- Error Response:
- Status Code:
404 Not Found
- Response Body:
{ "error": "Not found" }
- Status Code:
- Endpoint:
The Lambda handler function (index.handler
) processes incoming HTTP requests based on the method:
- POST: Calls
add_new_item()
to insert a new record with a generated UUID. - GET: Calls
get_item_by_id()
to retrieve a record by its unique ID. - Helper Functions:
generate_response()
: Standardizes API responses with CORS headers.add_new_item()
andget_item_by_id()
: Interact with DynamoDB.
- Install AWS CDK
npm install -g aws-cdk
- create and activate a Virtual Environment (Optional but recommended)
python3 -m venv .venv && source .venv/bin/activate
- Install Python dependencies
pip install -r requirements.txt
- Deploy the stack
cdk deploy
This project was inspired by the Udemy course:
Course Title:AWS CDK for professionals (Python and TypeScript)
by [Alex Dan]