Resolve "cannot import name 'DEFAULT_CIPHERS' from 'urllib3.util.ssl_'" in AWS Lambda
Problem Statement
When executing Python code on AWS Lambda using layers, you may encounter this runtime error:
{
"errorMessage": "Unable to import module 'lambda_function': cannot import name 'DEFAULT_CIPHERS' from 'urllib3.util.ssl_'",
"errorType": "Runtime.ImportModuleError"
}
This issue typically occurs when:
- Using Python 3.9 runtime (though it can happen in other versions)
- Installing libraries via layers (
requests
,pandas
,beautifulsoup4
, etc.) - Having version conflicts between
urllib3
,requests
, andboto3
- Using
urllib3
version ≥2.0 which removed theDEFAULT_CIPHERS
attribute
The core problem stems from version incompatibility between urllib3
(v2.0+) and boto3
(AWS SDK), where boto3
hasn't fully adopted the syntax changes introduced in urllib3
2.0.
Recommended Solutions
1. Pin urllib3 Version (Most Reliable Solution)
Restrict urllib3
to versions <2.0 in your layer installation:
pip install urllib3\<2 -t ./python --no-user
pip install requests -t ./python --no-user
pip install pandas -t ./python --no-user
pip install beautifulsoup4 -t ./python --no-user
Zip the layer and apply it to Lambda. This works because:
- Forces compatibility with
boto3
/botocore
- Maintains modern library versions
- Resolves the missing
DEFAULT_CIPHERS
import
::tip For requirement.txt based deployments, add:
urllib3<2
::
2. Upgrade Lambda Runtime to Python 3.10+
Newer Python runtimes include updated boto3
versions with urllib3
v2.0+ support:
- In Lambda Configuration ➔ General configuration ➔ Edit
- Change runtime to Python 3.10 or 3.11
- Verify installed versions in Lambda:
boto3
≥1.26.153botocore
≥1.29.153
::warning After changing runtime, re-test all dependencies. Some libraries may require updates. ::
3. Pin Specific requests Version
If older requests
dependencies are acceptable (not recommended long-term):
pip install requests==2.28.2 -t ./python --no-user
This installs a requests
version with compatible urllib3
dependencies.
4. Docker-Based Installation (For Complex Dependencies)
Build layers using Lambda's Docker image to match AWS environment:
mkdir layer
cp requirements.txt layer/requirements.txt
docker run -ti -v $(pwd)/layer:/app -w /app \
--entrypoint /bin/bash public.ecr.aws/lambda/python:3.11 \
-c "pip3 install --target ./python -r requirements.txt"
zip -r layer.zip python
::tip Windows PowerShell users Replace $(pwd)
with ${pwd}
in the Docker command ::
Why These Solutions Work
The DEFAULT_CIPHERS
error occurs because:
urllib3
2.0 removedDEFAULT_CIPHERS
through refactoring- Older
boto3
/botocore
versions import this removed attribute - Installing default versions of packages like
requests
pullsurllib3>=2.0
The above solutions address compatibility gaps through: Version Pinning: Explicitly controls dependency versions Runtime Updates: Uses Lambda environments with repaired compatibility Environment Matching: Ensures dependencies match Lambda's execution context
Best Practices for Prevention
- Always pin critical dependencies in layers/requirements.txt:text
urllib3<2 boto3>=1.26.153
- Test layers in AWS SAM local before deployment
- Regularly update Lambda runtimes to benefit from backported fixes
- Use dependency isolation:python
import sys sys.path.insert(0, '/opt/python')
Example Solution Code
Final working deployment commands:
# Clean previous installs
rm -rf python/
# Install packages with version control
pip install urllib3\<2 -t ./python --no-user
pip install requests -t ./python --no-user
pip install pandas -t ./python --no-user
pip install beautifulsoup4 -t ./python --no-user
# Verify installed versions
./python/bin/pip freeze | grep 'urllib3\|boto3'
# Package layer
zip -r python_layer.zip python
Key Takeaways
- The root cause is breaking changes in
urllib3
≥2.0 not yet fully supported by AWS SDK - Pinning
urllib3<2
resolves 90% of cases (simplest solution) - Runtime updates to Python 3.10+ provide built-in compatibility
- Always validate dependency versions within the Lambda environment
Use these solutions to maintain compatibility while AWS updates its SDKs. Regularly check AWS Python Support News for future urllib3
v2 support announcements.