7 Must-Know AWS Lambda tips

1. Application dependencies

error while loading shared libraries: libname.so.1

Put dependencies in lib folder and package them with your executable. AWS Lambda appends lib to LD_LIBRARY_PATH, however, in almost all cases packaged dependencies will have to be tried first. You will need to prepend lib to LD_LIBRARY_PATH to achieve it:

2. Execution time

{“message”: “Endpoint request timed out”}

Lambdas have a default timeout of 3 seconds. It can be increased to 5 minutes. I did not notice an error message when the default timeout has been exceeded. The execution would just stop leaving you clueless. Here is how to adjust the timeout:

Important!

If the lambda function is responding to web requests via API Gateway, then you need to make sure that it does not exceed the 30 second timeout. API Gateway will drop all requests after 30 seconds and this cannot be changed.

3. Working with files

AWS Lambda will base64encode the body of the HTTP request coming from API Gateway. You will need to decode it to get the binary data. You have to configure API Gateway to allow the file types you accept. This is done by first specifying the content type in the header: 
curl --data-binary "@Screenshot.png" -H "Content-Type: image/png" https://uri-to-apigateway

And second by listing the content type in API Gateway:

4. Responding to requests with binary data

API Gateway will read Accept header of the client and will check if the response of the Lambda function is compatible. If the Content-Type of the Lambda function would not be understood by the client, API Gateway will drop the request. If my Lambda served PNG images, then API Gateway will only work with clients that send Accept: image/png header. Here is a CURL example: curl -H "Accept: image/png" https://uri-to-api-gateway

5. Function memory

The default memory limit is 128MB which can be low if you are processing binary files. It is difficult to calculate the exact memory that the function will use. I started with 1500MB and reduced it over time by monitoring the memory usage of the function and setting limits to the filesize.

6. Working with files

If Lambda is working with files, it is best to use S3 to store the file and fetch it from there when Lambda starts executing. There is limit on the POST size that API Gateway allows but there is also tighter limit on Lambda 6MB. If the request data exceeds that limit, the request will be dropped and it would not start executing the function.

7. AWS Lambda outgoing IP Address

Unfortunately, it is not trivial to assign a static IP address to AWS Lambda. The recommended way is to create VPC with NAT Gateway. The major downside is that the costs associated with running AWS Lambda in VPC with a NAT Gateway and Elastic IP are high and will incur cost even when Lambda is not in use.