Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add support for AWS go: new create templates aws-go and aws-go-dep, d…
…ocs, updated integration tests
- Loading branch information
Yun Zhi Lin
committed
Jan 23, 2018
1 parent
3e717fa
commit df85e2d
Showing
21 changed files
with
479 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<!-- | ||
title: Hello World Go Example | ||
menuText: Hello World Go Example | ||
description: Create a Go Hello World Lambda function | ||
layout: Doc | ||
--> | ||
|
||
<!-- DOCS-SITE-LINK:START automatically geneated --> | ||
### [Read this on the main serverless docs site](https://www.serverless.com/framework/docs/providers/aws/examples/hello-world/go/) | ||
<!-- DOCS-SITE-LINK:END --> | ||
|
||
# Hello World Go Example | ||
|
||
Make sure `serverless` is installed. [See installation guide](../../../guide/installation.md). | ||
|
||
You should also have [go](https://golang.org/doc/install) and [make](https://www.gnu.org/software/make/) | ||
|
||
It is always good practice to organise your `go` projects within [GOPATH](https://golang.org/doc/code.html#GOPATH), to maximise the benefits of go tooling. | ||
|
||
## 1. Create a service | ||
There are two templates for `go`: | ||
|
||
1. [aws-go](https://github.com/serverless/serverless/tree/master/lib/plugins/create/templates/aws-go) - `serverless create --template aws-go --path myService` | ||
2. [aws-go-dep](https://github.com/serverless/serverless/tree/master/lib/plugins/create/templates/aws-go-dep) - `serverless create --template aws-go-dep --path myService` | ||
|
||
where: | ||
- 'aws-go' fetches dependencies using standard `go get`. | ||
- 'aws-go-dep' uses [go dep](https://github.com/golang/dep) and requires your project to be in `$GOPATH/src` | ||
- 'myService' is a new folder to be created with 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. | ||
|
||
## 2. Build using go build to create static binaries | ||
|
||
Run `make build` to build both functions. Successful build should generate the following binaries: | ||
|
||
``` | ||
. | ||
├── bin/ | ||
│ |── hello | ||
│ └── world | ||
``` | ||
|
||
## 3. Deploy | ||
`serverless deploy` or `sls deploy`. `sls` is shorthand for the Serverless CLI command | ||
|
||
## 4. Invoke deployed function | ||
Invoking the both functions should return a successful results: | ||
|
||
```bash | ||
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 just deployed and run your Hello World function! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Gopkg.toml example | ||
# | ||
# Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md | ||
# for detailed Gopkg.toml documentation. | ||
# | ||
# required = ["github.com/user/thing/cmd/thing"] | ||
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"] | ||
# | ||
# [[constraint]] | ||
# name = "github.com/user/project" | ||
# version = "1.0.0" | ||
# | ||
# [[constraint]] | ||
# name = "github.com/user/project2" | ||
# branch = "dev" | ||
# source = "github.com/myfork/project2" | ||
# | ||
# [[override]] | ||
# name = "github.com/x/y" | ||
# version = "2.4.0" | ||
|
||
|
||
[[constraint]] | ||
name = "github.com/aws/aws-lambda-go" | ||
version = "^1.0.1" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
build: | ||
dep ensure | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# Serverless directories | ||
.serverless | ||
|
||
# golang output binary directory | ||
bin | ||
|
||
# golang vendor (dependencies) directory | ||
vendor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/aws/aws-lambda-go/lambda" | ||
) | ||
|
||
type Response struct { | ||
Message string `json:"message"` | ||
} | ||
|
||
func Handler() (Response, error) { | ||
return Response{ | ||
Message: "Go Serverless v1.0! Your function executed successfully!", | ||
}, nil | ||
} | ||
|
||
func main() { | ||
lambda.Start(Handler) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
# Welcome to Serverless! | ||
# | ||
# This file is the main config file for your service. | ||
# It's very minimal at this point and uses default values. | ||
# You can always add more config options for more control. | ||
# We've included some commented out config examples here. | ||
# Just uncomment any of them to get that config option. | ||
# | ||
# For full config options, check the docs: | ||
# docs.serverless.com | ||
# | ||
# Happy Coding! | ||
|
||
service: aws-go-dep # NOTE: update this with your service name | ||
|
||
# You can pin your service to only deploy with a specific Serverless version | ||
# Check out our docs for more details | ||
# frameworkVersion: "=X.X.X" | ||
|
||
provider: | ||
name: aws | ||
runtime: go1.x | ||
|
||
# you can overwrite defaults here | ||
# stage: dev | ||
# region: us-east-1 | ||
|
||
# you can add statements to the Lambda function's IAM Role here | ||
# iamRoleStatements: | ||
# - Effect: "Allow" | ||
# Action: | ||
# - "s3:ListBucket" | ||
# Resource: { "Fn::Join" : ["", ["arn:aws:s3:::", { "Ref" : "ServerlessDeploymentBucket" } ] ] } | ||
# - Effect: "Allow" | ||
# Action: | ||
# - "s3:PutObject" | ||
# Resource: | ||
# Fn::Join: | ||
# - "" | ||
# - - "arn:aws:s3:::" | ||
# - "Ref" : "ServerlessDeploymentBucket" | ||
# - "/*" | ||
|
||
# you can define service wide environment variables here | ||
# environment: | ||
# variable1: value1 | ||
|
||
package: | ||
exclude: | ||
- ./** | ||
include: | ||
- ./bin/** | ||
|
||
functions: | ||
hello: | ||
handler: bin/hello | ||
world: | ||
handler: bin/world | ||
|
||
# The following are a few example events you can configure | ||
# NOTE: Please make sure to change your handler code to work with those events | ||
# Check the event documentation for details | ||
# events: | ||
# events: | ||
# - http: | ||
# path: users/create | ||
# method: get | ||
# - s3: ${env:BUCKET} | ||
# - schedule: rate(10 minutes) | ||
# - sns: greeter-topic | ||
# - stream: arn:aws:dynamodb:region:XXXXXX:table/foo/stream/1970-01-01T00:00:00.000 | ||
# - alexaSkill | ||
# - alexaSmartHome: amzn1.ask.skill.xx-xx-xx-xx | ||
# - iot: | ||
# sql: "SELECT * FROM 'some_topic'" | ||
# - cloudwatchEvent: | ||
# event: | ||
# source: | ||
# - "aws.ec2" | ||
# detail-type: | ||
# - "EC2 Instance State-change Notification" | ||
# detail: | ||
# state: | ||
# - pending | ||
# - cloudwatchLog: '/aws/lambda/hello' | ||
# - cognitoUserPool: | ||
# pool: MyUserPool | ||
# trigger: PreSignUp | ||
|
||
# Define function environment variables here | ||
# environment: | ||
# variable2: value2 | ||
|
||
# you can add CloudFormation resource templates here | ||
#resources: | ||
# Resources: | ||
# NewResource: | ||
# Type: AWS::S3::Bucket | ||
# Properties: | ||
# BucketName: my-new-bucket | ||
# Outputs: | ||
# NewOutput: | ||
# Description: "Description for the output" | ||
# Value: "Some output value" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/aws/aws-lambda-go/lambda" | ||
) | ||
|
||
type Response struct { | ||
Message string `json:"message"` | ||
} | ||
|
||
func Handler() (Response, error) { | ||
return Response{ | ||
Message: "Okay so your other function also executed successfully!", | ||
}, nil | ||
} | ||
|
||
func main() { | ||
lambda.Start(Handler) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
build: | ||
go get github.com/aws/aws-lambda-go/lambda | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Serverless directories | ||
.serverless | ||
|
||
# golang output binary directory | ||
bin |
Oops, something went wrong.