This project is a serverless REST API built using AWS Lambda, DynamoDB, and the Go programming language. It provides functionality for managing user records in a DynamoDB table and is structured to facilitate scalability and maintainability.
The project follows a modular file structure:
cmd
│ main.go
pkg
├── handlers
│ ├── handlers.go
│ ├── api_response.go
├── user
│ ├── user.go
├── validators
│ ├── is_email_valid.go
- Entry point of the application.
- Initializes AWS session and DynamoDB client.
- Routes HTTP methods (
GET,POST,PUT,DELETE) to their respective handlers.
- Implements HTTP handlers for user-related operations:
GetUser: Fetches user(s) based on query parameters.CreateUser: Adds a new user to the DynamoDB table.UpdateUser: Updates an existing user's data.DeleteUser: Removes a user from the DynamoDB table.UnhandledMethod: Handles unsupported HTTP methods.
- Provides the
apiResponsefunction to format API responses with status codes, headers, and JSON bodies.
- Contains the core logic for interacting with DynamoDB:
FetchUser: Fetches a single user by email.FetchUsers: Retrieves all users.CreateUser: Validates and adds a new user.UpdateUser: Validates and updates user details.DeleteUser: Deletes a user from the table.
- Provides the
IsEmailValidfunction to validate email addresses using regex.
- Install Go (version 1.16 or later).
- Configure AWS CLI with valid credentials.
- Create a DynamoDB table with a primary key named
email. - Set environment variables:
AWS_REGION: The AWS region for your DynamoDB table.TABLE_NAME: The name of your DynamoDB table.
- Clone the repository:
git clone <repository-url> cd <project-directory>
- Install dependencies:
go mod tidy
- Use the AWS SAM CLI to run the application locally:
sam local start-api - Test the endpoints with
curlor tools likePostman.
- Endpoint:
POST /users - Command:
curl --header "Content-Type: application/json" \ --request POST \ --data '{"email":"chdvanshsingh@gmail.com", "firstname":"Vansh", "lastname":"Singh"}' \ https://<api-gateway-url>/users
- Endpoint:
GET /users - Command:
curl --request GET https://<api-gateway-url>/users
- Endpoint:
GET /users?email=<email> - Command:
curl --request GET https://<api-gateway-url>/users?email=chdvanshsingh@gmail.com
- Endpoint:
PUT /users - Command:
curl --header "Content-Type: application/json" \ --request PUT \ --data '{"email":"chdvanshsingh@gmail.com", "firstname":"VanshUpdated", "lastname":"SinghUpdated"}' \ https://<api-gateway-url>/users
- Endpoint:
DELETE /users?email=<email> - Command:
curl --request DELETE https://<api-gateway-url>/users?email=chdvanshsingh@gmail.com
- Use
curl,Postman, or other tools to test the API. - Write unit tests for individual functions in the
pkg/directory.
- Fork the repository.
- Create a feature branch.
- Commit your changes and create a pull request.
This project is licensed under the MIT License.