9
9
"strconv"
10
10
"strings"
11
11
12
+ "github.com/aws/aws-sdk-go-v2/config"
12
13
"github.com/localstack/lambda-runtime-init/internal/aws/xray"
13
14
"github.com/localstack/lambda-runtime-init/internal/bootstrap"
14
15
"github.com/localstack/lambda-runtime-init/internal/hotreloading"
@@ -24,8 +25,8 @@ import (
24
25
func InitLsOpts () * server.LsOpts {
25
26
return & server.LsOpts {
26
27
// required
27
- RuntimeEndpoint : utils .GetEnvOrDie ("LOCALSTACK_RUNTIME_ENDPOINT" ),
28
- RuntimeId : utils .GetEnvOrDie ("LOCALSTACK_RUNTIME_ID" ),
28
+ RuntimeEndpoint : utils .MustGetEnv ("LOCALSTACK_RUNTIME_ENDPOINT" ),
29
+ RuntimeId : utils .MustGetEnv ("LOCALSTACK_RUNTIME_ID" ),
29
30
AccountId : utils .GetEnvWithDefault ("LOCALSTACK_FUNCTION_ACCOUNT_ID" , "000000000000" ),
30
31
// optional with default
31
32
InteropPort : utils .GetEnvWithDefault ("LOCALSTACK_INTEROP_PORT" , "9563" ),
@@ -45,6 +46,19 @@ func InitLsOpts() *server.LsOpts {
45
46
}
46
47
}
47
48
49
+ func InitFunctionConfig () server.FunctionConfig {
50
+ return server.FunctionConfig {
51
+ FunctionName : utils .GetEnvWithDefault ("AWS_LAMBDA_FUNCTION_NAME" , "test_function" ),
52
+ FunctionVersion : utils .GetEnvWithDefault ("AWS_LAMBDA_FUNCTION_VERSION" , "$LATEST" ),
53
+ FunctionTimeoutSec : utils .GetEnvWithDefault ("AWS_LAMBDA_FUNCTION_TIMEOUT" , "30" ),
54
+ InitializationType : utils .GetEnvWithDefault ("AWS_LAMBDA_INITIALIZATION_TYPE" , "on-demand" ),
55
+ LogGroupName : utils .GetEnvWithDefault ("AWS_LAMBDA_LOG_GROUP_NAME" , "/aws/lambda/Functions" ),
56
+ LogStreamName : utils .GetEnvWithDefault ("AWS_LAMBDA_LOG_STREAM_NAME" , "$LATEST" ),
57
+ FunctionMemorySizeMb : utils .GetEnvWithDefault ("AWS_LAMBDA_FUNCTION_MEMORY_SIZE" , "3008" ),
58
+ FunctionHandler : utils .GetEnvWithDefault ("AWS_LAMBDA_FUNCTION_HANDLER" , os .Getenv ("_HANDLER" )),
59
+ }
60
+ }
61
+
48
62
// UnsetLsEnvs unsets environment variables specific to LocalStack to achieve better runtime parity with AWS
49
63
func UnsetLsEnvs () {
50
64
unsetList := [... ]string {
@@ -81,6 +95,9 @@ func main() {
81
95
82
96
// configuration parsing
83
97
lsOpts := InitLsOpts ()
98
+ functionConf := InitFunctionConfig ()
99
+ awsEnvConf , _ := config .NewEnvConfig ()
100
+
84
101
UnsetLsEnvs ()
85
102
86
103
// set up logging following the Logrus logging levels: https://github.com/sirupsen/logrus#level-logging
@@ -155,6 +172,7 @@ func main() {
155
172
logCollector := logging .NewLogCollector ()
156
173
localStackLogsEgressApi := logging .NewLocalStackLogsEgressAPI (logCollector )
157
174
tracer := tracing .NewLocalStackTracer ()
175
+ // localSupervisor := supervisor.NewLocalSupervisor()
158
176
159
177
// build sandbox
160
178
sandbox := rapidcore .
@@ -169,6 +187,10 @@ func main() {
169
187
SetLogsEgressAPI (localStackLogsEgressApi ).
170
188
SetTracer (tracer )
171
189
190
+ // Externally set supervisor for metrics tracking
191
+ // sandbox.SetSupervisor(localSupervisor)
192
+ // sandbox.SetRuntimeFsRootPath(localSupervisor.RootPath)
193
+
172
194
// xray daemon
173
195
endpoint := "http://" + lsOpts .LocalstackIP + ":" + lsOpts .EdgePort
174
196
xrayConfig := xray .NewConfig (endpoint , xRayLogLevel )
@@ -181,9 +203,6 @@ func main() {
181
203
})
182
204
d .Run () // async
183
205
184
- defaultInterop := sandbox .DefaultInteropServer ()
185
- interopServer := server .NewCustomInteropServer (lsOpts , defaultInterop , logCollector )
186
- sandbox .SetInteropServer (interopServer )
187
206
if len (handler ) > 0 {
188
207
sandbox .SetHandler (handler )
189
208
}
@@ -194,25 +213,27 @@ func main() {
194
213
195
214
// initialize all flows and start runtime API
196
215
sandboxContext , internalStateFn := sandbox .Create ()
197
- // Populate our custom interop server
198
- interopServer .SetSandboxContext (sandboxContext )
199
- interopServer .SetInternalStateGetter (internalStateFn )
216
+ // Populate our interop server
217
+ sandbox . DefaultInteropServer () .SetSandboxContext (sandboxContext )
218
+ sandbox . DefaultInteropServer () .SetInternalStateGetter (internalStateFn )
200
219
201
- // get timeout
202
- invokeTimeoutEnv := utils .GetEnvOrDie ("AWS_LAMBDA_FUNCTION_TIMEOUT" ) // TODO: collect all AWS_* env parsing
203
- invokeTimeoutSeconds , err := strconv .Atoi (invokeTimeoutEnv )
204
- if err != nil {
205
- log .Fatalln (err )
206
- }
207
- go hotreloading .RunHotReloadingListener (interopServer , lsOpts .HotReloadingPaths , fileWatcherContext , lsOpts .FileWatcherStrategy )
220
+ localStackService := server .NewLocalStackAPI (sandbox .LambdaInvokeAPI (), bootstrap , logCollector , xrayConfig .Endpoint , lsOpts , functionConf , awsEnvConf )
208
221
209
222
// start runtime init. It is important to start `InitHandler` synchronously because we need to ensure the
210
223
// notification channels and status fields are properly initialized before `AwaitInitialized`
211
224
log .Debugln ("Starting runtime init." )
212
- server .InitHandler (sandbox .LambdaInvokeAPI (), utils .GetEnvOrDie ("AWS_LAMBDA_FUNCTION_VERSION" ), int64 (invokeTimeoutSeconds ), bootstrap , lsOpts .AccountId ) // TODO: replace this with a custom init
225
+ localStackService .Initialize ()
226
+
227
+ invokeServer := server .NewServer (lsOpts .InteropPort , localStackService )
228
+ invokeServer .RegisterOnShutdown (localStackService .Close )
229
+
230
+ defer invokeServer .Shutdown (context .Background ())
231
+
232
+ go invokeServer .ListenAndServe ()
233
+ go hotreloading .RunHotReloadingListener (sandbox .DefaultInteropServer (), lsOpts .HotReloadingPaths , fileWatcherContext , lsOpts .FileWatcherStrategy )
213
234
214
235
log .Debugln ("Awaiting initialization of runtime init." )
215
- if err := interopServer .AwaitInitialized (); err != nil {
236
+ if err := sandbox . DefaultInteropServer () .AwaitInitialized (); err != nil {
216
237
// Error cases: ErrInitDoneFailed or ErrInitResetReceived
217
238
log .Errorln ("Runtime init failed to initialize: " + err .Error () + ". Exiting." )
218
239
// NOTE: Sending the error status to LocalStack is handled beforehand in the custom_interop.go through the
@@ -221,7 +242,7 @@ func main() {
221
242
}
222
243
223
244
log .Debugln ("Completed initialization of runtime init. Sending status ready to LocalStack." )
224
- if err := interopServer .SendStatus (server .Ready , []byte {}); err != nil {
245
+ if err := localStackService .SendStatus (server .Ready , []byte {}); err != nil {
225
246
log .Fatalln ("Failed to send status ready to LocalStack " + err .Error () + ". Exiting." )
226
247
}
227
248
0 commit comments