AWS has announced few days ago, Go as supported language for AWS Lambda. So, I got my hand dirty and I made a Serverless Golang Lambda Function to discover new Movies by genres, I went even further and created a Frontend in top of my API with Angular 5.
Note: The full source code for this application can be found on GitHub
To get started, install the dependencies below:
1 2
go get github.com/aws/aws-lambda-go/lambda # for handler registration go get github.com/stretchr/testify # for unit tests
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"` }
var data MovieDBResponse if err := json.NewDecoder(resp.Body).Decode(&data); err != nil { return []Movie{}, ErrorBackend }
return data.Movies, nil }
funcmain() { lambda.Start(Handler) }
The handler function takes as a parameter the movie genre ID then query the TMDb API – Awesome free API for Movies and TV Shows – to get list of movies. I registred the handler using the lambda.Start() method.
To test our handler before deploying it, we can create a basic Unit Test:
Note: substitute role flag with your own IAM role.
Sign in to the AWS Management Console, and navigate to Lambda Dashboard, you should see your lambda function has been created:
Set TMDb API KEY (Sign up for an account) as environment variable:
Create a new test event:
Upon successful execution, view results in the console:
To provide the HTTPS frontend for our API, let’s add API Gateway as a trigger to the function:
Deployment:
Now, if you point your favorite browser to the Invoke URL:
Congratulations ! you have created your first Lambda function in Go.
Let’s build a quick UI in top of the API with Angular 5. Create an Angular project from scratch using Angular CLI. Then, generate a new Service to calls the API Gateway URL: