-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
149 lines (131 loc) · 3.33 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package main
import (
"context"
"encoding/json"
"fmt"
fake "github.com/NextOption/aws/lambda"
"github.com/aws/aws-sdk-go/aws"
real "github.com/aws/aws-sdk-go/service/lambda"
)
type Order struct {
ID string `json:"id"`
}
type OrderProcessResult struct {
OrderID string `json:"order_id"`
Success bool `json:"success"`
}
// handleFn1 is handler for lambda_fn_1
func handleFn1(ctx context.Context, input []byte) ([]byte, error) {
// because you do not need input and output, so we just ignore it
coreFn1()
return []byte{}, nil
}
func coreFn1() {
fmt.Println("I'm inside of coreFn1")
}
// handleFn2 is handler for lambda_fn_2
func handleFn2(ctx context.Context, input []byte) ([]byte, error) {
order := Order{}
err := json.Unmarshal(input, &order)
if err != nil {
fmt.Println("error when unmarshal input: ", err)
return []byte{}, err
}
success, err := coreFn2(&order)
result := OrderProcessResult{
OrderID: order.ID,
Success: success,
}
out, err := json.Marshal(&result)
if err != nil {
fmt.Println("error when marshal output: ", err)
return []byte{}, err
}
return out, nil
}
func coreFn2(order *Order) (success bool, err error) {
fmt.Println("I'm inside of coreFn2, receive: ", order)
return true, nil
}
func main() {
var (
lambdaFn1Name = "lambda_fn_1"
fn2Name = "lambda_fn_2"
)
enableFakeLambda := true
if enableFakeLambda {
InitFakeLambda(
fake.WithFunction(lambdaFn1Name, handleFn1),
fake.WithFunction(fn2Name, handleFn2),
)
} else {
InitAWSLambda()
}
// invoke lambda_fn_1
// lambda_fn_1 return nothing so we just skip it
_, err := GetServerless().Invoke(&real.InvokeInput{
FunctionName: &lambdaFn1Name,
})
if err != nil {
fmt.Printf("error when invoke lambda 1: %v", err)
return
}
order := Order{
ID: "uuid",
}
inputPayload, err := json.Marshal(&order)
if err != nil {
fmt.Println("error when marshal input: ", err)
return
}
invokeOutput, err := GetServerless().Invoke(&real.InvokeInput{
FunctionName: &fn2Name,
Payload: inputPayload,
})
if err != nil {
fmt.Printf("error when invoke lambda 2: %v", err)
return
}
if invokeOutput != nil && invokeOutput.Payload != nil {
orderProcessResult := OrderProcessResult{}
err = json.Unmarshal(invokeOutput.Payload, &orderProcessResult)
if err != nil {
fmt.Println("error when unmarshal output: ", err)
return
}
fmt.Println("result: ", orderProcessResult)
}
invokeOutput, err = GetServerless().InvokeWithContext(context.TODO(), &real.InvokeInput{
FunctionName: &fn2Name,
Payload: inputPayload,
})
if err != nil {
fmt.Printf("error when invoke lambda 2: %v", err)
return
}
if invokeOutput != nil && invokeOutput.Payload != nil {
orderProcessResult := OrderProcessResult{}
err = json.Unmarshal(invokeOutput.Payload, &orderProcessResult)
if err != nil {
fmt.Println("error when unmarshal output: ", err)
return
}
fmt.Println("result: ", orderProcessResult)
}
invokeOutput, err = GetServerless().InvokeWithContext(context.TODO(), &real.InvokeInput{
InvocationType: aws.String(real.InvocationTypeEvent),
FunctionName: &fn2Name,
Payload: inputPayload,
})
if err != nil {
fmt.Printf("error when invoke lambda 2: %v", err)
return
}
}
// real main for lambda will be like following
//func main() {
// lambda.Start(handleFn1)
//}
//func main() {
// lambda.Start(handleFn2)
//}