-
Notifications
You must be signed in to change notification settings - Fork 5
/
http-lambda-invoker.go
154 lines (127 loc) · 3.78 KB
/
http-lambda-invoker.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
150
151
152
153
154
package main
import (
"io/ioutil"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/endpoints"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/lambda"
"github.com/aws/aws-sdk-go/service/lambda/lambdaiface"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"strings"
)
// LambdaClient enables mocking of the client for test purposes
type LambdaClient struct {
lambdaiface.LambdaAPI
}
type proxyHeader map[string]string
// Parts of the request to send to Lambda.
type makeProxyRequest struct {
Body string `json:"body"`
Headers proxyHeader `json:"headers"`
HTTPMethod string `json:"httpMethod"`
Path string `json:"path"`
QueryStringParams map[string][]string `json:"queryStringParameters"`
}
// Parts of the response to send back to the caller.
type restResponse struct {
Body string
Headers map[string]string
StatusCode int
}
// Set some defaults for envvars.
// Access key and secret should normally be ignored as we're calling a local function.
func getConfig(key string) string {
c := os.Getenv(key)
if c != "" {
return c
}
switch key {
case "AWS_ACCESS_KEY_ID":
return "foo"
case "AWS_SECRET_ACCESS_KEY":
return "bar"
case "AWS_REGION":
return endpoints.UsEast1RegionID
case "PORT":
return "8080"
default:
return ""
}
}
func makeProxyHeaders(originalHeaders map[string][]string) proxyHeader {
var newHeaders = make(proxyHeader)
for header := range originalHeaders {
newHeaders[header] = strings.Join(originalHeaders[header], "")
}
return newHeaders
}
func handleError(w http.ResponseWriter, err error) {
http.Error(w, fmt.Sprintf("Error: %v", err), http.StatusBadRequest)
}
func handler(w http.ResponseWriter, r *http.Request) {
// Create AWS session.
sess := session.Must(session.NewSession(&aws.Config{
Credentials: credentials.NewStaticCredentials(getConfig("AWS_ACCESS_KEY_ID"), getConfig("AWS_SECRET_ACCESS_KEY"), getConfig("AWS_SESSION_TOKEN")),
Region: aws.String(getConfig("AWS_REGION")),
Endpoint: aws.String(getConfig("LAMBDA_ENDPOINT")),
}))
// Initialize lambda client.
c := LambdaClient{
lambda.New(sess, &aws.Config{}),
}
c.invokeLambda(w, r)
}
func (c *LambdaClient) invokeLambda(w http.ResponseWriter, r *http.Request) {
// Error handling seems really verbose. Is there a better way?
// Read request body.
body, err := ioutil.ReadAll(r.Body)
if err != nil {
handleError(w, err)
return
}
// Convert headers to appropriate ApiGateway format
proxyHeaders := makeProxyHeaders(r.Header)
// Get struct.
request := makeProxyRequest{string(body), proxyHeaders, r.Method, r.URL.Path, r.URL.Query()}
// Marshal request.
payload, err := json.Marshal(request)
if err != nil {
handleError(w, err)
return
}
// Invoke Lambda.
result, err := c.Invoke(&lambda.InvokeInput{FunctionName: aws.String(getConfig("LAMBDA_NAME")), Payload: payload})
if err != nil {
handleError(w, err)
return
}
var response restResponse
// Unmarshal response into `response`.
err = json.Unmarshal(result.Payload, &response)
if err != nil {
handleError(w, err)
return
}
// Add headers to ResponseWriter omitting content-length, which came back with the wrong length.
for key, value := range response.Headers {
if key != "content-length" {
w.Header().Add(key, value)
}
}
// Enable cors
w.Header().Set("Access-Control-Allow-Origin", "*")
// Write status code and body.
w.WriteHeader(response.StatusCode)
fmt.Fprint(w, string(response.Body))
}
// Start simple web server with configured port, sending all traffic to handler.
func main() {
var Port = getConfig("PORT")
http.HandleFunc("/", handler)
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%v", Port), nil))
}