AWS Lambda Gotchas

kayh

Kay

Posted on September 23, 2022

AWS Lambda Gotchas

This post includes few gotchas on AWS Lambda.

1) Lambda Python Runtimes - Python 3.6/3.7 are Amazon Linux 1 and Python 3.8/3.9 are Amazon Linux 2

Python 3.6/3.7 are Amazon Linux 1 and Python 3.8/3.9 are Amazon Linux 2.

In general it should be fine to upgrade from Python 3.6 to 3.9.

But there are cases you'll need to make some changes. For example, if you have code utilizing some sys call, e.g. curl - curl is not installed in Amazon Linux 2 by default.

2) AWS CLI not allowing valid JSON in payload parameter with lambda invoke

If you see error like Invalid base64:, it could be because since awscli 2, payloads need to be base64 encoded when invoking a Lambda function.

By default, the AWS CLI version 2 now passes all binary input and binary output parameters as base64-encoded strings. A parameter that requires binary input has its type specified as blob (binary large object) in the documentation.

You will need to pass in also --cli-binary-format raw-in-base64-out. For example:

aws lambda invoke --function-name testsms \
    --invocation-type Event \
    --cli-binary-format raw-in-base64-out \
    --payload '{"key": "test"}' response.json
Enter fullscreen mode Exit fullscreen mode

See also

3) Cannot do ping from Lambda Function

See AWS Lambda FAQs

Lambda attempts to impose as few restrictions as possible on normal language and operating system activities, but there are a few activities that are disabled: Inbound network connections are blocked by AWS Lambda, and for outbound connections, only TCP/IP and UDP/IP sockets are supported, and ptrace (debugging) system calls are blocked. TCP port 25 traffic is also blocked as an anti-spam measure.

4) Code storage for uploaded Lambda functions (CodeStorageExceededException)

The Lambda service stores your function code in an internal S3 bucket that's private to your account. Each AWS account is allocated 75 GB of storage in each Region (and can be increased up to Terabytes). Code storage includes the total storage used by both Lambda functions and layers. If you reach the quota, you receive a CodeStorageExceededException when you attempt to deploy new functions.

See Lambda quotas.

To see the storage used

  1. From AWS Lambda console > Dashboard
  2. From AWS CLI:

    aws lambda list-versions-by-function --function-name myTestFunction
    aws lambda get-layer-version --layer-version --layer-name TestLayer --version-number 2
    

    This returns each published version of the function/layer together with the $LATEST version. The CodeSize attribute shows the total number of bytes used by code storage of this function/layer.

See Monitoring Lambda code storage.

💖 💪 🙅 🚩
kayh
Kay

Posted on September 23, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related