From fae1d48ae4a846336b87da66c44ef7e8fa33637a Mon Sep 17 00:00:00 2001 From: Yunspace Date: Fri, 26 Jan 2018 00:30:36 +1100 Subject: [PATCH 1/8] make deploy region configurable --- deploy.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/deploy.sh b/deploy.sh index 9181d1b..2bd4213 100644 --- a/deploy.sh +++ b/deploy.sh @@ -2,6 +2,7 @@ declare -a folders=("csharp" "csharp2" "fsharp" "fsharp2" "go" "java" "python" "python3" "nodejs4" "nodejs6") export AWS_PROFILE=personal +export AWS_REGION=us-east-1 for i in `seq 1 10`; do @@ -10,7 +11,7 @@ do cd $folder pwd - sls deploy --region ap-southeast-2 + sls deploy --region $AWS_REGION cd .. done From 3522d590515d3ee68a405c6d1f9122717edcaec3 Mon Sep 17 00:00:00 2001 From: Yunspace Date: Sun, 28 Jan 2018 00:43:56 +1100 Subject: [PATCH 2/8] added aws-python-go based on the now deprecated eawsy python shim --- deploy.sh | 6 ++-- python-go/.env.example | 16 +++++++++ python-go/.gitignore | 5 +++ python-go/Gopkg.lock | 36 ++++++++++++++++++++ python-go/Gopkg.toml | 23 +++++++++++++ python-go/Makefile | 66 ++++++++++++++++++++++++++++++++++++ python-go/README.md | 43 +++++++++++++++++++++++ python-go/docker-compose.yml | 9 +++++ python-go/handler.go | 13 +++++++ python-go/serverless.yml | 15 ++++++++ 10 files changed, 229 insertions(+), 3 deletions(-) create mode 100644 python-go/.env.example create mode 100644 python-go/.gitignore create mode 100644 python-go/Gopkg.lock create mode 100644 python-go/Gopkg.toml create mode 100644 python-go/Makefile create mode 100644 python-go/README.md create mode 100644 python-go/docker-compose.yml create mode 100644 python-go/handler.go create mode 100644 python-go/serverless.yml diff --git a/deploy.sh b/deploy.sh index 2bd4213..3c19eff 100644 --- a/deploy.sh +++ b/deploy.sh @@ -1,10 +1,10 @@ #!/bin/bash -declare -a folders=("csharp" "csharp2" "fsharp" "fsharp2" "go" "java" "python" "python3" "nodejs4" "nodejs6") +declare -a folders=("csharp" "csharp2" "fsharp" "fsharp2" "go" "java" "python" "python3" "nodejs4" "nodejs6", "python-go") export AWS_PROFILE=personal export AWS_REGION=us-east-1 -for i in `seq 1 10`; +for i in `seq 1 11`; do for folder in "${folders[@]}" do @@ -16,4 +16,4 @@ do cd .. done -done \ No newline at end of file +done diff --git a/python-go/.env.example b/python-go/.env.example new file mode 100644 index 0000000..36a8419 --- /dev/null +++ b/python-go/.env.example @@ -0,0 +1,16 @@ +# .env.example to illustrate possible values + +# Serverless +ENV=dev + +# Golang +WORKDIR=/go/src/github.com/yunspace/lambda-platform-perf-comparison/python-go + +# AWS +AWS_ACCESS_KEY_ID +AWS_SECRET_ACCESS_KEY +AWS_REGION=ap-southeast-2 + +# Optional AWS STS fields if you use assume role +AWS_SESSION_TOKEN +AWS_SECURITY_TOKEN diff --git a/python-go/.gitignore b/python-go/.gitignore new file mode 100644 index 0000000..74b77fc --- /dev/null +++ b/python-go/.gitignore @@ -0,0 +1,5 @@ +.env +.serverless +*.so +*.zip +vendor \ No newline at end of file diff --git a/python-go/Gopkg.lock b/python-go/Gopkg.lock new file mode 100644 index 0000000..38fe70d --- /dev/null +++ b/python-go/Gopkg.lock @@ -0,0 +1,36 @@ +# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'. + + +[[projects]] + branch = "master" + name = "github.com/eawsy/aws-lambda-go-core" + packages = ["service/lambda/runtime"] + revision = "e26eed6aa244a3d45aa693816a9c5faf39390fcd" + +[[projects]] + branch = "master" + name = "github.com/eawsy/aws-lambda-go-event" + packages = ["service/lambda/runtime/event/apigatewayproxyevt"] + revision = "a318481f661f091bafea408f9967cb1ac5bf8905" + +[[projects]] + name = "github.com/satori/go.uuid" + packages = ["."] + revision = "879c5887cd475cd7864858769793b2ceb0d44feb" + version = "v1.1.0" + +[[projects]] + branch = "master" + name = "github.com/yunspace/serverless-golang" + packages = [ + "api", + "aws/event/apigateway" + ] + revision = "5e18759e8f359749e77e8b82e49612358f265920" + +[solve-meta] + analyzer-name = "dep" + analyzer-version = 1 + inputs-digest = "fa20ce0948eee3badc0d0a27d1a918333e65d55e20c16e245cda7e795e9f59d4" + solver-name = "gps-cdcl" + solver-version = 1 diff --git a/python-go/Gopkg.toml b/python-go/Gopkg.toml new file mode 100644 index 0000000..e60b3a5 --- /dev/null +++ b/python-go/Gopkg.toml @@ -0,0 +1,23 @@ +[[constraint]] + name = "github.com/davecgh/go-spew" + version = "1.1.0" + +[[constraint]] + branch = "master" + name = "github.com/eawsy/aws-lambda-go-core" + +[[constraint]] + branch = "master" + name = "github.com/eawsy/aws-lambda-go-event" + +[[constraint]] + name = "github.com/pmezard/go-difflib" + version = "1.0.0" + +[[constraint]] + branch = "master" + name = "github.com/stretchr/testify" + +[[constraint]] + branch = "master" + name = "github.com/yunspace/serverless-golang" diff --git a/python-go/Makefile b/python-go/Makefile new file mode 100644 index 0000000..7e81a6d --- /dev/null +++ b/python-go/Makefile @@ -0,0 +1,66 @@ +HANDLER=handler +PACKAGE=package +ifdef DOTENV + DOTENV_TARGET=dotenv +else + DOTENV_TARGET=.env +endif + +######################################## +# entrypoints - run within docker only # +######################################## +deps: + docker-compose run --rm sls-go make _deps +.PHONY: deps + +build: $(DOTENV_TARGET) _clean deps + docker-compose run --rm sls-go make _build +.PHONY: build + +test: $(DOTENV_TARGET) deps + docker-compose run --rm sls-go make _test +.PHONY: test + +deploy: $(DOTENV_TARGET) + docker-compose run --rm sls-go make _deploy +.PHONY: deploy + +remove: $(DOTENV_TARGET) + docker-compose run --rm sls-go make _remove +.PHONY: remove + +shell: $(DOTENV_TARGET) + docker-compose run --rm sls-go bash +.PHONY: shell + +######################################## +# others - run within or out of docker # +######################################## +.env: + @echo "Create .env with .env.template" + cp .env.template .env + +dotenv: + @echo "Switching .env to $(DOTENV)" + cp $(DOTENV) .env + +# target to run within container +_clean: + @rm -rf .serverless $(HANDLER).zip $(HANDLER).so $(PACKAGE).zip + +_deps: + @dep ensure + +_build: + @go build -buildmode=plugin -ldflags='-w -s' -o $(HANDLER).so + @pack $(HANDLER) $(HANDLER).so $(PACKAGE).zip + @chown $(shell stat -c '%u:%g' .) $(HANDLER).so $(PACKAGE).zip + +_test: + @go test $(shell go list ./... | grep -v /vendor/) + +_deploy: + @sls deploy -v + +_remove: + @sls remove -v diff --git a/python-go/README.md b/python-go/README.md new file mode 100644 index 0000000..fd0c9ea --- /dev/null +++ b/python-go/README.md @@ -0,0 +1,43 @@ +# Serverless AWS Golang Event +Serverless AWS APIGateway events example using: + +- [AWS Lambda Go Shim](https://github.com/eawsy/aws-lambda-go-shim) +- [API Gateway Proxy Event](https://github.com/eawsy/aws-lambda-go-event/tree/master/service/lambda/runtime/event/apigatewayproxyevt) + +Each CRUD operation is it's own Lambda Function. This is convenient to hook into other +Event Source triggers such as `Kinesis` or `SNS`. + +## Usage +Setup and deploy a new project called `your-app`: + +```bash +cd $GOPATH/src/your-path/ +serverless install -u https://github.com/yunspace/serverless-golang/tree/master/examples/aws-golang-event -n your-app +``` + +```bash +cd +make DOTENV=.env.example dotenv + +``` +* fill in and correct any of the variables in .env +* replace `WORKDIR` in .env with `/go/src/your-path/your-app` + +```bash +make test build deploy +``` + +use curl, PostMan or any REST client and do a `POST` on the provided gateway endpoint: + +```json +{ + "id": "c576e9bc-548e-457d-b662-254ade7fc695", + "message": "hello world" +} +``` + +should get the same payload back with status `200` + +```bash +make remove +``` diff --git a/python-go/docker-compose.yml b/python-go/docker-compose.yml new file mode 100644 index 0000000..5754f6a --- /dev/null +++ b/python-go/docker-compose.yml @@ -0,0 +1,9 @@ +version: '2.1' +services: + sls-go: + image: yunspace/serverless-golang:1.25.0 + env_file: .env + volumes: + - .:${WORKDIR} + - ~/.aws:/root/.aws + working_dir: ${WORKDIR} \ No newline at end of file diff --git a/python-go/handler.go b/python-go/handler.go new file mode 100644 index 0000000..5f53e55 --- /dev/null +++ b/python-go/handler.go @@ -0,0 +1,13 @@ +package main + +import ( + "github.com/eawsy/aws-lambda-go-core/service/lambda/runtime" + "github.com/eawsy/aws-lambda-go-event/service/lambda/runtime/event/apigatewayproxyevt" + "github.com/yunspace/serverless-golang/aws/event/apigateway" +) + +func Get(evt *apigatewayproxyevt.Event, _ *runtime.Context) (interface{}, error) { + r := apigateway.NewAPIGatewayResponse(200) + r.Body = "hello" + return r, nil +} diff --git a/python-go/serverless.yml b/python-go/serverless.yml new file mode 100644 index 0000000..f1291bb --- /dev/null +++ b/python-go/serverless.yml @@ -0,0 +1,15 @@ +service: aws-python-go +package: + artifact: package.zip +provider: + name: aws + runtime: python2.7 + stage: dev + region: ${env:AWS_REGION} +functions: + get: + handler: handler.Get + events: + - http: + path: / + method: get \ No newline at end of file From 217abd4e47b0ab6bdc8fbf8e8965dc4e148a3d1a Mon Sep 17 00:00:00 2001 From: Yunspace Date: Sun, 18 Feb 2018 15:19:10 +1100 Subject: [PATCH 3/8] added linker flags to go --- deploy.sh | 38 +++++++++++++++++++------------------- go/Makefile | 2 +- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/deploy.sh b/deploy.sh index 3c19eff..5dcc973 100644 --- a/deploy.sh +++ b/deploy.sh @@ -1,19 +1,19 @@ -#!/bin/bash -declare -a folders=("csharp" "csharp2" "fsharp" "fsharp2" "go" "java" "python" "python3" "nodejs4" "nodejs6", "python-go") - -export AWS_PROFILE=personal -export AWS_REGION=us-east-1 - -for i in `seq 1 11`; -do - for folder in "${folders[@]}" - do - cd $folder - pwd - - sls deploy --region $AWS_REGION - - cd .. - done - -done +#!/bin/bash +declare -a folders=("csharp" "csharp2" "fsharp" "fsharp2" "go" "java" "python" "python3" "nodejs4" "nodejs6", "python-go") + +export AWS_PROFILE=default +export AWS_REGION=us-east-1 + +for i in `seq 1 11`; +do + for folder in "${folders[@]}" + do + cd $folder + pwd + + sls deploy --region $AWS_REGION + + cd .. + done + +done diff --git a/go/Makefile b/go/Makefile index 1c23aaa..101449b 100644 --- a/go/Makefile +++ b/go/Makefile @@ -1,4 +1,4 @@ build: go get github.com/aws/aws-lambda-go/lambda go get github.com/aws/aws-lambda-go/events - env GOOS=linux go build -o bin/main \ No newline at end of file + env GOOS=linux go build -ldflags='-w -s' -o bin/main \ No newline at end of file From 02cddae9233e597cf77c732ef202e2b9345df1d5 Mon Sep 17 00:00:00 2001 From: Yun Zhi Lin Date: Sun, 18 Feb 2018 15:30:01 +1100 Subject: [PATCH 4/8] corrected python go function name --- python-go/serverless.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python-go/serverless.yml b/python-go/serverless.yml index f1291bb..2650e92 100644 --- a/python-go/serverless.yml +++ b/python-go/serverless.yml @@ -7,7 +7,7 @@ provider: stage: dev region: ${env:AWS_REGION} functions: - get: + hello: handler: handler.Get events: - http: From 15e0a913362577fb748908d80151c5c1317bf45f Mon Sep 17 00:00:00 2001 From: Yunspace Date: Sat, 24 Feb 2018 19:32:56 +1100 Subject: [PATCH 5/8] remove unused open statement in fsharp --- fsharp/Handler.fs | 4 ---- fsharp2/Handler.fs | 4 ---- 2 files changed, 8 deletions(-) diff --git a/fsharp/Handler.fs b/fsharp/Handler.fs index cc14f25..3657ed7 100755 --- a/fsharp/Handler.fs +++ b/fsharp/Handler.fs @@ -6,10 +6,6 @@ open Amazon.Lambda.Core do () module Handler = - open System - open System.IO - open System.Text - type Response = { statusCode : int; body : string } let hello(): Response = { diff --git a/fsharp2/Handler.fs b/fsharp2/Handler.fs index cc14f25..3657ed7 100644 --- a/fsharp2/Handler.fs +++ b/fsharp2/Handler.fs @@ -6,10 +6,6 @@ open Amazon.Lambda.Core do () module Handler = - open System - open System.IO - open System.Text - type Response = { statusCode : int; body : string } let hello(): Response = { From cb0f77f9f32ec7163fc5f2eaaf8f1416ca059a3e Mon Sep 17 00:00:00 2001 From: Yunspace Date: Sun, 4 Mar 2018 20:45:34 +1100 Subject: [PATCH 6/8] return pointer instead of struct. No diff to performance --- go/Makefile | 5 ++++- go/main.go | 4 ++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/go/Makefile b/go/Makefile index 101449b..6c7d24b 100644 --- a/go/Makefile +++ b/go/Makefile @@ -1,4 +1,7 @@ build: go get github.com/aws/aws-lambda-go/lambda go get github.com/aws/aws-lambda-go/events - env GOOS=linux go build -ldflags='-w -s' -o bin/main \ No newline at end of file + env GOOS=linux go build -ldflags='-w -s' -o bin/main + +compress: + upx --brute bin/main \ No newline at end of file diff --git a/go/main.go b/go/main.go index 84b6f58..ae30fbf 100644 --- a/go/main.go +++ b/go/main.go @@ -7,8 +7,8 @@ import ( "github.com/aws/aws-lambda-go/lambda" ) -func Handler(ctx context.Context, request *events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) { - return events.APIGatewayProxyResponse{Body: "hello", StatusCode: 200}, nil +func Handler(ctx context.Context, request *events.APIGatewayProxyRequest) (*events.APIGatewayProxyResponse, error) { + return &events.APIGatewayProxyResponse{Body: "hello", StatusCode: 200}, nil } func main() { From 1c096cc824639fe9ae5e877bd658a946ed7e452f Mon Sep 17 00:00:00 2001 From: coestre Date: Thu, 8 Mar 2018 15:23:56 -0600 Subject: [PATCH 7/8] Refactor: cleanup java example to simplify similar to other languages --- .gitignore | 2 + java/hello.iml | 7 +- java/pom.xml | 20 --- .../com/serverless/ApiGatewayResponse.java | 129 ------------------ .../src/main/java/com/serverless/Handler.java | 17 +-- .../main/java/com/serverless/Response.java | 24 ++-- java/src/main/resources/log4j.properties | 6 - 7 files changed, 22 insertions(+), 183 deletions(-) delete mode 100644 java/src/main/java/com/serverless/ApiGatewayResponse.java delete mode 100644 java/src/main/resources/log4j.properties diff --git a/.gitignore b/.gitignore index dca9d31..c5cc947 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,5 @@ java/target/ apis.txt +*.iml +*.ipr diff --git a/java/hello.iml b/java/hello.iml index 928d292..48ad464 100644 --- a/java/hello.iml +++ b/java/hello.iml @@ -1,6 +1,6 @@ - + @@ -11,10 +11,5 @@ - - - - - \ No newline at end of file diff --git a/java/pom.xml b/java/pom.xml index 4bafb38..cea594c 100644 --- a/java/pom.xml +++ b/java/pom.xml @@ -19,26 +19,6 @@ aws-lambda-java-core 1.1.0 - - com.amazonaws - aws-lambda-java-log4j - 1.0.0 - - - com.fasterxml.jackson.core - jackson-core - 2.8.5 - - - com.fasterxml.jackson.core - jackson-databind - 2.8.5 - - - com.fasterxml.jackson.core - jackson-annotations - 2.8.5 - diff --git a/java/src/main/java/com/serverless/ApiGatewayResponse.java b/java/src/main/java/com/serverless/ApiGatewayResponse.java deleted file mode 100644 index cb25c5d..0000000 --- a/java/src/main/java/com/serverless/ApiGatewayResponse.java +++ /dev/null @@ -1,129 +0,0 @@ -package com.serverless; - -import java.nio.charset.StandardCharsets; -import java.util.Base64; -import java.util.Collections; -import java.util.Map; - -import org.apache.log4j.Logger; - -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; - -public class ApiGatewayResponse { - - private final int statusCode; - private final String body; - private final Map headers; - private final boolean isBase64Encoded; - - public ApiGatewayResponse(int statusCode, String body, Map headers, boolean isBase64Encoded) { - this.statusCode = statusCode; - this.body = body; - this.headers = headers; - this.isBase64Encoded = isBase64Encoded; - } - - public int getStatusCode() { - return statusCode; - } - - public String getBody() { - return body; - } - - public Map getHeaders() { - return headers; - } - - // API Gateway expects the property to be called "isBase64Encoded" => isIs - public boolean isIsBase64Encoded() { - return isBase64Encoded; - } - - public static Builder builder() { - return new Builder(); - } - - public static class Builder { - - private static final Logger LOG = Logger.getLogger(ApiGatewayResponse.Builder.class); - - private static final ObjectMapper objectMapper = new ObjectMapper(); - - private int statusCode = 200; - private Map headers = Collections.emptyMap(); - private String rawBody; - private Object objectBody; - private byte[] binaryBody; - private boolean base64Encoded; - - public Builder setStatusCode(int statusCode) { - this.statusCode = statusCode; - return this; - } - - public Builder setHeaders(Map headers) { - this.headers = headers; - return this; - } - - /** - * Builds the {@link ApiGatewayResponse} using the passed raw body string. - */ - public Builder setRawBody(String rawBody) { - this.rawBody = rawBody; - return this; - } - - /** - * Builds the {@link ApiGatewayResponse} using the passed object body - * converted to JSON. - */ - public Builder setObjectBody(Object objectBody) { - this.objectBody = objectBody; - return this; - } - - /** - * Builds the {@link ApiGatewayResponse} using the passed binary body - * encoded as base64. {@link #setBase64Encoded(boolean) - * setBase64Encoded(true)} will be in invoked automatically. - */ - public Builder setBinaryBody(byte[] binaryBody) { - this.binaryBody = binaryBody; - setBase64Encoded(true); - return this; - } - - /** - * A binary or rather a base64encoded responses requires - *
    - *
  1. "Binary Media Types" to be configured in API Gateway - *
  2. a request with an "Accept" header set to one of the "Binary Media - * Types" - *
- */ - public Builder setBase64Encoded(boolean base64Encoded) { - this.base64Encoded = base64Encoded; - return this; - } - - public ApiGatewayResponse build() { - String body = null; - if (rawBody != null) { - body = rawBody; - } else if (objectBody != null) { - try { - body = objectMapper.writeValueAsString(objectBody); - } catch (JsonProcessingException e) { - LOG.error("failed to serialize object", e); - throw new RuntimeException(e); - } - } else if (binaryBody != null) { - body = new String(Base64.getEncoder().encode(binaryBody), StandardCharsets.UTF_8); - } - return new ApiGatewayResponse(statusCode, body, headers, base64Encoded); - } - } -} diff --git a/java/src/main/java/com/serverless/Handler.java b/java/src/main/java/com/serverless/Handler.java index 114ef69..904a086 100644 --- a/java/src/main/java/com/serverless/Handler.java +++ b/java/src/main/java/com/serverless/Handler.java @@ -1,17 +1,14 @@ package com.serverless; -import java.util.Map; - import com.amazonaws.services.lambda.runtime.Context; import com.amazonaws.services.lambda.runtime.RequestHandler; -public class Handler implements RequestHandler, ApiGatewayResponse> { +import java.util.Map; + +public class Handler implements RequestHandler, Response> { - @Override - public ApiGatewayResponse handleRequest(Map input, Context context) { - return ApiGatewayResponse.builder() - .setStatusCode(200) - .setObjectBody("hello") - .build(); - } + @Override + public Response handleRequest(Map input, Context context) { + return new Response("hello", 200); + } } \ No newline at end of file diff --git a/java/src/main/java/com/serverless/Response.java b/java/src/main/java/com/serverless/Response.java index cc4f22f..2e7a30c 100644 --- a/java/src/main/java/com/serverless/Response.java +++ b/java/src/main/java/com/serverless/Response.java @@ -2,19 +2,19 @@ public class Response { - private final String body; - private final Integer status; + private final String body; + private final Integer status; - public Response(String body, Integer status) { - this.body = body; - this.status = status; - } + Response(String body, Integer status) { + this.body = body; + this.status = status; + } - public String getBody() { - return this.body; - } + public String getBody() { + return this.body; + } - public Integer getInput() { - return this.status; - } + public Integer getInput() { + return this.status; + } } diff --git a/java/src/main/resources/log4j.properties b/java/src/main/resources/log4j.properties deleted file mode 100644 index 63904cf..0000000 --- a/java/src/main/resources/log4j.properties +++ /dev/null @@ -1,6 +0,0 @@ -log = . -log4j.rootLogger = DEBUG, LAMBDA - -log4j.appender.LAMBDA=com.amazonaws.services.lambda.runtime.log4j.LambdaAppender -log4j.appender.LAMBDA.layout=org.apache.log4j.PatternLayout -log4j.appender.LAMBDA.layout.conversionPattern=%d{yyyy-MM-dd HH:mm:ss} <%X{AWSRequestId}> %-5p %c:%L - %m%n From dd846535cc0162bbbb58440e7ef579ede1efa458 Mon Sep 17 00:00:00 2001 From: Christian Oestreich Date: Thu, 8 Mar 2018 15:31:50 -0600 Subject: [PATCH 8/8] Delete iml file as that should be excluded --- java/hello.iml | 15 --------------- 1 file changed, 15 deletions(-) delete mode 100644 java/hello.iml diff --git a/java/hello.iml b/java/hello.iml deleted file mode 100644 index 48ad464..0000000 --- a/java/hello.iml +++ /dev/null @@ -1,15 +0,0 @@ - - - - - - - - - - - - - - - \ No newline at end of file