/
aws-go-sdk-lambda-example-run-function.go
111 lines (89 loc) · 2.77 KB
/
aws-go-sdk-lambda-example-run-function.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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
/*
Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
This file is licensed under the Apache License, Version 2.0 (the "License").
You may not use this file except in compliance with the License. A copy of the
License is located at
http://aws.amazon.com/apache2.0/
This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
OF ANY KIND, either express or implied. See the License for the specific
language governing permissions and limitations under the License.
*/
package main
import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/lambda"
"encoding/json"
"fmt"
"os"
"strconv"
)
type getItemsRequest struct {
SortBy string
SortOrder string
ItemsToGet int
}
type getItemsResponseError struct {
Message string `json:"message"`
}
type getItemsResponseData struct {
Item string `json:"item"`
}
type getItemsResponseBody struct {
Result string `json:"result"`
Data []getItemsResponseData `json:"data"`
Error getItemsResponseError `json:"error"`
}
type getItemsResponseHeaders struct {
ContentType string `json:"Content-Type"`
}
type getItemsResponse struct {
StatusCode int `json:"statusCode"`
Headers getItemsResponseHeaders `json:"headers"`
Body getItemsResponseBody `json:"body"`
}
func main() {
// Create Lambda service client
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
client := lambda.New(sess, &aws.Config{Region: aws.String("us-west-2")})
// Get the 10 most recent items
request := getItemsRequest{"time", "descending", 10}
payload, err := json.Marshal(request)
if err != nil {
fmt.Println("Error marshalling MyGetItemsFunction request")
os.Exit(0)
}
result, err := client.Invoke(&lambda.InvokeInput{FunctionName: aws.String("MyGetItemsFunction"), Payload: payload})
if err != nil {
fmt.Println("Error calling MyGetItemsFunction")
os.Exit(0)
}
var resp getItemsResponse
err = json.Unmarshal(result.Payload, &resp)
if err != nil {
fmt.Println("Error unmarshalling MyGetItemsFunction response")
os.Exit(0)
}
// If the status code is NOT 200, the call failed
if resp.StatusCode != 200 {
fmt.Println("Error getting items, StatusCode: " + strconv.Itoa(resp.StatusCode))
os.Exit(0)
}
// If the result is failure, we got an error
if resp.Body.Result == "failure" {
fmt.Println("Failed to get items")
os.Exit(0)
}
// Print out items
if len(resp.Body.Data) > 0 {
for i := range resp.Body.Data {
fmt.Println(resp.Body.Data[i].Item)
}
} else {
fmt.Println("There were no items")
}
}