Make sure serverless
is installed. See installation guide.
Once installed the Serverless CLI can be called with serverless
or the shorthand sls
command.
$ sls
Commands
* You can run commands with "serverless" or the shortcut "sls"
* Pass "--verbose" to this command to get in-depth plugin info
* Pass "--no-color" to disable CLI colors
* Pass "--help" after any <command> for contextual help
You should also have go and make
It is good practice to organize your go
projects within GOPATH, unless using Go modules, to maximize the benefits of go tooling.
The Serverless Framework includes starter templates for various languages and providers. There are three templates for go
.
aws-go
fetches dependencies using standard go get
.
sls create --template aws-go --path myService
aws-go-dep
uses go dep and requires your project to be in $GOPATH/src
.
sls create --template aws-go-dep --path myService
aws-go-mod
uses standard Go modules and requires your project to be outside $GOPATH
and to use Go 1.13+.
sls create --template aws-go-mod --path myService
Using the create
command we can specify one of the available templates. For this example use aws-go-dep with the --template
or shorthand -t
flag.
The --path
or shorthand -p
is the location to be created with the template service files.
Change directories into 'myService' folder and you can see this project has 2 handler functions: hello
and world
split into 2 separate go packages (folders):
.
├── hello/
│ └── main.go
├── world/
│ └── main.go
This because a main()
function is required as entry point for each handler executable.
Run make build
to build both functions. Successful build should generate the following binaries:
.
├── bin/
│ |── hello
│ └── world
sls deploy
This will deploy your function to AWS Lambda based on the settings in serverless.yml
.
sls invoke -f hello
sls invoke -f world
Invoke either deployed function with command invoke
and --function
or shorthand -f
.
In your terminal window you should see the response from AWS Lambda.
serverless invoke -f hello
{
"message": "Go Serverless v1.0! Your function executed successfully!"
}
serverless invoke -f world
{
"message": "Okay so your other function also executed successfully!"
}
Congrats you have deployed and ran your Hello World function!
Product