This content originally appeared on DEV Community and was authored by Ticat Wolves
In my previous article I explained how to structure a Golang project for better readability and maintainability.
Now, let’s take it a step further by building a URL Shortener Service using Go and AWS SAM (Serverless Application Model).
What is AWS SAM?
SAM (Serverless Application Model) is an Infrastructure as Code (IaC) tool provided by AWS. It simplifies deploying serverless applications by letting you define them in a template and deploying via CloudFormation
Key advantages:
Easy deployment of serverless applications
Built-in support for AWS Lambda, API Gateway, DynamoDB, etc.
Package and publish apps to the AWS Serverless Application Repository
CI/CD friendly
Flow Diagram
Technical Flow
Here’s how the URL shortener works under the hood:
- Client Request -> The user submits a request to API Gateway.
- Lambda Trigger -> API Gateway routes the request to a Lambda function written in Go.
- Supported Operations – POST, GET.
- POST Operations:
- The Lambda Parse the request payload.
- The handler generates a hash of the original URL.
- The hash and actual URL are stored in the database (DynamoDB).
- GET Operations:
- It retrieves the original URL by using Hash in the request URL.
- Lambda returns an HTTP 302 redirect with the original URL in the Location header.
- Client browser follows the redirect and loads the original URL.
Source Code
I’ve open-sourced the complete implementation here:
ShrinkIt – URL Shortener with Go + SAM
Building and Deploying with SAM
Step 1: Clone git clone https://github.com/ticatwolves/shrinkIt.git
Step 2: Build the Project make build
Step 3: Deploy the Project make deploy
Feel free to check it out, clone the repo, and try it yourself!
Have you tried building serverless applications with Go before? Share your experience in the comments — I’d love to hear your thoughts!
This content originally appeared on DEV Community and was authored by Ticat Wolves