Serverless Golang API with AWS Lambda
How to create a movie website using an AWS Lambda function written in Go — the latest language for serverless applications
AWS announced 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:
The handler function uses the movie genre ID as a parameter to query the TMDb API — an awesome free API for Movies and TV Shows — and returns a list of movies. The handler is registered using the lambda.Start() method.
Testing the handler
To test our handler before deploying it, we can create a basic unit test using the code provided below. To run the unit test, simply issue the following command: go test
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 role flag with your own IAM role!
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
.
- In the
main.component.html
, iterate over the API response:
As a reminder — the full code is available in GitHub.
- Now it’s time to generate production grade artifacts:
ng build — env=prod
- 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!