DEV Community

Ticat Wolves
Ticat Wolves

Posted on

Shorten URL Service with Go and AWS SAM

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:

  1. Client Request -> The user submits a request to API Gateway.
  2. Lambda Trigger -> API Gateway routes the request to a Lambda function written in Go.
  3. Supported Operations - POST, GET.
  4. 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).
  5. 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!

Top comments (0)