Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
bmoffatt committed Oct 23, 2018
2 parents a5e9c6a + df9f95d commit 93d59f2
Show file tree
Hide file tree
Showing 37 changed files with 940 additions and 68 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ before_install:

install:
- dep ensure
- go get github.com/golang/lint/golint
- go get golang.org/x/lint/golint
- go get github.com/haya14busa/goverage

matrix:
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ in cmd.exe:
``` bat
set GOOS=linux
set GOARCH=amd64
set CGO_ENABLED=0
go build -o main main.go
%USERPROFILE%\Go\bin\build-lambda-zip.exe -o main.zip main
```
Expand All @@ -72,6 +73,7 @@ in Powershell:
``` posh
$env:GOOS = "linux"
$env:GOARCH = "amd64"
$env:CGO_ENABLED = "0"
go build -o main main.go
~\Go\Bin\build-lambda-zip.exe -o main.zip main
```
Expand Down
10 changes: 4 additions & 6 deletions cfn/wrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,16 @@ func lambdaWrapWithClient(lambdaFunction CustomResourceFunction, client httpClie
fn = func(ctx context.Context, event Event) (reason string, err error) {
r := NewResponse(&event)
r.PhysicalResourceID, r.Data, err = lambdaFunction(ctx, event)

if r.PhysicalResourceID == "" {
log.Println("PhysicalResourceID must exist, copying Log Stream name")
r.PhysicalResourceID = lambdacontext.LogStreamName
}
if err != nil {
r.Status = StatusFailed
r.Reason = err.Error()
log.Printf("sending status failed: %s", r.Reason)
} else {
r.Status = StatusSuccess

if event.RequestType == RequestCreate && r.PhysicalResourceID == "" {
log.Println("PhysicalResourceID must exist on creation, copying Log Stream name")
r.PhysicalResourceID = lambdacontext.LogStreamName
}
}

err = r.sendWith(client)
Expand Down
38 changes: 31 additions & 7 deletions cmd/build-lambda-zip/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ import (
func main() {
app := cli.NewApp()
app.Name = "build-lambda-zip"
app.Usage = "Put an executable into a zip file that works with AWS Lambda."
app.Usage = "Put an executable and supplemental files into a zip file that works with AWS Lambda."
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "output, o",
Value: "",
Usage: "output file path for the zip. Defaults to the input file name.",
Usage: "output file path for the zip. Defaults to the first input file name.",
},
}

Expand All @@ -34,7 +34,7 @@ func main() {
outputZip = fmt.Sprintf("%s.zip", filepath.Base(inputExe))
}

if err := compressExe(outputZip, inputExe); err != nil {
if err := compressExeAndArgs(outputZip, inputExe, c.Args().Tail()); err != nil {
return fmt.Errorf("Failed to compress file: %v", err)
}
return nil
Expand All @@ -61,20 +61,44 @@ func writeExe(writer *zip.Writer, pathInZip string, data []byte) error {
return err
}

func compressExe(outZipPath, exePath string) error {
func compressExeAndArgs(outZipPath string, exePath string, args []string) error {
zipFile, err := os.Create(outZipPath)
if err != nil {
return err
}
defer zipFile.Close()
defer func() {
closeErr := zipFile.Close()
if closeErr != nil {
fmt.Fprintf(os.Stderr, "Failed to close zip file: %v\n", closeErr)
}
return
}()

zipWriter := zip.NewWriter(zipFile)
defer zipWriter.Close()

data, err := ioutil.ReadFile(exePath)
if err != nil {
return err
}

return writeExe(zipWriter, filepath.Base(exePath), data)
err = writeExe(zipWriter, filepath.Base(exePath), data)
if err != nil {
return err
}

for _, arg := range args {
writer, err := zipWriter.Create(arg)
if err != nil {
return err
}
data, err := ioutil.ReadFile(arg)
if err != nil {
return err
}
_, err = writer.Write(data)
if err != nil {
return err
}
}
return err
}
2 changes: 2 additions & 0 deletions events/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,5 @@ This package provides input types for Lambda functions that process AWS events.
[S3 Events](README_S3.md)

[SNS Events](README_SNS.md)

[SQS Events](README_SQS.md)
16 changes: 13 additions & 3 deletions events/README_Cognito.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
# Sample Function

The following is a sample Lambda function that receives Amazon Cognito event record data as an input and writes some of the record data to CloudWatch Logs. (Note that by default anything written to Console will be logged as CloudWatch Logs events.)
The following is a sample Lambda function that receives Amazon Cognito Sync event record data as an input and writes some of the record data to CloudWatch Logs. (Note that by default anything written to Console will be logged as CloudWatch Logs events.)

```go

package main

import (
"strings"
"fmt"

"github.com/aws/aws-lambda-go/lambda"
"github.com/aws/aws-lambda-go/events"
)

func handleRequest(ctx context.Context, cognitoEvent events.CognitoEvent) {
func handler(cognitoEvent events.CognitoEvent) error {
for datasetName, datasetRecord := range cognitoEvent.DatasetRecords {
fmt.Printf("[%s -- %s] %s -> %s -> %s \n",
cognitoEvent.EventType,
Expand All @@ -18,5 +22,11 @@ func handleRequest(ctx context.Context, cognitoEvent events.CognitoEvent) {
datasetRecord.Op,
datasetRecord.NewValue)
}
return nil
}

func main() {
lambda.Start(handler)
}

```
25 changes: 25 additions & 0 deletions events/README_Cognito_UserPools_PostConfirmation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Sample Function

The following is a sample Lambda function that receives Amazon Cognito User Pools post-confirmation event as an input and writes some of the record data to CloudWatch Logs. (Note that by default anything written to Console will be logged as CloudWatch Logs events.)

Please see instructions for setting up the Cognito triggers at https://docs.aws.amazon.com/cognito/latest/developerguide/cognito-user-identity-pools-working-with-aws-lambda-triggers.html .

```go
package main

import (
"fmt"

"github.com/aws/aws-lambda-go/lambda"
"github.com/aws/aws-lambda-go/events"
)

func handler(event events.CognitoEventUserPoolsPostConfirmation) (events.CognitoEventUserPoolsPostConfirmation, error) {
fmt.Printf("PostConfirmation for user: %s\n", event.UserName)
return event, nil
}

func main() {
lambda.Start(handler)
}
```
26 changes: 26 additions & 0 deletions events/README_Cognito_UserPools_PreSignup.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Sample Function

The following is a sample Lambda function that receives Amazon Cognito User Pools pre-signup event as an input and writes some of the record data to CloudWatch Logs. (Note that by default anything written to Console will be logged as CloudWatch Logs events.)

Please see instructions for setting up the Cognito triggers at https://docs.aws.amazon.com/cognito/latest/developerguide/cognito-user-identity-pools-working-with-aws-lambda-triggers.html .

```go
package main

import (
"fmt"

"github.com/aws/aws-lambda-go/lambda"
"github.com/aws/aws-lambda-go/events"
)

func handler(event events.CognitoEventUserPoolsPreSignup) (events.CognitoEventUserPoolsPreSignup, error) {
fmt.Printf("PreSignup of user: %s\n", event.UserName)
event.Response.AutoConfirmUser = true
return event, nil
}

func main() {
lambda.Start(handler)
}
```
35 changes: 35 additions & 0 deletions events/README_Connect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Sample Function

The following is a sample Lambda function that receives an Amazon Connect event as an input and writes some of the record data to CloudWatch Logs. (Note that by default anything written to Console will be logged as CloudWatch Logs events.)

```go
package main

import (
"context"
"fmt"

"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
)

func main() {
lambda.Start(handler)
}

func handler(ctx context.Context, connectEvent events.ConnectEvent) (events.ConnectResponse, error) {
fmt.Printf("Processing Connect event with ContactID %s.\n", connectEvent.Details.ContactData.ContactID)

fmt.Printf("Invoked with %d parameters\n", len(connectEvent.Details.Parameters))
for k, v := range connectEvent.Details.Parameters {
fmt.Printf("%s : %s\n", k, v)
}

resp := events.ConnectResponse{
"Result": "Success",
"NewAttribute": "NewValue",
}

return resp, nil
}
```
29 changes: 29 additions & 0 deletions events/README_SQS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

# Sample Function

The following is a sample class and Lambda function that receives Amazon SQS event message data as input, writes some of the message data to CloudWatch Logs, and responds with a 200 status and the same body as the request. (Note that by default anything written to Console will be logged as CloudWatch Logs events.)

```go
package main

import (
"context"
"fmt"

"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
)

func handler(ctx context.Context, sqsEvent events.SQSEvent) error {
for _, message := range sqsEvent.Records {
fmt.Printf("The message %s for event source %s = %s \n", message.MessageId, message.EventSource, message.Body)
}

return nil
}

func main() {
lambda.Start(handler)
}

```
20 changes: 20 additions & 0 deletions events/appsync.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package events

import "encoding/json"

// AppSyncResolverTemplate represents the requests from AppSync to Lambda
type AppSyncResolverTemplate struct {
Version string `json:"version"`
Operation AppSyncOperation `json:"operation"`
Payload json.RawMessage `json:"payload"`
}

// AppSyncOperation specifies the operation type supported by Lambda operations
type AppSyncOperation string

const (
// OperationInvoke lets AWS AppSync know to call your Lambda function for every GraphQL field resolver
OperationInvoke AppSyncOperation = "Invoke"
// OperationBatchInvoke instructs AWS AppSync to batch requests for the current GraphQL field
OperationBatchInvoke AppSyncOperation = "BatchInvoke"
)
50 changes: 50 additions & 0 deletions events/appsync_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package events

import (
"encoding/json"
"io/ioutil"
"testing"

"github.com/aws/aws-lambda-go/events/test"
"github.com/stretchr/testify/assert"
)

func TestAppSyncResolverTemplate_invoke(t *testing.T) {
inputJSON, err := ioutil.ReadFile("./testdata/appsync-invoke.json")
if err != nil {
t.Errorf("could not open test file. details: %v", err)
}

var inputEvent AppSyncResolverTemplate
if err = json.Unmarshal(inputJSON, &inputEvent); err != nil {
t.Errorf("could not unmarshal event. details: %v", err)
}
assert.Equal(t, OperationInvoke, inputEvent.Operation)

outputJSON, err := json.Marshal(inputEvent)
if err != nil {
t.Errorf("could not marshal event. details: %v", err)
}

test.AssertJsonsEqual(t, inputJSON, outputJSON)
}

func TestAppSyncResolverTemplate_batchinvoke(t *testing.T) {
inputJSON, err := ioutil.ReadFile("./testdata/appsync-batchinvoke.json")
if err != nil {
t.Errorf("could not open test file. details: %v", err)
}

var inputEvent AppSyncResolverTemplate
if err = json.Unmarshal(inputJSON, &inputEvent); err != nil {
t.Errorf("could not unmarshal event. details: %v", err)
}
assert.Equal(t, OperationBatchInvoke, inputEvent.Operation)

outputJSON, err := json.Marshal(inputEvent)
if err != nil {
t.Errorf("could not marshal event. details: %v", err)
}

test.AssertJsonsEqual(t, inputJSON, outputJSON)
}

0 comments on commit 93d59f2

Please sign in to comment.