-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
61 lines (52 loc) · 1.49 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
package main
import (
"encoding/json"
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/lambda"
)
type event struct {
URL string `json:"url"`
Data string `json:"data"`
}
func newClient() lambda.Lambda {
var s *session.Session
//Used NewSharedCredentials, which will take the required AWS keyID and secret key from ~/.aws/credentials file
s = session.Must(session.NewSession(&aws.Config{
Region: aws.String("us-west-2"),
Credentials: credentials.NewSharedCredentials("", ""),
}))
var lambdaClient *lambda.Lambda = lambda.New(s)
return *lambdaClient
}
func send(evt event, client interface{}) int {
data, err := json.Marshal(evt)
if err != nil {
fmt.Println("Error while marshalling:", err)
return 500
}
fmt.Println(string(data))
lambdaClient, _ := client.(lambda.Lambda)
res, err := lambdaClient.Invoke(&lambda.InvokeInput{
FunctionName: aws.String("testFunction"),
Payload: data,
})
if err != nil {
fmt.Println("Error calling testFunction:", err)
return 501
}
fmt.Println("res.Payload:", string(res.Payload))
var statusCode int
err = json.Unmarshal(res.Payload, &statusCode)
return statusCode
}
func main() {
evt := event{"https://webhook.site/59ca3a13-3cca-4012-a716-94d1ac717be6", "Hi, this is lambda poc!"}
//data, _ := json.Marshal(evt)
//fmt.Println(string(data))
client := newClient()
res := send(evt, client)
fmt.Println("response:", res)
}