VOOZH about

URL: https://repost.aws/questions/QUVqE6rr97TgK1yC_K5Ne7xA/unsupported-locale-setting-error-in-python-3-12-lambda-runtime-due-to-missing-langpacks

⇱ Unsupported Locale Setting error in Python 3.12 Lambda Runtime due to missing langpacks | AWS re:Post


Skip to content

Unsupported Locale Setting error in Python 3.12 Lambda Runtime due to missing langpacks

1

I've created a GitHub issue on the lambda images repo, but perhaps this is a better place to surface this item.

On Python 3.12 with Amazon Linux 2023, there is an error setting the locale that was not present in any previous version of the image (e.g. Python 3.10/11 on Amazon Linux 2), so I believe this to be a bug.

It seems the langpacks are missing in this image.

Is there a known issue with locale configuration/langpacks in the AWS Lambda Python 3.12 runtime images? Would this be added to the next image release?

Language
English

asked a year ago916 views

2 Answers
  • Newest
  • Most votes
  • Most comments
2

Hello.

As stated in the document below, Amazon Linux 2023 container images do not include locale.
https://docs.aws.amazon.com/linux/al2023/ug/image-comparison.html

Currently, I think the solution is to register locale data as a Lambda layer.

docker run -it --rm -v ./volume:/volume amazonlinux:2023 bash
dnf -y install glibc-langpack-en glibc-langpack-es zip
cp -r /usr/lib/locale/en_US.utf8 /volume/locale/
cp -r /usr/lib/locale/es_ES.utf8 /volume/locale/
cd /volume
zip -r locale.zip locale
exit

cd volume/
aws lambda publish-layer-version --layer-name my-layer --zip-file fileb://locale.zip --compatible-runtimes python3.12 --compatible-architectures "x86_64"

After creating the layer, set "LOCPATH" in the Lambda environment variable and set the value to "/opt/locale".
After setting the environment variables, connect the layer to Lambda and run the code below to set the locale.

import locale
import os

def lambda_handler(event, context):
 print(os.environ['LOCPATH'])
 print('Before setting locales:', locale.getlocale())
 locale.setlocale(locale.LC_TIME, 'en_US.UTF-8')
 print('After setting to en_US.UTF-8:', locale.getlocale());
 locale.setlocale(locale.LC_TIME, 'es_ES.UTF-8');
 print('After setting to es_ES.UTF-8:', locale.getlocale())
EXPERT

answered a year ago

0
This recommendation was generated using AWS Generative AI capabilities. You are responsible for evaluating the recommendation in your specific context and implementing appropriate oversight and safeguards. Learn more
This answer has not been reviewed by an expert

Yes, there is a known issue with locale configuration in the AWS Lambda Python 3.12 runtime images. This problem is related to the transition to Amazon Linux 2023 as the base operating system for the Python 3.12 Lambda runtime.

In previous versions of the Lambda runtime (e.g., Python 3.10 and 3.11) which were based on Amazon Linux 2, locale settings worked without issues. However, with the shift to Amazon Linux 2023 for Python 3.12, the necessary language packs are not included by default.

The error you're encountering, "Unsupported Locale Setting," is due to the absence of the required locale packages in the new runtime environment. This affects operations that depend on locale settings, such as date formatting or language-specific string operations.

To resolve this issue, you would typically need to install the "glibc-all-langpacks" package. However, in the context of AWS Lambda, you don't have direct access to install system packages in the runtime environment.

As a workaround, you might consider the following options:

  1. Use a custom runtime or container image where you can install the necessary language packs.
  2. Set the locale to a basic one that is available by default, such as "C" or "C.UTF-8".
  3. Wait for AWS to address this issue in a future update to the Python 3.12 Lambda runtime.

It's worth noting that this is a significant change in behavior compared to previous Lambda runtimes, and it may affect many Python applications that rely on specific locale settings. AWS is likely aware of this issue, given its impact, and may provide a solution in future updates to the Lambda runtime.

In the meantime, if your application critically depends on specific locale settings, you might need to consider using a custom runtime or reverting to Python 3.11 until the issue is resolved.

Sources
setlocale stopped working in lambda python 3.12 | AWS re:Post
Update Lambda Runtime (pyhton 3.9 to 3.11) | AWS re:Post

answered a year ago

  • Doug
    4 months ago

    If only it was addressed in subsequent runtimes ...