- Newest
- Most votes
- Most comments
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())
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:
- Use a custom runtime or container image where you can install the necessary language packs.
- Set the locale to a basic one that is available by default, such as "C" or "C.UTF-8".
- 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
Relevant content
- asked a year ago
- asked a year ago
