From 2f3590a9009bb7386d09b8500e8b3c23cfbeb73e Mon Sep 17 00:00:00 2001 From: Michael Hart Date: Mon, 22 Jan 2018 15:38:57 -0500 Subject: [PATCH 1/5] Add Go 1.x support --- runtime.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/runtime.go b/runtime.go index c360e0d298..a5a66a9823 100644 --- a/runtime.go +++ b/runtime.go @@ -81,6 +81,7 @@ var runtimeName = struct { python27 string python36 string java8 string + go1x string }{ nodejs: "nodejs", nodejs43: "nodejs4.3", @@ -88,6 +89,7 @@ var runtimeName = struct { python27: "python2.7", python36: "python3.6", java8: "java8", + go1x: "go1.x", } var runtimeImageFor = map[string]string{ @@ -97,6 +99,7 @@ var runtimeImageFor = map[string]string{ runtimeName.python27: "lambci/lambda:python2.7", runtimeName.python36: "lambci/lambda:python3.6", runtimeName.java8: "lambci/lambda:java8", + runtimeName.go1x: "lambci/lambda:go1.x", } // NewRuntimeOpt contains parameters that are passed to the NewRuntime method From 1cfd626a52946c38722ce1718c87b44975d53cee Mon Sep 17 00:00:00 2001 From: Paul Maddox Date: Tue, 23 Jan 2018 23:48:27 +0100 Subject: [PATCH 2/5] Fix issue where a template with no AWS::Serverless:Function.Timeout set causes AWS_LAMBDA_FUNCTION_TIMEOUT=0 to be sent to the Lambda runtime. Instead it should default to the SAM specification default (3 seconds). Also included the same default behaviour for the memory size as this was missing too. (#271) --- runtime.go | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/runtime.go b/runtime.go index a5a66a9823..bc551df2ce 100644 --- a/runtime.go +++ b/runtime.go @@ -292,6 +292,22 @@ func (r *Runtime) Invoke(event string, profile string) (io.Reader, io.Reader, er } } + // If the timeout hasn't been set for the function in the SAM template + // then default to 3 seconds (as per SAM specification). + // This needs to be done before environment variables are generated for + // the Lambda runtime so that the correct AWS_LAMBDA_FUNCTION_TIMEOUT is used + if r.Function.Timeout <= 0 { + r.Function.Timeout = 3 + } + + // If the memory size hasn't been set for the function in the SAM template + // then default to 128MB (as per SAM specification). + // This needs to be done before environment variables are generated for + // the Lambda runtime so that the correct AWS_LAMBDA_FUNCTION_MEMORY_SIZE is used + if r.Function.MemorySize <= 0 { + r.Function.MemorySize = 128 + } + // Define the container options config := &container.Config{ WorkingDir: "/var/task", @@ -363,11 +379,10 @@ func (r *Runtime) Invoke(event string, profile string) (io.Reader, io.Reader, er } func (r *Runtime) setupTimeoutTimer(stdout, stderr io.ReadCloser) { + // Start a timer, we'll use this to abort the function if it runs beyond the specified timeout - timeout := time.Duration(3) * time.Second - if r.Function.Timeout > 0 { - timeout = time.Duration(r.Function.Timeout) * time.Second - } + timeout := time.Duration(r.Function.Timeout) * time.Second + r.TimeoutTimer = time.NewTimer(timeout) go func() { <-r.TimeoutTimer.C From f1c4c4905e3ac78b5f9dc0dc15144e094da63c85 Mon Sep 17 00:00:00 2001 From: Joshua Daniel Franklin Date: Thu, 25 Jan 2018 12:06:21 -0600 Subject: [PATCH 3/5] Add samples/hello-world/golang example (#276) --- samples/hello-world/golang/README.md | 33 ++++++++++++++++++++++++ samples/hello-world/golang/event.json | 5 ++++ samples/hello-world/golang/main.go | 22 ++++++++++++++++ samples/hello-world/golang/template.yaml | 11 ++++++++ 4 files changed, 71 insertions(+) create mode 100644 samples/hello-world/golang/README.md create mode 100644 samples/hello-world/golang/event.json create mode 100644 samples/hello-world/golang/main.go create mode 100644 samples/hello-world/golang/template.yaml diff --git a/samples/hello-world/golang/README.md b/samples/hello-world/golang/README.md new file mode 100644 index 0000000000..045a7a39e0 --- /dev/null +++ b/samples/hello-world/golang/README.md @@ -0,0 +1,33 @@ +# AWS SAM Hello World Example # + +A simple AWS SAM template that specifies a single Lambda function. + +## Usage ## + +To create and deploy the SAM Hello World example, first ensure that you've met the requirements described in the [root README](../../README.md). Then follow the steps below. + +### Build your package ### + + GOOS=linux go build -o main + +### Test your application locally ### + +Use [SAM Local](https://github.com/awslabs/aws-sam-local) to run your Lambda function locally: + + sam local invoke "HelloWorld" -e event.json + +### Package artifacts ### + +Run the following command, replacing `BUCKET-NAME` with the name of your bucket: + + sam package --template-file template.yaml --s3-bucket BUCKET-NAME --output-template-file packaged-template.yaml + +This creates a new template file, packaged-template.yaml, that you will use to deploy your serverless application. + +### Deploy to AWS CloudFormation ### + +Run the following command, replacing `MY-NEW-STACK` with a name for your CloudFormation stack. + + sam deploy --template-file packaged-template.yaml --stack-name MY-NEW-STACK --capabilities CAPABILITY_IAM + +This uploads your template to an S3 bucket and deploys the specified resources using AWS CloudFormation. diff --git a/samples/hello-world/golang/event.json b/samples/hello-world/golang/event.json new file mode 100644 index 0000000000..a5fb7093c9 --- /dev/null +++ b/samples/hello-world/golang/event.json @@ -0,0 +1,5 @@ +{ + "key1": "value1", + "key2": "value2", + "key3": "value3" +} diff --git a/samples/hello-world/golang/main.go b/samples/hello-world/golang/main.go new file mode 100644 index 0000000000..9944e2aa6f --- /dev/null +++ b/samples/hello-world/golang/main.go @@ -0,0 +1,22 @@ +package main + +import ( + "fmt" + "log" + "context" + "github.com/aws/aws-lambda-go/lambda" +) + +type Event map[string]interface{} + +func HandleRequest(ctx context.Context, event Event) (string, error) { + log.Print("value1 = ", event["key1"] ) + log.Print("value2 = ", event["key2"] ) + log.Print("value3 = ", event["key3"] ) + + return fmt.Sprintf("Hello World"), nil +} + +func main() { + lambda.Start(HandleRequest) +} diff --git a/samples/hello-world/golang/template.yaml b/samples/hello-world/golang/template.yaml new file mode 100644 index 0000000000..fed1a828af --- /dev/null +++ b/samples/hello-world/golang/template.yaml @@ -0,0 +1,11 @@ +AWSTemplateFormatVersion : '2010-09-09' +Transform: AWS::Serverless-2016-10-31 + +Description: A simple Hello World Serverless project +Resources: + HelloWorld: + Type: AWS::Serverless::Function + Properties: + Runtime: go1.x + Handler: main + CodeUri: . From 4611fd7b63761603debcb7d1d51c2b7c387b04d3 Mon Sep 17 00:00:00 2001 From: Chris Munns Date: Thu, 1 Feb 2018 12:16:37 -0500 Subject: [PATCH 4/5] update README.md to add upgrade instructions per CLI's nag --- README.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/README.md b/README.md index 21d5afe4a7..57b851fadc 100644 --- a/README.md +++ b/README.md @@ -79,6 +79,14 @@ sam --version If you get a permission error when using npm (such as `EACCES: permission denied`), please see the instructions on this page of the NPM documentation: [https://docs.npmjs.com/getting-started/fixing-npm-permissions](https://docs.npmjs.com/getting-started/fixing-npm-permissions). +#### Upgrading via npm + +To update **`sam`** once installed via npm: + +```bash +npm update -g aws-sam-local +``` + ### Binary release We also release the CLI as binaries that you can download and instantly use. You can find them under [Releases] in this repo. In case you cannot find the version or architecture you're looking for you can refer to [Build From Source](#build-from-source) section for build details. @@ -97,6 +105,7 @@ This will install **`sam`** to your `$GOPATH/bin` folder. Make sure this directo aws-sam-local --help ``` + ## Usage **`sam`** requires a SAM template in order to know how to invoke your function locally, and it's also true for spawning API Gateway locally - If no template is specified `template.yaml` will be used instead. From 541fcc185254175e1f51ea6818ff09e6a7544cf3 Mon Sep 17 00:00:00 2001 From: Chris Munns Date: Thu, 1 Feb 2018 12:22:04 -0500 Subject: [PATCH 5/5] Readme.md removing space --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index 57b851fadc..95a723000e 100644 --- a/README.md +++ b/README.md @@ -105,7 +105,6 @@ This will install **`sam`** to your `$GOPATH/bin` folder. Make sure this directo aws-sam-local --help ``` - ## Usage **`sam`** requires a SAM template in order to know how to invoke your function locally, and it's also true for spawning API Gateway locally - If no template is specified `template.yaml` will be used instead.