0
votes

I am working on a piece of code in AWS Lambda wriiten in Java which need to call a third party system through REST (fire & forget).

As per this approach, every time a request comes from a user, a new Lambda will be executed which will establish a connection to make a third party call.

Is there any alernate way to overcome connection establishing. Some connection pooling I can make use if I can redirect my calls through API gateway or other connection pool?

Thanks in advance!

1

1 Answers

1
votes

Per the Lambda documentation:

After a Lambda function is executed, AWS Lambda maintains the Execution Context for some time in anticipation of another Lambda function invocation

[...]

Any declarations in your Lambda function code (outside the handler code, see Programming Model) remains initialized, providing additional optimization when the function is invoked again. For example, if your Lambda function establishes a database connection, instead of reestablishing the connection, the original connection is used in subsequent invocations.

This is relevant if your Lambda function is invoked repeatedly within a short time frame (I think it's 300 seconds). If it's invoked less frequently then you'll get a new container each time and will need to re-establish your connections.

In the case of a Java implementation, "outside the handler code" includes the object constructor. For example, in this code I create a couple of services within the constructor of my Lambda handler class, and store them as member variables.

You could also do lazy instantiation, which I think is more relevant for non-Java implementations (Java gives you constructors, so use them!).

One thing to be aware of is that the underlying connections might time out while the Lambda is inactive. So ensure that you test the connection before use (in the case of a database connection pool this is easy: you give it a "test on checkout" query, and the pool will replace the connection if it's no longer valid).