golang-serverless-restapi
golang-serverless-restapi copied to clipboard
A starter kit for serverless REST API in Golang
Serverless REST API in Go
This is a starter kit for creating a serverless REST API using Go and apex.
Installation
Install apex and the Go packages as instructed below:
# install apex if you don't have it yet
curl https://raw.githubusercontent.com/apex/apex/master/install.sh | sudo sh
# download this package
go get github.com/qiangxue/golang-serverless-restapi
# download the go-apex package
go get github.com/apex/go-apex
Deploying Lambda
First, follow the apex documentation to set up the AWS credentials needed for deploying Lambdas using apex.
Then, modify the project.json
file by inserting the correct AWS role
and profile
values.
Run the following command to build and deploy the Lambda:
apex deploy --region us-east-1
And use the AWS console or the following command to verify that the Lambda is successfully deployed:
apex invoke apis
Configuring AWS API Gateway
- Log into the AWS console and switch to the API Gateway page. On that page, choose "Create new API" and enter the API name as "hello" (or any other name you prefer).
- In the "Resources" tab of the "hello" API, click on the "Actions" dropdown button and select "Create Resource". In the "New Child Resource" page, select "Configure as proxy resource", and then click on the "Create Resource" button.
- In the "/{proxy+} - ANY - Setup" page, choose "Lambda Region" as
us-east-1
(or the actual AWS region that you used to deploy the Lambda function) and enter the "Lambda Function" name asrest_apis
. Click the "Save" button to complete the proxy resource setup. - Click on the "Actions" dropdown button and select "Deploy API". In the popup window, choose
[New Stage]
in the "Deployment stage" dropdown list, and enterprod
in the "Stage name" input field. Click on the "Deploy" button to complete the deployment. - At this point, you should be redirected to a page showing the deployment information about the "hello" API. You should see an "Invoke URL" on the page. The URL may look like "https://bm7empvth7.execute-api.us-east-1.amazonaws.com/prod". This is the base URL that everyone can use to hit our APIs.
Trying it Out
Run the following commands to verify the API is accessible and working as expected:
# replace InvokeURL with the actual Invoke URL found in Step 5
> curl InvokeURL/foo
hello
> curl InvokeURL/bar?hello=world
GET /bar?hello=world
In the AWS console, locate the Lambda function named rest_apis
and check its monitoring result to verify that the Lambda was invoked.
What's Next
In the above demo, we used the standard HTTP ServerMux to wire up the HTTP handlers for handling different API endpoints. You may replace it with your favorite third-party HTTP routers (e.g. ozzo-routing, echo, gin). This can be done easily by modifying the functions/apis/api.go file.
Because we are using AWS API Gateway as a proxy to invoke the Lambda, we may implement more API endpoints without the need of reconfiguring the Gateway. That is, each time we make changes to our REST API project, we only need to run apex deploy
to deploy it to AWS. We do not need to reconfigure AWS API Gateway.