This serverless application (REST API) is built using Golang to leverage its efficiency and concurrency features, providing a highly scalable and reliable backend service. It follows the serverless architecture paradigm by utilizing AWS Lambda for compute, Amazon DynamoDB for persistent data storage, and Amazon API Gateway for request handling and routing.
This architecture ensures cost-effectiveness by eliminating the need for dedicated servers while maintaining scalability and high availability, making it ideal for modern cloud-native applications.
- Serverless Architecture: Powered by AWS Lambda and API Gateway.
- Database: DynamoDB for efficient, scalable storage.
- Written in Go: Leveraging Go's speed and efficiency.
Ensure you have the following installed:
- Go (Golang):
- Install Go from Go Official Website.
Serverless-with-Golang/
├───build
| └───deployment.zip
├───cmd
| ├───main.go
│ └───bootstrap
├───pkg
│ ├───handlers
│ ├───user
│ └───validators
├── README.md
├── go.sum
└── go.mod
-
Clone the Repository
git clone https://github.com/ARUP-G/Serverless-with-Golang.git cd Serverless-with-Golang
-
Build the Go Application
# For Linux user GOOS=linux GOARCH=amd64 go build -o bootstrap cmd/main.go #For windows user $env:GOOS="linux"; $env:GOARCH="amd64"; go build -o bootstarp cmd/main.go # This builds the Go application compatible with AWS Lambda's environment.
-
Setup Using AWS Console
-
Create a DynamoDB Table
- Go to the DynamoDB Console: DynamoDB Console.
- Click Create table.
- Provide the following details:
- Table Name: go-serverless-tb (or a name of your choice, change it in the main.go)
- Partition Key: email (String)
- Select On-demand capacity mode (Pay-per-request) for flexibility.
- Click Create table.
-
Create a Lambda Function
- Go to the Lambda Console: Lambda Console.
- Click Create function.
- Select Author from scratch.
- Provide the following details:
- Function name: go-serverless-app
- Runtime: Amazon Linux
- Architecture: x86_64
- Under Permissions, create a new role with basic Lambda permissions.
- Click Create function.
-
Upload the Code
-
Add Environment Variables (Optional)
- Scroll to the Configuration tab and click Environment variables.
- Add variables such as
TABLE_NAME
for your DynamoDB table name.
-
Grant Lambda Access to DynamoDB
- In the Lambda Console, go to the Configuration tab.
- Click Permissions and then click the role name under Execution role.
- Attach the
AmazonDynamoDBFullAccess
policy:- Click Add permissions → Attach policies.
- Search for
AmazonDynamoDBFullAccess
and click Attach policy.
-
Create an API Gateway
- Go to the API Gateway Console: API Gateway Console.
- Click Create API and choose REST API.
- Select Build.
- Under Configure routes, add the following route:
- Method: ANY
- Resource path: /items/{proxy+}
- Select Integration and choose Lambda function.
- Enter your Lambda function name (go-serverless-app).
- Click Create.
- Deploy the API:
-
-
Test the Setup
-
Test via API Gateway
- Copy the Invoke URL from the Stages section of your API Gateway.
- Test the endpoint using curl or Postman:
curl -X POST -H "Content-Type: application/json" -d '{"email": "test@example.com","firstName": "Kernel", "lastName": "Dev"}' https://<API_ID>.execute-api.<REGION>.amazonaws.com/prod/items
-
Check DynamoDB
- Go to the DynamoDB Console → Tables → Your Table.
- Click Explore table items to verify the data.
-
-
Cleanup Resources
To avoid unnecessary charges, delete the resources when they are no longer needed:
- Lambda Function:
- Go to the Lambda Console, select the function, and click Delete.
- DynamoDB Table:
- Go to the DynamoDB Console, select the table, and click Delete table.
- API Gateway:
- Go to the API Gateway Console, select the API, and click Delete API.
- Lambda Function:
Contributions are welcome! Please submit a PR or open an issue.
This project is licensed under the MIT License.