add support for AWS go: new create templates aws-go and aws-go-dep, docs, updated integration tests

This commit is contained in:
Yun Zhi Lin 2018-01-23 23:52:51 +11:00
parent 3e717fa337
commit df85e2d9bc
21 changed files with 479 additions and 2 deletions

View File

@ -132,7 +132,7 @@ The following are services you can instantly install and use by running `serverl
## <a name="features"></a>Features
* Supports Node.js, Python, Java, Scala, C#, F#, Groovy, Kotlin, PHP & Swift.
* Supports Node.js, Python, Java, Scala, C#, F#, Go, Groovy, Kotlin, PHP & Swift.
* Manages the lifecycle of your serverless architecture (build, deploy, update, delete).
* Safely deploy functions, events and their required resources together via provider resource managers (e.g., AWS CloudFormation).
* Functions can be grouped ("serverless services") for easy management of code, resources & processes, across large projects & teams.

View File

@ -63,6 +63,16 @@ services:
image: microsoft/dotnet:1.0.4-sdk
volumes:
- ./tmp/serverless-integration-test-aws-fsharp:/app
aws-go:
image: golang:1.9
volumes:
- ./tmp/serverless-integration-test-aws-go:/app
- ./tmp/serverless-integration-test-aws-go:/go/src/app
aws-go-dep:
image: yunspace/golang:1.9
volumes:
- ./tmp/serverless-integration-test-aws-go-dep:/app
- ./tmp/serverless-integration-test-aws-go-dep:/go/src/app
aws-nodejs-typescript:
image: node:6.10.3
volumes:

View File

@ -17,6 +17,8 @@ Pick your language of choice:
* [JavaScript](./node)
* [Python](./python)
* [csharp](./csharp)
* [C#](./csharp)
* [F#](./fsharp)
* [Go](./go)
[View all examples](https://www.serverless.com/framework/docs/providers/aws/examples/)

View File

@ -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!

View File

@ -63,6 +63,7 @@ Here are the available runtimes for AWS Lambda:
* aws-scala-sbt
* aws-csharp
* aws-fsharp
* aws-go
Check out the [create command docs](../cli-reference/create) for all the details and options.

View File

@ -28,6 +28,8 @@ const validTemplates = [
'aws-scala-sbt',
'aws-csharp',
'aws-fsharp',
'aws-go',
'aws-go-dep',
'azure-nodejs',
'google-nodejs',
'kubeless-python',

View File

@ -726,5 +726,39 @@ describe('Create', () => {
expect((/service: my-awesome-service/).test(serverlessYmlfileContent)).to.equal(true);
});
});
it('should generate scaffolding for "aws-go" template', () => {
process.chdir(tmpDir);
create.options.template = 'aws-go';
return create.create().then(() => {
const dirContent = walkDirSync(tmpDir)
.map(elem => elem.replace(path.join(tmpDir, path.sep), ''));
expect(dirContent).to.include('serverless.yml');
expect(dirContent).to.include(path.join('hello', 'main.go'));
expect(dirContent).to.include(path.join('world', 'main.go'));
expect(dirContent).to.include('Makefile');
expect(dirContent).to.include('.gitignore');
});
});
it('should generate scaffolding for "aws-go-dep" template', () => {
process.chdir(tmpDir);
create.options.template = 'aws-go-dep';
return create.create().then(() => {
const dirContent = walkDirSync(tmpDir)
.map(elem => elem.replace(path.join(tmpDir, path.sep), ''));
expect(dirContent).to.include('serverless.yml');
expect(dirContent).to.include(path.join('hello', 'main.go'));
expect(dirContent).to.include(path.join('world', 'main.go'));
expect(dirContent).to.include('Gopkg.toml');
expect(dirContent).to.include('Gopkg.lock');
expect(dirContent).to.include('Makefile');
expect(dirContent).to.include('.gitignore');
});
});
});
});

View File

@ -0,0 +1,19 @@
# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'.
[[projects]]
name = "github.com/aws/aws-lambda-go"
packages = [
"lambda",
"lambda/messages",
"lambdacontext"
]
revision = "6e2e37798efbb1dfd8e9c6681702e683a6046517"
version = "v1.0.1"
[solve-meta]
analyzer-name = "dep"
analyzer-version = 1
inputs-digest = "85fa166cc59d0fa113a1517ffbb5dee0f1fa4a6795239936afb18c64364af759"
solver-name = "gps-cdcl"
solver-version = 1

View File

@ -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"

View File

@ -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

View File

@ -0,0 +1,8 @@
# Serverless directories
.serverless
# golang output binary directory
bin
# golang vendor (dependencies) directory
vendor

View File

@ -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)
}

View File

@ -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"

View File

@ -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)
}

View File

@ -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

View File

@ -0,0 +1,5 @@
# Serverless directories
.serverless
# golang output binary directory
bin

View File

@ -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)
}

View File

@ -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 # 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"

View File

@ -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)
}

View File

@ -37,5 +37,10 @@ serverless deploy -v
echo "Invoking Service"
serverless invoke --function hello
if [ $template == "aws-go" ] || [ $template == "aws-go-dep" ]
then
serverless invoke --function world
fi
echo "Removing Service"
serverless remove -v

View File

@ -10,6 +10,8 @@ function integration-test {
integration-test aws-csharp 'apt-get -qq update && apt-get -qq -y install zip && dotnet restore && dotnet lambda package --configuration release --framework netcoreapp1.0 --output-package bin/release/netcoreapp1.0/deploy-package.zip'
integration-test aws-fsharp 'apt-get -qq update && apt-get -qq -y install zip && dotnet restore && dotnet lambda package --configuration release --framework netcoreapp1.0 --output-package bin/release/netcoreapp1.0/deploy-package.zip'
integration-test aws-go 'cd /go/src/app && make build'
integration-test aws-go-dep 'cd /go/src/app && make build'
integration-test aws-groovy-gradle ./gradlew build
integration-test aws-java-gradle ./gradlew build
integration-test aws-java-maven mvn package