1313
1414namespace Ymir \Runtime ;
1515
16- use AsyncAws \Lambda \LambdaClient ;
1716use AsyncAws \Ssm \Input \GetParametersByPathRequest ;
18- use AsyncAws \Ssm \SsmClient ;
1917use AsyncAws \Ssm \ValueObject \Parameter ;
2018use Tightenco \Collect \Support \Arr ;
19+ use Ymir \Runtime \Aws \LambdaClient ;
20+ use Ymir \Runtime \Aws \SsmClient ;
2121use Ymir \Runtime \Exception \InvalidConfigurationException ;
22- use Ymir \Runtime \FastCgi \PhpFpmProcess ;
2322use Ymir \Runtime \Lambda \Handler \ConsoleCommandLambdaEventHandler ;
2423use Ymir \Runtime \Lambda \Handler \Http as HttpHandler ;
2524use Ymir \Runtime \Lambda \Handler \LambdaEventHandlerCollection ;
@@ -37,28 +36,24 @@ class Runtime
3736 */
3837 public static function create (): RuntimeInterface
3938 {
40- $ coldStart = microtime (true );
41- $ logger = new Logger (getenv ('YMIR_RUNTIME_LOG_LEVEL ' ) ?: Logger::INFO );
42- $ runtimeApiClient = new RuntimeApiClient ((string ) getenv ('AWS_LAMBDA_RUNTIME_API ' ), $ logger );
43-
4439 try {
40+ $ coldStart = microtime (true );
41+ $ context = RuntimeContext::createFromEnvironment ();
4542 $ functionType = getenv ('YMIR_FUNCTION_TYPE ' );
46- $ region = getenv ('AWS_REGION ' );
47- $ rootDirectory = getenv ('LAMBDA_TASK_ROOT ' );
4843
4944 if (!is_string ($ functionType )) {
5045 throw new InvalidConfigurationException ('The "YMIR_FUNCTION_TYPE" environment variable is missing ' );
51- } elseif (!is_string ($ rootDirectory )) {
52- throw new InvalidConfigurationException ('The "LAMBDA_TASK_ROOT" environment variable is missing ' );
53- } elseif (!is_string ($ region )) {
54- throw new InvalidConfigurationException ('The "AWS_REGION" environment variable is missing ' );
5546 }
5647
57- self ::injectSecretEnvironmentVariables ($ logger , $ region );
48+ self ::injectSecretEnvironmentVariables ($ context );
49+
50+ $ logger = $ context ->getLogger ();
51+ $ rootDirectory = $ context ->getRootDirectory ();
52+ $ runtimeApiClient = $ context ->getRuntimeApiClient ();
5853
5954 $ handlers = [
6055 new PingLambdaEventHandler (),
61- new WarmUpEventHandler (new LambdaClient ([ ' region ' => $ region ], null , null , $ logger ), $ logger ),
56+ new WarmUpEventHandler (LambdaClient:: createFromContext ( $ context ), $ logger ),
6257 ];
6358
6459 switch ($ functionType ) {
@@ -76,8 +71,7 @@ public static function create(): RuntimeInterface
7671
7772 break ;
7873 case WebsiteRuntime::TYPE :
79- $ maxInvocations = ((int ) getenv ('YMIR_RUNTIME_MAX_INVOCATIONS ' )) ?: null ;
80- $ phpFpmProcess = PhpFpmProcess::createForConfig ($ logger );
74+ $ phpFpmProcess = $ context ->getPhpFpmProcess ();
8175
8276 $ runtime = new WebsiteRuntime ($ runtimeApiClient , new LambdaEventHandlerCollection ($ logger , array_merge ($ handlers , [
8377 // Application/Framework specific handlers
@@ -88,7 +82,7 @@ public static function create(): RuntimeInterface
8882
8983 // Fallback handlers
9084 new HttpHandler \PhpScriptHttpEventHandler ($ logger , $ phpFpmProcess , $ rootDirectory , getenv ('_HANDLER ' ) ?: 'index.php ' ),
91- ])), $ logger , $ phpFpmProcess , $ maxInvocations );
85+ ])), $ logger , $ phpFpmProcess , $ context -> getMaxInvocations () );
9286
9387 $ runtime ->start ();
9488
@@ -111,7 +105,7 @@ public static function create(): RuntimeInterface
111105 /**
112106 * Inject the secret environment variables into the runtime.
113107 */
114- private static function injectSecretEnvironmentVariables (Logger $ logger , string $ region ): void
108+ private static function injectSecretEnvironmentVariables (RuntimeContext $ context ): void
115109 {
116110 $ secretsPath = getenv ('YMIR_SECRETS_PATH ' );
117111
@@ -124,13 +118,13 @@ private static function injectSecretEnvironmentVariables(Logger $logger, string
124118 // results because they use a numbered index.
125119 //
126120 // @see https://stackoverflow.com/questions/70536304/why-does-iterator-to-array-give-different-results-than-foreach
127- collect (iterator_to_array (( new SsmClient ([ ' region ' => $ region ], null , null , $ logger ) )->getParametersByPath (new GetParametersByPathRequest ([
121+ collect (iterator_to_array (SsmClient:: createFromContext ( $ context )->getParametersByPath (new GetParametersByPathRequest ([
128122 'Path ' => $ secretsPath ,
129123 'WithDecryption ' => true ,
130124 ])), false ))->mapWithKeys (function (Parameter $ parameter ) {
131125 return [Arr::last (explode ('/ ' , (string ) $ parameter ->getName ())) => (string ) $ parameter ->getValue ()];
132- })->filter ()->each (function ($ value , $ name ) use ($ logger ): void {
133- $ logger ->debug (sprintf ('Injecting [%s] secret environment variable into runtime ' , $ name ));
126+ })->filter ()->each (function ($ value , $ name ) use ($ context ): void {
127+ $ context -> getLogger () ->debug (sprintf ('Injecting [%s] secret environment variable into runtime ' , $ name ));
134128 $ _ENV [$ name ] = $ value ;
135129 });
136130 }
0 commit comments