forked from aws/aws-lambda-runtime-interface-emulator
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcustom_interop.go
More file actions
278 lines (241 loc) · 8.93 KB
/
custom_interop.go
File metadata and controls
278 lines (241 loc) · 8.93 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package main
// Original implementation: lambda/rapidcore/server.go includes Server struct with state
// Server interface between Runtime API and this init: lambda/interop/model.go:Server
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"strconv"
"strings"
"time"
"github.com/aws/aws-lambda-runtime-interface-emulator/internal/lambda/core/statejson"
"github.com/aws/aws-lambda-runtime-interface-emulator/internal/lambda/interop"
"github.com/aws/aws-lambda-runtime-interface-emulator/internal/lambda/rapidcore"
"github.com/aws/aws-lambda-runtime-interface-emulator/internal/lambda/rapidcore/standalone"
"github.com/go-chi/chi/v5"
log "github.com/sirupsen/logrus"
)
type CustomInteropServer struct {
delegate *rapidcore.Server
localStackAdapter *LocalStackAdapter
port string
upstreamEndpoint string
}
type LocalStackAdapter struct {
UpstreamEndpoint string
RuntimeId string
}
type LocalStackStatus string
const (
Ready LocalStackStatus = "ready"
Error LocalStackStatus = "error"
)
func (l *LocalStackAdapter) SendStatus(status LocalStackStatus, payload []byte) error {
statusUrl := fmt.Sprintf("%s/status/%s/%s", l.UpstreamEndpoint, l.RuntimeId, status)
_, err := http.Post(statusUrl, "application/json", bytes.NewReader(payload))
if err != nil {
return err
}
return nil
}
// The InvokeRequest is sent by LocalStack to trigger an invocation
type InvokeRequest struct {
InvokeId string `json:"invoke-id"`
InvokedFunctionArn string `json:"invoked-function-arn"`
Payload string `json:"payload"`
TraceId string `json:"trace-id"`
}
// The ErrorResponse is sent TO LocalStack when encountering an error
type ErrorResponse struct {
ErrorMessage string `json:"errorMessage"`
ErrorType string `json:"errorType,omitempty"`
RequestId string `json:"requestId,omitempty"`
StackTrace []string `json:"stackTrace,omitempty"`
}
func NewCustomInteropServer(lsOpts *LsOpts, delegate interop.Server, logCollector *LogCollector) (server *CustomInteropServer) {
server = &CustomInteropServer{
delegate: delegate.(*rapidcore.Server),
port: lsOpts.InteropPort,
upstreamEndpoint: lsOpts.RuntimeEndpoint,
localStackAdapter: &LocalStackAdapter{
UpstreamEndpoint: lsOpts.RuntimeEndpoint,
RuntimeId: lsOpts.RuntimeId,
},
}
// TODO: extract this
go func() {
r := chi.NewRouter()
r.Post("/invoke", func(w http.ResponseWriter, r *http.Request) {
invokeR := InvokeRequest{}
bytess, err := io.ReadAll(r.Body)
if err != nil {
log.Error(err)
}
go func() {
err = json.Unmarshal(bytess, &invokeR)
if err != nil {
log.Error(err)
}
invokeResp := &standalone.ResponseWriterProxy{}
functionVersion := GetEnvOrDie("AWS_LAMBDA_FUNCTION_VERSION") // default $LATEST
_, _ = fmt.Fprintf(logCollector, "START RequestId: %s Version: %s\n", invokeR.InvokeId, functionVersion)
invokeStart := time.Now()
err = server.Invoke(invokeResp, &interop.Invoke{
ID: invokeR.InvokeId,
InvokedFunctionArn: invokeR.InvokedFunctionArn,
Payload: strings.NewReader(invokeR.Payload), // r.Body,
NeedDebugLogs: true,
TraceID: invokeR.TraceId,
// TODO: set correct segment ID from request
//LambdaSegmentID: "LambdaSegmentID", // r.Header.Get("X-Amzn-Segment-Id"),
//CognitoIdentityID: "",
//CognitoIdentityPoolID: "",
//DeadlineNs: "",
//ClientContext: "",
//ContentType: "",
//ReservationToken: "",
//VersionID: "",
//InvokeReceivedTime: 0,
//ResyncState: interop.Resync{},
})
timeout := int(server.delegate.GetInvokeTimeout().Seconds())
isErr := false
if err != nil {
switch {
case errors.Is(err, rapidcore.ErrInvokeTimeout):
log.Debugf("Got invoke timeout")
isErr = true
errorResponse := ErrorResponse{
ErrorMessage: fmt.Sprintf(
"%s %s Task timed out after %d.00 seconds",
time.Now().Format("2006-01-02T15:04:05Z"),
invokeR.InvokeId,
timeout,
),
}
jsonErrorResponse, err := json.Marshal(errorResponse)
if err != nil {
log.Fatalln("unable to marshall json timeout response")
}
_, err = invokeResp.Write(jsonErrorResponse)
if err != nil {
log.Fatalln("unable to write to response")
}
case errors.Is(err, rapidcore.ErrInvokeDoneFailed):
// we can actually just continue here, error message is sent below
default:
log.Fatalln(err)
}
}
// optional sleep. can be used for debugging purposes
if lsOpts.PostInvokeWaitMS != "" {
waitMS, err := strconv.Atoi(lsOpts.PostInvokeWaitMS)
if err != nil {
log.Fatalln(err)
}
time.Sleep(time.Duration(waitMS) * time.Millisecond)
}
timeoutDuration := time.Duration(timeout) * time.Second
memorySize := GetEnvOrDie("AWS_LAMBDA_FUNCTION_MEMORY_SIZE")
PrintEndReports(invokeR.InvokeId, "", memorySize, invokeStart, timeoutDuration, logCollector)
serializedLogs, err2 := json.Marshal(logCollector.getLogs())
if err2 == nil {
_, err2 = http.Post(server.upstreamEndpoint+"/invocations/"+invokeR.InvokeId+"/logs", "application/json", bytes.NewReader(serializedLogs))
// TODO: handle err
}
var errR map[string]any
marshalErr := json.Unmarshal(invokeResp.Body, &errR)
if !isErr && marshalErr == nil {
_, isErr = errR["errorType"]
}
if isErr {
log.Infoln("Sending to /error")
_, err = http.Post(server.upstreamEndpoint+"/invocations/"+invokeR.InvokeId+"/error", "application/json", bytes.NewReader(invokeResp.Body))
if err != nil {
log.Error(err)
}
} else {
log.Infoln("Sending to /response")
_, err = http.Post(server.upstreamEndpoint+"/invocations/"+invokeR.InvokeId+"/response", "application/json", bytes.NewReader(invokeResp.Body))
if err != nil {
log.Error(err)
}
}
}()
w.WriteHeader(200)
_, _ = w.Write([]byte("OK"))
})
err := http.ListenAndServe(":"+server.port, r)
if err != nil {
log.Error(err)
}
}()
return server
}
func (c *CustomInteropServer) SendResponse(invokeID string, resp *interop.StreamableInvokeResponse) error {
log.Traceln("SendResponse called")
return c.delegate.SendResponse(invokeID, resp)
}
func (c *CustomInteropServer) SendErrorResponse(invokeID string, resp *interop.ErrorInvokeResponse) error {
log.Traceln("SendErrorResponse called")
return c.delegate.SendErrorResponse(invokeID, resp)
}
// SendInitErrorResponse writes error response during init to a shared memory and sends GIRD FAULT.
func (c *CustomInteropServer) SendInitErrorResponse(resp *interop.ErrorInvokeResponse) error {
log.Traceln("SendInitErrorResponse called")
if err := c.localStackAdapter.SendStatus(Error, resp.Payload); err != nil {
log.Fatalln("Failed to send init error to LocalStack " + err.Error() + ". Exiting.")
}
return c.delegate.SendInitErrorResponse(resp)
}
func (c *CustomInteropServer) GetCurrentInvokeID() string {
log.Traceln("GetCurrentInvokeID called")
return c.delegate.GetCurrentInvokeID()
}
func (c *CustomInteropServer) SendRuntimeReady() error {
log.Traceln("SendRuntimeReady called")
return c.delegate.SendRuntimeReady()
}
func (c *CustomInteropServer) Init(i *interop.Init, invokeTimeoutMs int64) error {
log.Traceln("Init called")
return c.delegate.Init(i, invokeTimeoutMs)
}
func (c *CustomInteropServer) Invoke(responseWriter http.ResponseWriter, invoke *interop.Invoke) error {
log.Traceln("Invoke called")
return c.delegate.Invoke(responseWriter, invoke)
}
func (c *CustomInteropServer) FastInvoke(w http.ResponseWriter, i *interop.Invoke, direct bool) error {
log.Traceln("FastInvoke called")
return c.delegate.FastInvoke(w, i, direct)
}
func (c *CustomInteropServer) Reserve(id string, traceID, lambdaSegmentID string) (*rapidcore.ReserveResponse, error) {
log.Traceln("Reserve called")
return c.delegate.Reserve(id, traceID, lambdaSegmentID)
}
func (c *CustomInteropServer) Reset(reason string, timeoutMs int64) (*statejson.ResetDescription, error) {
log.Traceln("Reset called")
return c.delegate.Reset(reason, timeoutMs)
}
func (c *CustomInteropServer) AwaitRelease() (*statejson.ReleaseResponse, error) {
log.Traceln("AwaitRelease called")
return c.delegate.AwaitRelease()
}
func (c *CustomInteropServer) InternalState() (*statejson.InternalStateDescription, error) {
log.Traceln("InternalState called")
return c.delegate.InternalState()
}
func (c *CustomInteropServer) CurrentToken() *interop.Token {
log.Traceln("CurrentToken called")
return c.delegate.CurrentToken()
}
func (c *CustomInteropServer) SetSandboxContext(sbCtx interop.SandboxContext) {
log.Traceln("SetSandboxContext called")
c.delegate.SetSandboxContext(sbCtx)
}
func (c *CustomInteropServer) SetInternalStateGetter(cb interop.InternalStateGetter) {
log.Traceln("SetInternalStateGetter called")
c.delegate.InternalStateGetter = cb
}