GoLang Tutorial - Serverless Web API with AWS Lambda
In this post, we're going to make an HTTP endpoint that accepts a POST request at the path /echo, logs the POST body, and echoes the body back to the client.
We may want to modify our $GOPATH/src/myservice/serverless.yml to attach an HTTP event:
service: myservice # NOTE: update this with your service name frameworkVersion: ">=1.28.0 <2.0.0" provider: name: aws runtime: go1.x package: exclude: - ./** include: - ./bin/** functions: hello: handler: bin/hello events: - http: path: hello method: post
We replaced the get with post.
We'll also need to update our function in hello/main.go:
The code main.go
Our Handler() function now takes an APIGatewayProxyRequest object and returns a APIGatewayProxyResponse object. In our code, we're printing the request body, then returning a response with the request body.
Since we modified our code, we need to compile it again, and then deploy it.
$ pwd /Users/kihyuckhong/go/src/myservice $ make dep ensure -v env GOOS=linux go build -ldflags="-s -w" -o bin/hello hello/main.go env GOOS=linux go build -ldflags="-s -w" -o bin/world world/main.go $ sls deploy Serverless: Packaging service... Serverless: Excluding development dependencies... Serverless: Uploading CloudFormation file to S3... Serverless: Uploading artifacts... Serverless: Uploading service myservice.zip file to S3 (11.94 MB)... Serverless: Validating template... Serverless: Updating Stack... Serverless: Checking Stack update progress... .............................. Serverless: Stack update finished... Service Information service: myservice stage: dev region: us-east-1 stack: myservice-dev resources: 16 api keys: None endpoints: POST - font color="green">https://fxmlls3d76.execute-api.us-east-1.amazonaws.com/dev/hello POST - https://fxmlls3d76.execute-api.us-east-1.amazonaws.com/dev/world functions: hello: myservice-dev-hello world: myservice-dev-world layers: None
Now we have an endpoint for the "hello" listed in our Service Information output. We can see our deployment from AWS Lambda console:
Let's use curl to hit our endpoint and get a response:
$ curl -X POST \ https://fxmlls3d76.execute-api.us-east-1.amazonaws.com/dev/hello \ -d "Serverless Go" Serverless Go
Note that we used the "-d" flag to send the specified data in a POST request to the HTTP server.
Note also that the main() function which is the entrypoint to our Golang binary is NOT our Lambda handler function. In the main() function we call lambda.Start() and pass in the Handler function as the lambda handler. Under the hood, a RPC server wraps our handler function:
func main() { lambda.Start() (Handler) }
Ref: Serverless Framework example for Golang and Lambda
Go Tutorial
- GoLang Tutorial - HelloWorld
- Calling code in an external package & go.mod / go.sum files
- Workspaces
- Workspaces II
- Visual Studio Code
- Data Types and Variables
- byte and rune
- Packages
- Functions
- Arrays and Slices
- A function taking and returning a slice
- Conditionals
- Loops
- Maps
- Range
- Pointers
- Closures and Anonymous Functions
- Structs and receiver methods
- Value or Pointer Receivers
- Interfaces
- Web Application Part 0 (Introduction)
- Web Application Part 1 (Basic)
- Web Application Part 2 (Using net/http)
- Web Application Part 3 (Adding "edit" capability)
- Web Application Part 4 (Handling non-existent pages and saving pages)
- Web Application Part 5 (Error handling and template caching)
- Web Application Part 6 (Validating the title with a regular expression)
- Web Application Part 7 (Function Literals and Closures)
- Building Docker image and deploying Go application to a Kubernetes cluster (minikube)
- Serverless Framework (Serverless Application Model-SAM)
- Serverless Web API with AWS Lambda
- Arrays vs Slices with an array left rotation sample
- Variadic Functions
- Goroutines
- Channels ("<-")
- Channels ("<-") with Select
- Channels ("<-") with worker pools
- Defer
- GoLang Panic and Recover
- String Formatting
- JSON
- SQLite
- Modules 0: Using External Go Modules from GitHub
- Modules 1 (Creating a new module)
- Modules 2 (Adding Dependencies)
- AWS SDK for Go (S3 listing)
- Linked List
- Binary Search Tree (BST) Part 1 (Tree/Node structs with insert and print functions)
- Go Application Authentication I (BasicAuth, Bearer-Token-Based Authentication)
- Go Application Authentication II (JWT Authentication)
Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization