aws-apigateway-go-lambda-with-fiber-clean-architecture

Clean Architecture of Golang AWS Lambda functions with DynamoDB and Fiber

This project contains a sample CDK application of ApiGateway, Lambda Functions and DynamoDB. Key Focus of this repository is to describe the clean-architecture of go lambda project and to demonstrate running services locally.

In this example project, you can run same functions that you are going to deploy into aws locally and your functions can also interact with your local DynamoDB.

✅ Requirements

✔️ Run Local DynamoDB in Terminal

When you have installed DynamoDBLocal.jar, then I would recommend to save following function in your shell to run local dynamoDB easily.

# To Run DynamoDb Local
function dynamo(){
 cd $USER/dynamolocal

 java -Djava.library.path=./DynamoDBLocal_lib/ -jar DynamoDBLocal.jar -sharedDb -port 8000
}

Whenever you run dynamo in your terminal, it will start to host your local dynamoDB on port 8000.

✔️ Set Up Employee Table in NoSQL Workbench

Employee Table is one of the sample data models of NoSQL Workbench.

  1. In main tab of the application, select Employee Data Model and click import.

  2. Click Visualize data model button.

  3. Click Commit to Amazon DynamoDB

  4. Select your localhost and commit.

This sample data model Employee will be committed into your local DynamoDB and you can have seed data to test your CRUD operations.

✨ Architecture

Architecture is simple. CDK defines an ApiGateway with single endpoint of employees. To this endpoint lambda functions are attached to run CRUD operations with DynamoDB.

Exactly same functionality of this AWS Services, however, can be hosted complete locally via fiber and NoSQL Workbench.

🔥 Deploy

  1. Clone the project to your local working directory
git clone https://github.com/deloittepark/aws-apigateway-go-lambda-clean-architecture.git
  1. Change the working directory to cdk’s directory
cd cdk
  1. Install dependencies
npm install
  1. This project uses typescript as client language for AWS CDK. Run the given command to compile typescript to javascript
npm run build
  1. Bootstrap your account with following command
npx aws-cdk bootstrap --toolkit-stack-name 'CDKToolkit-Golang-Fiber' --qualifier 'fiber' --cloudformation-execution-policies 'arn:aws:iam::aws:policy/AdministratorAccess' aws://<YOUR_AWS_ACCOUNT_ID>/<REGION> 
  1. Synthesize CloudFormation template from the AWS CDK app
cdk synth
  1. Deploy the stack to your default AWS account and region. The output of this command should give you the URL of the ApiGateway, which you can invoke via terminal or Postman
cdk deploy

🚀 Local Test

You can run this services without deploying into AWS Environment complete locally.

  1. In your terminal, run dynamo or following command, if you haven’t set up dynamo function in your shell.
java -Djava.library.path=./DynamoDBLocal_lib/ -jar DynamoDBLocal.jar -sharedDb -port 8000
  1. Go to directory api/cmd/getEmployees

  2. Start your api locally

env=local go run main.go
  1. In Terminal you can test follwoing command to create employee in your locally hosted DynamoDB

curl --location --request POST 'http://127.0.0.1:8080/employees' \
--form 'loginAlias="test"' \
--form 'firstName="hello"' \
--form 'lastName="world"' \
--form 'managerLoginAlias="N.A"'

🔨 Cleanup

Run the given command to delete the resources that were created. It might take some time for the CloudFormation stack to get deleted. This will delete all deployed resources including cloudwatch lamdba log groups. 🌳🌎🌈

cdk destroy

👀 References

  1. ☁️ awslabs/aws-lambda-go-api-proxy

  2. gofiber

GitHub

View Github