33use Throwable ;
44use com \amazon \aws \lambda \{Context , Environment };
55use io \IOException ;
6- use lang \{XPClass , XPException };
6+ use lang \{XPClass , XPException , Environment as System };
77use peer \http \{HttpConnection , RequestData };
88use text \json \{Json , StreamInput };
99use util \cmd \Console ;
1616class AwsRunner {
1717
1818 /**
19- * Returns the lambda handler class using the `_HANDLER` environment
20- * variable .
19+ * Returns the lambda handler instance using the `_HANDLER` and
20+ * `LAMBDA_TASK_ROOT` environment variables .
2121 *
22- * @return lang.XPClass
22+ * @param [:string] $environment
23+ * @param io.streams.StringWriter $writer
24+ * @return com.amazon.aws.lambda.Handler
2325 */
24- private static function handler () {
25- return XPClass::forName ($ _ENV ['_HANDLER ' ]);
26+ private static function handler ($ environment , $ writer ) {
27+ return XPClass::forName ($ environment ['_HANDLER ' ])->newInstance (new Environment (
28+ $ environment ['LAMBDA_TASK_ROOT ' ] ?? '. ' ,
29+ $ writer ,
30+ $ environment
31+ ));
2632 }
2733
2834 /**
2935 * Returns a lambda API endpoint using the `AWS_LAMBDA_RUNTIME_API`
3036 * environment variable.
3137 *
3238 * @see https://docs.aws.amazon.com/lambda/latest/dg/runtimes-api.html
39+ * @param [:string] $environment
3340 * @param string $path
3441 * @return peer.http.HttpConnection
3542 */
36- private static function endpoint ($ path ) {
37- $ c = new HttpConnection ("http:// {$ _ENV ['AWS_LAMBDA_RUNTIME_API ' ]}/2018-06-01/runtime/ {$ path }" );
43+ private static function endpoint ($ environment , $ path ) {
44+ $ c = new HttpConnection ("http:// {$ environment ['AWS_LAMBDA_RUNTIME_API ' ]}/2018-06-01/runtime/ {$ path }" );
3845
3946 // Use a 15 minute timeout, this is the maximum lambda runtime, see
4047 // https://docs.aws.amazon.com/lambda/latest/dg/gettingstarted-limits.html
@@ -95,12 +102,13 @@ private static function error($e) {
95102 * @return int
96103 */
97104 public static function main ($ args ) {
105+ $ environment = System::variables ();
98106
99107 // Initialization
100108 try {
101- $ lambda = self ::handler ()-> newInstance ( new Environment ( $ _ENV [ ' LAMBDA_TASK_ROOT ' ] , Console::$ out) )->lambda ();
109+ $ lambda = self ::handler ($ environment , Console::$ out )->lambda ();
102110 } catch (Throwable $ t ) {
103- self ::endpoint ('init/error ' )->post (
111+ self ::endpoint ($ environment , 'init/error ' )->post (
104112 new RequestData (self ::error ($ t )),
105113 ['Content-Type ' => 'application/json ' ]
106114 );
@@ -110,13 +118,13 @@ public static function main($args) {
110118 // Process events using the lambda runtime interface
111119 do {
112120 try {
113- $ r = self ::endpoint ('invocation/next ' )->get ();
121+ $ r = self ::endpoint ($ environment , 'invocation/next ' )->get ();
114122 } catch (IOException $ e ) {
115123 Console::$ err ->writeLine ($ e );
116124 break ;
117125 }
118126
119- $ context = new Context ($ r ->headers (), $ _ENV );
127+ $ context = new Context ($ r ->headers (), $ environment );
120128 try {
121129 $ event = 0 === $ context ->payloadLength ? null : self ::read ($ r ->in ());
122130
@@ -127,7 +135,7 @@ public static function main($args) {
127135 $ response = self ::error ($ t );
128136 }
129137
130- self ::endpoint ("invocation/ {$ context ->awsRequestId }/ {$ type }" )->post (
138+ self ::endpoint ($ environment , "invocation/ {$ context ->awsRequestId }/ {$ type }" )->post (
131139 new RequestData ($ response ),
132140 ['Content-Type ' => 'application/json ' ]
133141 );
0 commit comments