File tree Expand file tree Collapse file tree 4 files changed +151
-0
lines changed Expand file tree Collapse file tree 4 files changed +151
-0
lines changed Original file line number Diff line number Diff line change 1+ version : 0.2
2+
3+ env :
4+ variables :
5+ # This S3 bucket will be used to store the packaged Lambda deployment bundle.
6+ # Make sure to provide a valid S3 bucket name (it must exist already).
7+ # The AWS CodeBuild IAM role must allow write access to it.
8+ S3_BUCKET : " pmaddox-public2"
9+ PACKAGE : " github.com/aws-samples/lambda-go-samples"
10+
11+ phases :
12+
13+ install :
14+ commands :
15+
16+ # AWS Codebuild Go images use /go for the $GOPATH so let's copy our
17+ # application source code into that directory structure.
18+ - mkdir -p "/go/src/$(dirname ${PACKAGE})"
19+ - ln -s "${CODEBUILD_SRC_DIR}" "/go/src/${PACKAGE}"
20+
21+ # Print all environment variables (handy for AWS CodeBuild logs)
22+ - env
23+
24+ # Install golint
25+ - go get -u github.com/golang/lint/golint
26+
27+ pre_build :
28+ commands :
29+
30+ # Make sure we're in the project directory within our GOPATH
31+ - cd "/go/src/${PACKAGE}"
32+
33+ # Fetch all dependencies
34+ - go get ./...
35+
36+ # Ensure code passes all lint tests
37+ - golint -set_exit_status
38+
39+ # Check the Go code for common problems with 'go vet'
40+ - go vet .
41+
42+ # Run all tests included with our application
43+ - go test .
44+
45+ build :
46+ commands :
47+
48+ # Build our go application
49+ - go build -o main
50+
51+ # Package our application with AWS SAM
52+ - aws cloudformation package --template-file template.yml --s3-bucket ${S3_BUCKET} --output-template-file packaged.yml
53+
54+ artifacts :
55+ files :
56+ - packaged.yml
Original file line number Diff line number Diff line change 1+
2+ package main
3+
4+ import (
5+ "errors"
6+ "log"
7+
8+ "github.com/aws/aws-lambda-go/events"
9+ "github.com/aws/aws-lambda-go/lambda"
10+ )
11+
12+ var (
13+ // ErrNameNotProvided is thrown when a name is not provided
14+ ErrNameNotProvided = errors .New ("no name was provided in the HTTP body" )
15+ )
16+
17+ // Handler is your Lambda function handler
18+ // It uses Amazon API Gateway request/responses provided by the aws-lambda-go/events package,
19+ // However you could use other event sources (S3, Kinesis etc), or JSON-decoded primitive types such as 'string'.
20+ func Handler (request events.APIGatewayProxyRequest ) (events.APIGatewayProxyResponse , error ) {
21+
22+ // stdout and stderr are sent to AWS CloudWatch Logs
23+ log .Printf ("Processing Lambda request %s\n " , request .RequestContext .RequestID )
24+
25+ // If no name is provided in the HTTP request body, throw an error
26+ if len (request .Body ) < 1 {
27+ return events.APIGatewayProxyResponse {}, ErrNameNotProvided
28+ }
29+
30+ return events.APIGatewayProxyResponse {
31+ Body : "Hello " + request .Body ,
32+ StatusCode : 200 ,
33+ }, nil
34+
35+ }
36+
37+ func main () {
38+ lambda .Start (Handler )
39+ }
Original file line number Diff line number Diff line change 1+ package main_test
2+
3+ import (
4+ "testing"
5+
6+ main "github.com/aws-samples/lambda-go-samples"
7+
8+ "github.com/aws/aws-lambda-go/events"
9+ "github.com/stretchr/testify/assert"
10+ )
11+
12+ func TestHandler (t * testing.T ) {
13+
14+ tests := []struct {
15+ request events.APIGatewayProxyRequest
16+ expect string
17+ err error
18+ }{
19+ {
20+ // Test that the handler responds with the correct response
21+ // when a valid name is provided in the HTTP body
22+ request : events.APIGatewayProxyRequest {Body : "Paul" },
23+ expect : "Hello Paul" ,
24+ err : nil ,
25+ },
26+ {
27+ // Test that the handler responds ErrNameNotProvided
28+ // when no name is provided in the HTTP body
29+ request : events.APIGatewayProxyRequest {Body : "" },
30+ expect : "" ,
31+ err : main .ErrNameNotProvided ,
32+ },
33+ }
34+
35+ for _ , test := range tests {
36+ response , err := main .Handler (test .request )
37+ assert .IsType (t , test .err , err )
38+ assert .Equal (t , test .expect , response .Body )
39+ }
40+
41+ }
Original file line number Diff line number Diff line change 1+ AWSTemplateFormatVersion : 2010-09-09
2+ Transform : AWS::Serverless-2016-10-31
3+
4+ Resources :
5+ HelloFunction :
6+ Type : AWS::Serverless::Function
7+ Properties :
8+ Handler : main
9+ Runtime : go1.x
10+ Events :
11+ GetEvent :
12+ Type : Api
13+ Properties :
14+ Path : /
15+ Method : post
You can’t perform that action at this time.
0 commit comments