This project is a cloud-based messaging backend API built using AWS services such as API Gateway, Lambda, DynamoDB, Cognito, and Secrets Manager. The backend supports user registration, authentication, sending messages, blocking users, creating groups, adding/removing users from groups, and checking messages.
- Architecture
- Flow and Architecture of the Messaging Backend API
- Setup and Deployment
- Testing
- Security
- License
The architecture consists of the following components:
- API Gateway: Manages API endpoints and integrates with Lambda functions.
- Lambda Functions: Handles the business logic for user registration, authentication, messaging, and group management.
- DynamoDB: Stores user data, messages, groups, and blocked users.
- Cognito: Manages user authentication and authorization.
- Secrets Manager: Securely stores and retrieves sensitive information such as Cognito client ID.
-
User Registration:
- Client sends a registration request to the API Gateway.
- API Gateway routes the request to the register_user Lambda function.
- The register_user Lambda function:
- Uses Cognito to create a new user and retrieves the user ID.
- Stores user data in the DynamoDB Users table.
- Returns a success message to the client.
-
User Authentication:
- Client sends an authentication request to the API Gateway.
- API Gateway routes the request to the authenticate_user Lambda function.
- The authenticate_user Lambda function:
- Uses Cognito to authenticate the user.
- Retrieves a token from Cognito.
- Returns the token to the client.
-
Send Message:
- Client sends a message request to the API Gateway.
- API Gateway routes the request to the send_message Lambda function.
- The send_message Lambda function:
- Checks the DynamoDB BlockedUsers table to ensure the recipient is not blocking the sender.
- Stores the message in the DynamoDB Messages table.
- Returns a success message to the client.
-
Block User:
- Client sends a block user request to the API Gateway.
- API Gateway routes the request to the block_user Lambda function.
- The block_user Lambda function:
- Updates the DynamoDB BlockedUsers table to block the specified user.
- Returns a success message to the client.
-
Create Group:
- Client sends a create group request to the API Gateway.
- API Gateway routes the request to the create_group Lambda function.
- The create_group Lambda function:
- Creates a new group in the DynamoDB Groups table.
- Returns the group ID to the client.
-
Add User to Group:
- Client sends an add user to group request to the API Gateway.
- API Gateway routes the request to the add_user_to_group Lambda function.
- The add_user_to_group Lambda function:
- Updates the DynamoDB GroupMemberships table to add the user to the group.
- Returns a success message to the client.
-
Remove User from Group:
- Client sends a remove user from group request to the API Gateway.
- API Gateway routes the request to the remove_user_from_group Lambda function.
- The remove_user_from_group Lambda function:
- Updates the DynamoDB GroupMemberships table to remove the user from the group.
- Returns a success message to the client.
-
Send Group Message:
- Client sends a group message request to the API Gateway.
- API Gateway routes the request to the send_group_message Lambda function.
- The send_group_message Lambda function:
- Retrieves group members from the DynamoDB GroupMemberships table.
- Stores the message in the DynamoDB Messages table.
- Returns a success message to the client.
-
Check Messages:
- Client sends a check messages request to the API Gateway.
- API Gateway routes the request to the check_messages Lambda function.
- The check_messages Lambda function:
- Retrieves messages for the user from the DynamoDB Messages table.
- Returns the messages to the client.
User Authentication and Authorization:
Utilized AWS Cognito for managing user authentication and authorization. Users must authenticate using their credentials to obtain a JWT token. JWT token is required for accessing protected endpoints. Secrets Management:
Used AWS Secrets Manager to securely store and manage sensitive information like Cognito client IDs. Lambda functions retrieve secrets from Secrets Manager to avoid hardcoding sensitive information. Environment Variables:
Used environment variables to store AWS credentials and other sensitive information, ensuring they are not hardcoded in the codebase. Access Control:
Implemented access control checks in the send_message Lambda function to verify if a user is blocked by the recipient. Implemented role-based access control in group-related operations to ensure only authorized users can perform specific actions.
Lambda Functions
register_user:
Handles user registration by interacting with AWS Cognito and DynamoDB. authenticate_user:
Handles user authentication by interacting with AWS Cognito. send_message:
Handles sending messages between users. Checks if the recipient has blocked the sender before storing the message in DynamoDB. block_user:
Handles blocking a user by updating the DynamoDB BlockedUsers table. create_group:
Handles creating a new group in the DynamoDB Groups table. add_user_to_group:
Handles adding a user to a group by updating the DynamoDB GroupMemberships table. remove_user_from_group:
Handles removing a user from a group by updating the DynamoDB GroupMemberships table. send_group_message:
Handles sending messages to a group by storing the message in the DynamoDB Messages table. check_messages:
Handles checking messages for a user by retrieving messages from the DynamoDB Messages table. DynamoDB Tables
Users table:
Stores user data. Messages table:
Stores messages sent between users and within groups. BlockedUsers table:
Stores information about users who have blocked other users. Groups table:
Stores group information. GroupMemberships table:
Stores information about group memberships. API Gateway Manages all API endpoints and routes requests to the appropriate Lambda functions. Ensures that only authenticated and authorized requests are processed. This architecture provides a scalable, secure, and efficient messaging backend using AWS services, ensuring that user data is managed securely and access control is enforced appropriately.
- AWS account
- AWS CLI installed and configured
- Terraform installed
- Python 3.9 installed
-
Clone the repository:
git clone https://github.com/your-repo/messaging-backend.git cd messaging-backend
-
Install dependencies and package Lambda functions:
cd lambda pip install -r requirements.txt -t . zip -r ../lambda.zip . cd ..
-
Initialize and apply Terraform configuration:
- You must install Terraform and configure your AWS credentials first.
cd./terraform terraform init terraform -chdir=terraform apply -auto-approve
-
Deployment script:
Alternatively, you can use the deployment script:
```sh
./deploy.sh
```
Tests are included to ensure the functionality of the Lambda functions. Tests are located in the tests directory and use pytest along with moto to mock AWS services.
Running Tests Install test dependencies:
pip install -r lambda/requirements.txt
Run tests:
pytest tests/
- Cognito: Manages user authentication and authorization.
- Secrets Manager: Securely stores and retrieves sensitive information such as Cognito client ID.