99 "strconv"
1010 "strings"
1111
12+ "github.com/aws/aws-sdk-go-v2/config"
1213 "github.com/localstack/lambda-runtime-init/internal/aws/xray"
1314 "github.com/localstack/lambda-runtime-init/internal/bootstrap"
1415 "github.com/localstack/lambda-runtime-init/internal/hotreloading"
@@ -24,8 +25,8 @@ import (
2425func InitLsOpts () * server.LsOpts {
2526 return & server.LsOpts {
2627 // 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" ),
2930 AccountId : utils .GetEnvWithDefault ("LOCALSTACK_FUNCTION_ACCOUNT_ID" , "000000000000" ),
3031 // optional with default
3132 InteropPort : utils .GetEnvWithDefault ("LOCALSTACK_INTEROP_PORT" , "9563" ),
@@ -45,6 +46,19 @@ func InitLsOpts() *server.LsOpts {
4546 }
4647}
4748
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+
4862// UnsetLsEnvs unsets environment variables specific to LocalStack to achieve better runtime parity with AWS
4963func UnsetLsEnvs () {
5064 unsetList := [... ]string {
@@ -81,6 +95,9 @@ func main() {
8195
8296 // configuration parsing
8397 lsOpts := InitLsOpts ()
98+ functionConf := InitFunctionConfig ()
99+ awsEnvConf , _ := config .NewEnvConfig ()
100+
84101 UnsetLsEnvs ()
85102
86103 // set up logging following the Logrus logging levels: https://github.com/sirupsen/logrus#level-logging
@@ -155,6 +172,7 @@ func main() {
155172 logCollector := logging .NewLogCollector ()
156173 localStackLogsEgressApi := logging .NewLocalStackLogsEgressAPI (logCollector )
157174 tracer := tracing .NewLocalStackTracer ()
175+ // localSupervisor := supervisor.NewLocalSupervisor()
158176
159177 // build sandbox
160178 sandbox := rapidcore .
@@ -169,6 +187,10 @@ func main() {
169187 SetLogsEgressAPI (localStackLogsEgressApi ).
170188 SetTracer (tracer )
171189
190+ // Externally set supervisor for metrics tracking
191+ // sandbox.SetSupervisor(localSupervisor)
192+ // sandbox.SetRuntimeFsRootPath(localSupervisor.RootPath)
193+
172194 // xray daemon
173195 endpoint := "http://" + lsOpts .LocalstackIP + ":" + lsOpts .EdgePort
174196 xrayConfig := xray .NewConfig (endpoint , xRayLogLevel )
@@ -181,9 +203,6 @@ func main() {
181203 })
182204 d .Run () // async
183205
184- defaultInterop := sandbox .DefaultInteropServer ()
185- interopServer := server .NewCustomInteropServer (lsOpts , defaultInterop , logCollector )
186- sandbox .SetInteropServer (interopServer )
187206 if len (handler ) > 0 {
188207 sandbox .SetHandler (handler )
189208 }
@@ -194,25 +213,27 @@ func main() {
194213
195214 // initialize all flows and start runtime API
196215 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 )
200219
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 )
208221
209222 // start runtime init. It is important to start `InitHandler` synchronously because we need to ensure the
210223 // notification channels and status fields are properly initialized before `AwaitInitialized`
211224 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 )
213234
214235 log .Debugln ("Awaiting initialization of runtime init." )
215- if err := interopServer .AwaitInitialized (); err != nil {
236+ if err := sandbox . DefaultInteropServer () .AwaitInitialized (); err != nil {
216237 // Error cases: ErrInitDoneFailed or ErrInitResetReceived
217238 log .Errorln ("Runtime init failed to initialize: " + err .Error () + ". Exiting." )
218239 // NOTE: Sending the error status to LocalStack is handled beforehand in the custom_interop.go through the
@@ -221,7 +242,7 @@ func main() {
221242 }
222243
223244 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 {
225246 log .Fatalln ("Failed to send status ready to LocalStack " + err .Error () + ". Exiting." )
226247 }
227248
0 commit comments