Skip to content

Commit 93d59f2

Browse files
authored
Merge branch 'master' into master
2 parents a5e9c6a + df9f95d commit 93d59f2

37 files changed

+940
-68
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ before_install:
1010

1111
install:
1212
- dep ensure
13-
- go get github.com/golang/lint/golint
13+
- go get golang.org/x/lint/golint
1414
- go get github.com/haya14busa/goverage
1515

1616
matrix:

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ in cmd.exe:
6464
``` bat
6565
set GOOS=linux
6666
set GOARCH=amd64
67+
set CGO_ENABLED=0
6768
go build -o main main.go
6869
%USERPROFILE%\Go\bin\build-lambda-zip.exe -o main.zip main
6970
```
@@ -72,6 +73,7 @@ in Powershell:
7273
``` posh
7374
$env:GOOS = "linux"
7475
$env:GOARCH = "amd64"
76+
$env:CGO_ENABLED = "0"
7577
go build -o main main.go
7678
~\Go\Bin\build-lambda-zip.exe -o main.zip main
7779
```

cfn/wrap.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,16 @@ func lambdaWrapWithClient(lambdaFunction CustomResourceFunction, client httpClie
2222
fn = func(ctx context.Context, event Event) (reason string, err error) {
2323
r := NewResponse(&event)
2424
r.PhysicalResourceID, r.Data, err = lambdaFunction(ctx, event)
25-
25+
if r.PhysicalResourceID == "" {
26+
log.Println("PhysicalResourceID must exist, copying Log Stream name")
27+
r.PhysicalResourceID = lambdacontext.LogStreamName
28+
}
2629
if err != nil {
2730
r.Status = StatusFailed
2831
r.Reason = err.Error()
2932
log.Printf("sending status failed: %s", r.Reason)
3033
} else {
3134
r.Status = StatusSuccess
32-
33-
if event.RequestType == RequestCreate && r.PhysicalResourceID == "" {
34-
log.Println("PhysicalResourceID must exist on creation, copying Log Stream name")
35-
r.PhysicalResourceID = lambdacontext.LogStreamName
36-
}
3735
}
3836

3937
err = r.sendWith(client)

cmd/build-lambda-zip/main.go

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ import (
1414
func main() {
1515
app := cli.NewApp()
1616
app.Name = "build-lambda-zip"
17-
app.Usage = "Put an executable into a zip file that works with AWS Lambda."
17+
app.Usage = "Put an executable and supplemental files into a zip file that works with AWS Lambda."
1818
app.Flags = []cli.Flag{
1919
cli.StringFlag{
2020
Name: "output, o",
2121
Value: "",
22-
Usage: "output file path for the zip. Defaults to the input file name.",
22+
Usage: "output file path for the zip. Defaults to the first input file name.",
2323
},
2424
}
2525

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

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

64-
func compressExe(outZipPath, exePath string) error {
64+
func compressExeAndArgs(outZipPath string, exePath string, args []string) error {
6565
zipFile, err := os.Create(outZipPath)
6666
if err != nil {
6767
return err
6868
}
69-
defer zipFile.Close()
69+
defer func() {
70+
closeErr := zipFile.Close()
71+
if closeErr != nil {
72+
fmt.Fprintf(os.Stderr, "Failed to close zip file: %v\n", closeErr)
73+
}
74+
return
75+
}()
7076

7177
zipWriter := zip.NewWriter(zipFile)
7278
defer zipWriter.Close()
73-
7479
data, err := ioutil.ReadFile(exePath)
7580
if err != nil {
7681
return err
7782
}
7883

79-
return writeExe(zipWriter, filepath.Base(exePath), data)
84+
err = writeExe(zipWriter, filepath.Base(exePath), data)
85+
if err != nil {
86+
return err
87+
}
88+
89+
for _, arg := range args {
90+
writer, err := zipWriter.Create(arg)
91+
if err != nil {
92+
return err
93+
}
94+
data, err := ioutil.ReadFile(arg)
95+
if err != nil {
96+
return err
97+
}
98+
_, err = writer.Write(data)
99+
if err != nil {
100+
return err
101+
}
102+
}
103+
return err
80104
}

events/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,5 @@ This package provides input types for Lambda functions that process AWS events.
2727
[S3 Events](README_S3.md)
2828

2929
[SNS Events](README_SNS.md)
30+
31+
[SQS Events](README_SQS.md)

events/README_Cognito.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
# Sample Function
22

3-
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.)
3+
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.)
44

55
```go
66

7+
package main
8+
79
import (
8-
"strings"
10+
"fmt"
11+
12+
"github.com/aws/aws-lambda-go/lambda"
913
"github.com/aws/aws-lambda-go/events"
1014
)
1115

12-
func handleRequest(ctx context.Context, cognitoEvent events.CognitoEvent) {
16+
func handler(cognitoEvent events.CognitoEvent) error {
1317
for datasetName, datasetRecord := range cognitoEvent.DatasetRecords {
1418
fmt.Printf("[%s -- %s] %s -> %s -> %s \n",
1519
cognitoEvent.EventType,
@@ -18,5 +22,11 @@ func handleRequest(ctx context.Context, cognitoEvent events.CognitoEvent) {
1822
datasetRecord.Op,
1923
datasetRecord.NewValue)
2024
}
25+
return nil
2126
}
27+
28+
func main() {
29+
lambda.Start(handler)
30+
}
31+
2232
```
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Sample Function
2+
3+
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.)
4+
5+
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 .
6+
7+
```go
8+
package main
9+
10+
import (
11+
"fmt"
12+
13+
"github.com/aws/aws-lambda-go/lambda"
14+
"github.com/aws/aws-lambda-go/events"
15+
)
16+
17+
func handler(event events.CognitoEventUserPoolsPostConfirmation) (events.CognitoEventUserPoolsPostConfirmation, error) {
18+
fmt.Printf("PostConfirmation for user: %s\n", event.UserName)
19+
return event, nil
20+
}
21+
22+
func main() {
23+
lambda.Start(handler)
24+
}
25+
```
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Sample Function
2+
3+
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.)
4+
5+
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 .
6+
7+
```go
8+
package main
9+
10+
import (
11+
"fmt"
12+
13+
"github.com/aws/aws-lambda-go/lambda"
14+
"github.com/aws/aws-lambda-go/events"
15+
)
16+
17+
func handler(event events.CognitoEventUserPoolsPreSignup) (events.CognitoEventUserPoolsPreSignup, error) {
18+
fmt.Printf("PreSignup of user: %s\n", event.UserName)
19+
event.Response.AutoConfirmUser = true
20+
return event, nil
21+
}
22+
23+
func main() {
24+
lambda.Start(handler)
25+
}
26+
```

events/README_Connect.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Sample Function
2+
3+
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.)
4+
5+
```go
6+
package main
7+
8+
import (
9+
"context"
10+
"fmt"
11+
12+
"github.com/aws/aws-lambda-go/events"
13+
"github.com/aws/aws-lambda-go/lambda"
14+
)
15+
16+
func main() {
17+
lambda.Start(handler)
18+
}
19+
20+
func handler(ctx context.Context, connectEvent events.ConnectEvent) (events.ConnectResponse, error) {
21+
fmt.Printf("Processing Connect event with ContactID %s.\n", connectEvent.Details.ContactData.ContactID)
22+
23+
fmt.Printf("Invoked with %d parameters\n", len(connectEvent.Details.Parameters))
24+
for k, v := range connectEvent.Details.Parameters {
25+
fmt.Printf("%s : %s\n", k, v)
26+
}
27+
28+
resp := events.ConnectResponse{
29+
"Result": "Success",
30+
"NewAttribute": "NewValue",
31+
}
32+
33+
return resp, nil
34+
}
35+
```

events/README_SQS.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
# Sample Function
3+
4+
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.)
5+
6+
```go
7+
package main
8+
9+
import (
10+
"context"
11+
"fmt"
12+
13+
"github.com/aws/aws-lambda-go/events"
14+
"github.com/aws/aws-lambda-go/lambda"
15+
)
16+
17+
func handler(ctx context.Context, sqsEvent events.SQSEvent) error {
18+
for _, message := range sqsEvent.Records {
19+
fmt.Printf("The message %s for event source %s = %s \n", message.MessageId, message.EventSource, message.Body)
20+
}
21+
22+
return nil
23+
}
24+
25+
func main() {
26+
lambda.Start(handler)
27+
}
28+
29+
```

events/appsync.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package events
2+
3+
import "encoding/json"
4+
5+
// AppSyncResolverTemplate represents the requests from AppSync to Lambda
6+
type AppSyncResolverTemplate struct {
7+
Version string `json:"version"`
8+
Operation AppSyncOperation `json:"operation"`
9+
Payload json.RawMessage `json:"payload"`
10+
}
11+
12+
// AppSyncOperation specifies the operation type supported by Lambda operations
13+
type AppSyncOperation string
14+
15+
const (
16+
// OperationInvoke lets AWS AppSync know to call your Lambda function for every GraphQL field resolver
17+
OperationInvoke AppSyncOperation = "Invoke"
18+
// OperationBatchInvoke instructs AWS AppSync to batch requests for the current GraphQL field
19+
OperationBatchInvoke AppSyncOperation = "BatchInvoke"
20+
)

events/appsync_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package events
2+
3+
import (
4+
"encoding/json"
5+
"io/ioutil"
6+
"testing"
7+
8+
"github.com/aws/aws-lambda-go/events/test"
9+
"github.com/stretchr/testify/assert"
10+
)
11+
12+
func TestAppSyncResolverTemplate_invoke(t *testing.T) {
13+
inputJSON, err := ioutil.ReadFile("./testdata/appsync-invoke.json")
14+
if err != nil {
15+
t.Errorf("could not open test file. details: %v", err)
16+
}
17+
18+
var inputEvent AppSyncResolverTemplate
19+
if err = json.Unmarshal(inputJSON, &inputEvent); err != nil {
20+
t.Errorf("could not unmarshal event. details: %v", err)
21+
}
22+
assert.Equal(t, OperationInvoke, inputEvent.Operation)
23+
24+
outputJSON, err := json.Marshal(inputEvent)
25+
if err != nil {
26+
t.Errorf("could not marshal event. details: %v", err)
27+
}
28+
29+
test.AssertJsonsEqual(t, inputJSON, outputJSON)
30+
}
31+
32+
func TestAppSyncResolverTemplate_batchinvoke(t *testing.T) {
33+
inputJSON, err := ioutil.ReadFile("./testdata/appsync-batchinvoke.json")
34+
if err != nil {
35+
t.Errorf("could not open test file. details: %v", err)
36+
}
37+
38+
var inputEvent AppSyncResolverTemplate
39+
if err = json.Unmarshal(inputJSON, &inputEvent); err != nil {
40+
t.Errorf("could not unmarshal event. details: %v", err)
41+
}
42+
assert.Equal(t, OperationBatchInvoke, inputEvent.Operation)
43+
44+
outputJSON, err := json.Marshal(inputEvent)
45+
if err != nil {
46+
t.Errorf("could not marshal event. details: %v", err)
47+
}
48+
49+
test.AssertJsonsEqual(t, inputJSON, outputJSON)
50+
}

0 commit comments

Comments
 (0)