2828import java .util .concurrent .ExecutorService ;
2929import java .util .concurrent .Executors ;
3030
31+ import com .amazonaws .services .lambda .runtime .ClientContext ;
32+ import com .amazonaws .services .lambda .runtime .CognitoIdentity ;
33+ import com .amazonaws .services .lambda .runtime .Context ;
34+ import com .amazonaws .services .lambda .runtime .LambdaLogger ;
35+ import com .amazonaws .services .lambda .runtime .LambdaRuntime ;
3136import org .apache .commons .logging .Log ;
3237import org .apache .commons .logging .LogFactory ;
3338
34-
3539import org .springframework .cloud .function .context .FunctionCatalog ;
3640import org .springframework .cloud .function .context .catalog .SimpleFunctionRegistry .FunctionInvocationWrapper ;
3741import org .springframework .cloud .function .context .config .RoutingFunction ;
@@ -130,6 +134,8 @@ private void eventLoop(ConfigurableApplicationContext context) {
130134 logger .debug ("Attempting to get new event" );
131135 ResponseEntity <String > response = this .pollForData (rest , requestEntity );
132136
137+ Context clientContext = generateClientContext (response .getHeaders ());
138+
133139 if (logger .isDebugEnabled ()) {
134140 logger .debug ("New Event received: " + response );
135141 }
@@ -140,9 +146,9 @@ private void eventLoop(ConfigurableApplicationContext context) {
140146 FunctionInvocationWrapper function = locateFunction (environment , functionCatalog , response .getHeaders ());
141147
142148 ByteArrayInputStream is = new ByteArrayInputStream (response .getBody ().getBytes (StandardCharsets .UTF_8 ));
143- Message <?> requestMessage = AWSLambdaUtils .generateMessage (is , function .getInputType (), function .isSupplier (), mapper , null );
144-
149+ Message <?> requestMessage = AWSLambdaUtils .generateMessage (is , function .getInputType (), function .isSupplier (), mapper , clientContext );
145150 Object functionResponse = function .apply (requestMessage );
151+
146152 byte [] responseBytes = AWSLambdaUtils .generateOutputFromObject (requestMessage , functionResponse , mapper , function .getOutputType ());
147153
148154 String invocationUrl = MessageFormat
@@ -157,12 +163,91 @@ private void eventLoop(ConfigurableApplicationContext context) {
157163 }
158164 }
159165 catch (Exception e ) {
166+ e .printStackTrace ();
160167 this .propagateAwsError (requestId , e , mapper , runtimeApi , rest );
161168 }
162169 }
163170 }
164171 }
165172
173+ private Context generateClientContext (HttpHeaders headers ) {
174+
175+ Map <String , String > environment = System .getenv ();
176+
177+ Context context = new Context () {
178+
179+ @ Override
180+ public int getRemainingTimeInMillis () {
181+ long now = System .currentTimeMillis ();
182+ if (!headers .containsKey ("Lambda-Runtime-Deadline-Ms" )) {
183+ return 0 ;
184+ }
185+ int delta = (int ) (Long .parseLong (headers .getFirst ("Lambda-Runtime-Deadline-Ms" )) - now );
186+ return delta > 0 ? delta : 0 ;
187+ }
188+
189+ @ Override
190+ public int getMemoryLimitInMB () {
191+ if (!environment .containsKey ("AWS_LAMBDA_FUNCTION_MEMORY_SIZE" )) {
192+ return 128 ;
193+ }
194+ return Integer .parseInt (environment .getOrDefault ("AWS_LAMBDA_FUNCTION_MEMORY_SIZE" , "128" ));
195+ }
196+
197+ @ Override
198+ public LambdaLogger getLogger () {
199+ return LambdaRuntime .getLogger ();
200+ }
201+
202+ @ Override
203+ public String getLogStreamName () {
204+ return environment .get ("LOG_STREAM_NAME" );
205+ }
206+
207+ @ Override
208+ public String getLogGroupName () {
209+ return environment .get ("LOG_GROUP_NAME" );
210+ }
211+
212+ @ Override
213+ public String getInvokedFunctionArn () {
214+ return headers .getFirst ("Lambda-Runtime-Invoked-Function-Arn" );
215+ }
216+
217+ @ Override
218+ public CognitoIdentity getIdentity () {
219+ return null ;
220+ }
221+
222+ @ Override
223+ public String getFunctionVersion () {
224+ return environment .get ("FUNCTION_VERSION" );
225+ }
226+
227+ @ Override
228+ public String getFunctionName () {
229+ return environment .get ("FUNCTION_NAME" );
230+ }
231+
232+ @ Override
233+ public ClientContext getClientContext () {
234+ return null ;
235+ }
236+
237+ @ Override
238+ public String getAwsRequestId () {
239+ return headers .getFirst ("Lambda-Runtime-Aws-Request-Id" );
240+ }
241+
242+ public String toString () {
243+ return "FUNCTION NAME: " + getFunctionName () + ", FUNCTION VERSION: " + getFunctionVersion ()
244+ + ", FUNCTION ARN: " + getInvokedFunctionArn () + ", FUNCTION MEM LIMIT: " + getMemoryLimitInMB ()
245+ + ", FUNCTION DEADLINE: " + getRemainingTimeInMillis ();
246+ }
247+ };
248+ return context ;
249+ }
250+
166251 private void propagateAwsError (String requestId , Exception e , JsonMapper mapper , String runtimeApi , RestTemplate rest ) {
167252 String errorMessage = e .getMessage ();
168253 String errorType = e .getClass ().getSimpleName ();
0 commit comments