Skip to content

Commit

Permalink
add support for AWS go: new create templates aws-go and aws-go-dep, d…
Browse files Browse the repository at this point in the history
…ocs, updated integration tests
  • Loading branch information
Yun Zhi Lin committed Jan 23, 2018
1 parent 3e717fa commit df85e2d
Show file tree
Hide file tree
Showing 21 changed files with 479 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -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.
Expand Down
10 changes: 10 additions & 0 deletions docker-compose.yml
Expand Up @@ -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:
Expand Down
4 changes: 3 additions & 1 deletion docs/providers/aws/examples/hello-world/README.md
Expand Up @@ -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/)
72 changes: 72 additions & 0 deletions docs/providers/aws/examples/hello-world/go/README.md
@@ -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!
1 change: 1 addition & 0 deletions docs/providers/aws/guide/services.md
Expand Up @@ -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.

Expand Down
2 changes: 2 additions & 0 deletions lib/plugins/create/create.js
Expand Up @@ -28,6 +28,8 @@ const validTemplates = [
'aws-scala-sbt',
'aws-csharp',
'aws-fsharp',
'aws-go',
'aws-go-dep',
'azure-nodejs',
'google-nodejs',
'kubeless-python',
Expand Down
34 changes: 34 additions & 0 deletions lib/plugins/create/create.test.js
Expand Up @@ -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');
});
});
});
});
19 changes: 19 additions & 0 deletions lib/plugins/create/templates/aws-go-dep/Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions lib/plugins/create/templates/aws-go-dep/Gopkg.toml
@@ -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"
4 changes: 4 additions & 0 deletions lib/plugins/create/templates/aws-go-dep/Makefile
@@ -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
8 changes: 8 additions & 0 deletions lib/plugins/create/templates/aws-go-dep/gitignore
@@ -0,0 +1,8 @@
# Serverless directories
.serverless

# golang output binary directory
bin

# golang vendor (dependencies) directory
vendor
19 changes: 19 additions & 0 deletions lib/plugins/create/templates/aws-go-dep/hello/main.go
@@ -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)
}
104 changes: 104 additions & 0 deletions lib/plugins/create/templates/aws-go-dep/serverless.yml
@@ -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"
19 changes: 19 additions & 0 deletions lib/plugins/create/templates/aws-go-dep/world/main.go
@@ -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)
}
4 changes: 4 additions & 0 deletions lib/plugins/create/templates/aws-go/Makefile
@@ -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
5 changes: 5 additions & 0 deletions lib/plugins/create/templates/aws-go/gitignore
@@ -0,0 +1,5 @@
# Serverless directories
.serverless

# golang output binary directory
bin

0 comments on commit df85e2d

Please sign in to comment.