Serverless 3, Fastapi 0.104+ and Layers! Error: typing-extension #12324
-
For fastapi version 0.104+ I have encountered the following error: cannot import name 'Doc' from 'typing_extensions' After much trial and error I realized that the problem was in the merge order of the lambda layers, since serverless adds a layer called "sls-sdk-python-v0-3-2" used for observability, which is merged after the layer created for the application, which affects the loading order of the libraries, I assume that the version of typing-extensions is older in this layer and therefore not compatible with the version of fastapi. So, my question, how can I modify the loading order or tell serverless to load its observability layer before adding the layer for the code that we have implemented? Edit: Revised sls-sdk-python-v0-3-2 layer and typing-extensions version is 4.7.1 and fastapi 0.104 and later require typing-extension >=4.8.0 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Just spent a few frustrating hours trying to fix this... Here is a python script that you can run in your CI after deploy to move the sls layer to the first place and thus prevent it from overwriting typing-extensions with an older version. Make sure to #!/usr/bin/env python3
import sys
import boto3
def reorder_layers(lambda_name):
# Initialize a Boto3 client for Lambda
lambda_client = boto3.client('lambda')
# Get the current configuration of the Lambda function, particularly the Layers
response = lambda_client.get_function_configuration(FunctionName=lambda_name)
layers = response.get('Layers', [])
# Separate the desired layer from others
reordered_layers = []
sls_sdk_layer = None
for layer in layers:
if "sls-sdk-python-v0-3-2" in layer['Arn']:
sls_sdk_layer = layer['Arn']
else:
reordered_layers.append(layer['Arn'])
# Prepend 'sls-sdk-python-v0-3-2' if it exists
if sls_sdk_layer:
reordered_layers.insert(0, sls_sdk_layer)
# Update the Lambda function configuration with the new layers order, if there's any change
if reordered_layers:
lambda_client.update_function_configuration(
FunctionName=lambda_name,
Layers=reordered_layers
)
print(f"Updated layers for Lambda function '{lambda_name}' successfully.")
else:
print(f"No layers to update for Lambda function '{lambda_name}'.")
if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage: {} <lambda_function_name>".format(sys.argv[0]))
sys.exit(1)
lambda_function_name = sys.argv[1]
reorder_layers(lambda_function_name) Other approaches:
|
Beta Was this translation helpful? Give feedback.
Just spent a few frustrating hours trying to fix this...
Here is a python script that you can run in your CI after deploy to move the sls layer to the first place and thus prevent it from overwriting typing-extensions with an older version. Make sure to
pip install boto3
first.