GoLang Tutorial - Serverless Framework (Serverless Application Model-SAM)
The Serverless Application Model (SAM) is an open-source framework that we can use to build serverless applications.
A serverless application is a combination of Lambda functions, event sources, and other resources that work together to perform tasks. Note that a serverless application is more than just a Lambda functionβit can include additional resources such as APIs, databases, and event source mappings.
Install Serverless Framework on local - Serverless CLI v1.9.0 or later:
$ npm install -g serverless ... + serverless@1.41.1 ...
The Framework will configure AWS for Go.
There are a couple Go templates already included with the Framework as of v1.26βaws-go for a basic service with two functions, and aws-go-dep for the basic service using the dep dependency management tool. Let's try the aws-go-dep template. So, we will need dep installed. On MacOS we can install or upgrade to the latest released version with Homebrew:
$ brew install dep Updating Homebrew... ==> Auto-updated Homebrew! Updated 1 tap (homebrew/cask). No changes to formulae. ==> Installing dependencies for dep: go ==> Installing dep dependency: go ==> Downloading https://homebrew.bintray.com/bottles/go-1.12.4.high_sierra.bottle.tar.gz ==> Downloading from https://akamai.bintray.com/99/9920e9264e80f0bac5098a0bbdbd1818c2c44eba37d3b9accd61c1236fcad5f2?__gda__=exp=1556574768~hmac=acc5a40c78db7c3 ######################################################################## 100.0% ==> Pouring go-1.12.4.high_sierra.bottle.tar.gz πΊ /usr/local/Cellar/go/1.12.4: 9,798 files, 452.6MB ==> Installing dep ==> Downloading https://homebrew.bintray.com/bottles/dep-0.5.1.high_sierra.bottle.tar.gz ==> Downloading from https://akamai.bintray.com/18/185d8734c3009053c1b7eca70e61e2749ca83fe3573257a95e4c7f173f8eacd8?__gda__=exp=1556574865~hmac=4db5ca163469b09 ######################################################################## 100.0% ==> Pouring dep-0.5.1.high_sierra.bottle.tar.gz πΊ /usr/local/Cellar/dep/0.5.1: 7 files, 11.5MB $ brew upgrade dep
Make sure we're in our ${GOPATH}/src directory, then run:
$ serverless create --template aws-go-dep --path myservice Serverless: Generating boilerplate... Serverless: Generating boilerplate in "/Users/kihyuckhong/go/src/myservice" _______ __ | _ .-----.----.--.--.-----.----| .-----.-----.-----. | |___| -__| _| | | -__| _| | -__|__ --|__ --| |____ |_____|__| \___/|_____|__| |__|_____|_____|_____| | | | The Serverless Application Framework | | serverless.com, v1.41.1 -------' Serverless: Successfully generated boilerplate for template: "aws-go-dep"
The myservice directory looks like this:
myservice βββ Gopkg.toml βββ Makefile βββ hello βΒ Β βββ main.go βββ serverless.yml βββ world βββ main.go
Let's go into our new service directory and compile the function:
$ cd myservice $ make dep ensure -v Root project is "myservice" 2 transitively valid internal packages 2 external packages imported from 1 projects (0) β select (root) (1) ? attempt github.com/aws/aws-lambda-go with 2 pkgs; 17 versions to try (1) try github.com/aws/aws-lambda-go@v1.10.0 (1) β select github.com/aws/aws-lambda-go@v1.10.0 w/5 pkgs β found solution with 5 packages from 1 projects Solver wall times by segment: b-source-exists: 1.425513682s b-gmal: 430.584962ms b-list-pkgs: 422.845174ms new-atom: 2.435891ms select-root: 546.41Β΅s satisfy: 328.575Β΅s select-atom: 127.94Β΅s b-list-versions: 57.818Β΅s other: 25.756Β΅s TOTAL: 2.282466208s (1/1) Wrote github.com/aws/aws-lambda-go@v1.10.0 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
Here is the basic directory structure (not shown all here):
The default command in the included Makefile will gather our dependencies and build the proper binaries for our functions. We can deploy now:
$ serverless deploy Serverless: Packaging service... Serverless: Excluding development dependencies... Serverless: Creating Stack... Serverless: Checking Stack create progress... ..... Serverless: Stack create finished... 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: GET - https://fxmlls3d76.execute-api.us-east-1.amazonaws.com/dev/hello GET - https://fxmlls3d76.execute-api.us-east-1.amazonaws.com/dev/world functions: hello: myservice-dev-hello world: myservice-dev-world layers: None
If we use the two endpoints, we get the followings:
We can invoke the same from command line:
$ serverless invoke -f hello { "statusCode": 200, "headers": { "Content-Type": "application/json", "X-MyCompany-Func-Reply": "hello-handler" }, "multiValueHeaders": null, "body": "{\"message\":\"Go Serverless v1.0! Your function executed successfully!\"}" } $ serverless invoke -f world { "statusCode": 200, "headers": { "Content-Type": "application/json", "X-MyCompany-Func-Reply": "world-handler" }, "multiValueHeaders": null, "body": "{\"message\":\"Okay so your other function also executed successfully!\"}" }
Here is the code:
The code: hello-main.go
Continue to Serverless Web API with AWS 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