DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Understanding Custom Authorization Mechanisms in Amazon API Gateway and AWS AppSync
  • Implementing Budget Policies and Budget Limits on Databricks
  • Building a Scalable ML Pipeline and API in AWS
  • Serverless NLP: Implementing Sentiment Analysis Using Serverless Technologies

Trending

  • Catching Data Perimeter Drift Before It Reaches Production
  • No More Cheap Claude: 4 First Principles of Token Economics in 2026
  • Jakarta EE 12: Entering the Data Age of Enterprise Java
  • AI Paradigm Shift: Analytics Without SQL
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. Serverless Golang API With AWS Lambda

Serverless Golang API With AWS Lambda

In this tutorial, we use several different technologies to create an API and use it to populate a custom web application with movie data.

By 
Mohamed Labouardy user avatar
Mohamed Labouardy
·
Jan. 23, 18 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
12.7K Views

Join the DZone community and get the full member experience.

Join For Free

AWS announced a few days ago that Go is now a supported language for AWS Lambda. This seemed like a great opportunity to get my hands dirty by creating a Go serverless application — and deploying it to Lambda.

The application uses a serverless Lambda function written in Go to discover new movies by genres — using the free TMDb API. To access the function, I’ll also walk you through how to create a simple front-end using Angular 5.

Let’s Get started

  • The first step involves installing these two dependencies:
go get github.com/aws/aws-lambda-go/lambda # for handler registration
go get github.com/stretchr/testify # for unit tests
  • Then use the following code to create a main.go file:
package main

import (
"encoding/json"
"errors"
"fmt"
"net/http"
"os"
"strconv"

"github.com/aws/aws-lambda-go/lambda"
)

var (
API_KEY      = os.Getenv("API_KEY")
ErrorBackend = errors.New("Something went wrong")
)

type Request struct {
ID int `json:"id"`
}

type MovieDBResponse struct {
Movies []Movie `json:"results"`
}

type Movie struct {
Title       string `json:"title"`
Description string `json:"overview"`
Cover       string `json:"poster_path"`
ReleaseDate string `json:"release_date"`
}

func Handler(request Request) ([]Movie, error) {
url := fmt.Sprintf("https://api.themoviedb.org/3/discover/movie?api_key=%s", API_KEY)

client := &http.Client{}

req, err := http.NewRequest("GET", url, nil)
if err != nil {
return []Movie{}, ErrorBackend
}

if request.ID > 0 {
q := req.URL.Query()
q.Add("with_genres", strconv.Itoa(request.ID))
req.URL.RawQuery = q.Encode()
}

resp, err := client.Do(req)
if err != nil {
return []Movie{}, ErrorBackend
}
defer resp.Body.Close()

var data MovieDBResponse
if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
return []Movie{}, ErrorBackend
}

return data.Movies, nil
}

func main() {
lambda.Start(Handler)
}

Building the Binary

  • Use the following command to build an executable binary for Linux:
GOOS=linux go build -o main main.go
  • Zip the binary into a deployment package:
zip deployment.zip main
  • Then use the AWS CLI to create a new Lambda Function:
aws lambda create-function \
 --region us-east-1 \
 --function-name DiscoverMovies \
 --zip-file fileb://./deployment.zip \
 --runtime go1.x \
 --role arn:aws:iam::<account-id>:role/<role> \
 --handler main

Note: be sure to substitute a role flag with your own IAM role!

Image title

How to Setup Your AWS Lambda function

  • Access the AWS Management Console and navigate to the Lambda Dashboard — you should see that your function was created:

  • Sign up for a free account with The Movie DB.
  • Then set TMDb API KEY as environment variable.

  • Create and configure a new test event:

  • View the results of the successful execution of your test in the console:

  • In order to create the HTTPS front-end for the API, we’ll use API Gateway as a trigger to the function:

  • Finally, deploy the API Gateway:

  • You can now point your favorite browser to the Invoke URL!

Congratulations! You have created your first Lambda function in Go!

How to Build the User Interface

Now let’s build a quick user interface to our API with Angular 5.

  • First, create an Angular project from scratch using Angular CLI.
  • Then, generate a new Service that calls the API Gateway URL.
import { Injectable } from '@angular/core';
import { Http  } from '@angular/http';
import { Observable } from 'rxjs/Rx';
import 'rxjs/add/operator/map';
import { environment } from '@env/environment';

@Injectable()
export class MovieService {
  private baseUrl: string = environment.api;

  constructor(private http: Http){}

  public getMovies(id?: number){
    return this.http
      .post(`${this.baseUrl}`, {ID: id})
      .map(res => {
        return res.json()
      })
  }
}

In the main.component.html, iterate over the API response:

<section class="container">
  <div class="row">
    <div class="col-md-12">
        <button *ngFor="let genre of genres" (click)="getMoviesByGenre(genre.id)" class="btn btn-secondary">{{genre.name}}</button>
    </div>
  </div>
  <div class="row">
    <div class="col-lg-12">
      <table class="table table-hover">
        <thead>
          <tr>
            <th>Poster</th>
            <th width="20%">Title</th>
            <th>Description</th>
            <th>Release Date</th>
          </tr>
        </thead>
        <tbody>
          <tr *ngFor="let movie of movies">
            <td>
              <img src="https://image.tmdb.org/t/p/w500/{{movie.cover}}" class="cover">
            </td>
            <td>
              <span class="title">{{movie.title}}</span>
            </td>
            <td>
              <p class="description">{{movie.descritpion}}</p>
            </td>
            <td>
              <span class="date">{{movie.releaseDate}}</span>
            </td>
          </tr>
        </tbody>
      </table>
    </div>
  </div>
</section>

Note: As a reminder — the full code is available on GitHub.

  • Now it’s time to generate production grade artifacts:ng build — env=prod
  • Image title
  • Your build artifacts will be stored in the dist/ directory.
  • Next, create an S3 bucket with AWS CLI:aws s3 mb s3://discover-movies
  • Upload the build artifacts to the bucket:
aws s3 cp dist/ s3://discover-movies — recursive — grants read=uri=http://acs.amazonaws.com/groups/global/AllUsers
  • Finally, enable website hosting on for your AWS S3 bucket:aws s3 website s3://discover-movies — index-document index.html
  • Let’s Go to The movies!

    After these steps, you should be able to point your browser to the S3 Bucket URL and start browsing through the latest releases!

    I’m really interested in learning about your experience with AWS Lambda using Go, or any feedback on this project. Please drop a comment below or connect with me on Twitter!

    AWS API AWS Lambda Golang

    Published at DZone with permission of Mohamed Labouardy. See the original article here.

    Opinions expressed by DZone contributors are their own.

    Related

    • Understanding Custom Authorization Mechanisms in Amazon API Gateway and AWS AppSync
    • Implementing Budget Policies and Budget Limits on Databricks
    • Building a Scalable ML Pipeline and API in AWS
    • Serverless NLP: Implementing Sentiment Analysis Using Serverless Technologies

    Partner Resources

    ×

    Comments

    The likes didn't load as expected. Please refresh the page and try again.

    • RSS
    • X
    • Facebook

    ABOUT US

    • About DZone
    • Support and feedback
    • Community research

    ADVERTISE

    • Advertise with DZone

    CONTRIBUTE ON DZONE

    • Article Submission Guidelines
    • Become a Contributor
    • Core Program
    • Visit the Writers' Zone

    LEGAL

    • Terms of Service
    • Privacy Policy

    CONTACT US

    • 3343 Perimeter Hill Drive
    • Suite 215
    • Nashville, TN 37211
    • [email protected]

    Let's be friends:

    • RSS
    • X
    • Facebook