This is a serverless application that provides a RESTful API to interact with an S3 bucket for file upload, retrieval, and deletion.
The S3 Bucket API is built using the Serverless Framework and AWS Lambda. It allows users to perform various operations on an S3 bucket, such as uploading files, retrieving files, and deleting files. The API is implemented with Lambda functions, and the data is stored in an S3 bucket with private access control.
Before running this application, you'll need the following:
- Node.js (>= 14.x) and npm installed on your local machine.
- AWS CLI configured with proper credentials to deploy the application.
- An S3 bucket configured with the appropriate permissions and an environment variable
FILE_UPLOAD_BUCKET_NAME
set to the bucket name.
- Clone the repository:
git clone https://github.com/SomnathKar000/serverless-s3-lambda-crud.git
cd serverless-s3-lambda-crud
- Install dependencies:
npm install
-
Update the
serverless.yml
file:- Replace
ap-south-1
in theprovider
section with your desired AWS region. - Replace
${self:custom.bucketName}
in thecustom
section with your desired S3 bucket name prefix.
- Replace
To run the application locally, you can use the Serverless Framework's invoke local
command. For example, to test the getFileHandler
function:
serverless invoke local --function getFileHandler --path ./test/event.json
Replace getFileHandler
with the desired function name and update the event.json
file with the event data.
The following API endpoints are available:
-
POST /upload
: Upload a file to the S3 bucket.Request body:
{ "fileName": "example.txt", "fileContext": "Your file content here" }
-
GET /files/{id}
: Retrieve a file from the S3 bucket by its ID. -
DELETE /delete/{id}
: Delete a file from the S3 bucket by its ID.
To deploy the application to AWS, use the following command:
npm run deploy
This will create the necessary AWS resources, including the Lambda functions and API Gateway with the defined routes.
The API Gateway stage endpoint for the deployed application is:
https://7xkvhkn60c.execute-api.ap-south-1.amazonaws.com/dev
To enable GitHub Actions for continuous integration and deployment, the following IAM permissions were granted to the IAM user associated with the GitHub Actions workflow:
Permissions Policies attached to the IAM user:
-
AmazonAPIGatewayAdministrator
: AWS managed policy that provides full access to Amazon API Gateway resources. -
AmazonAPIGatewayPushToCloudWatchLogs
: AWS managed policy that allows pushing API Gateway logs to CloudWatch Logs. -
AmazonS3FullAccess
: AWS managed policy that provides full access to Amazon S3 resources. -
AWSCloudFormationFullAccess
: AWS managed policy that provides full access to AWS CloudFormation resources. -
AWSLambda_FullAccess
: AWS managed policy that provides full access to AWS Lambda resources. -
AWSLambdaRole
: AWS managed policy that grants permissions required for Lambda execution. -
CloudWatchLogsFullAccess
: AWS managed policy that provides full access to CloudWatch Logs. -
IAMFullAccess
: AWS managed policy that provides full access to IAM resources.
Note: It is important to ensure that the IAM user associated with the GitHub Actions workflow has the necessary permissions to deploy the application.
Please make sure to handle IAM user credentials securely and avoid sharing sensitive information. Using GitHub Actions secrets to store the AWS_ACCESS_KEY_ID
and AWS_SECRET_ACCESS_KEY
is a good practice.