"Unable to import module 'lambda_function': No module named 'charset_normalizer'"

0

i'm following this" Tutorial: Creating a search application with Amazon OpenSearch Service", on this url 'https://docs.aws.amazon.com/opensearch-service/latest/developerguide/search-example.html'.

Now in the lambda functions screen under functions I have a testFunction. Here i uploaded a zip file with a folder 'packages' and a file 'lambda_function.py'. If I test the file in the browser I get this error "Unable to import module 'lambda_function': No module named 'charset_normalizer'". In the package folder there is a folder 'charset_normalizer'?

If I run the code local, I get no errors...

3 Answers
1
Accepted Answer

Hello robbe, I think what causes the error is that Lambda cannot find the module 'charset_normalizer'. I think you can solve it by creating a layer for you Lambda. Try to follow these steps:

  1. Go to pypi charset-normalizer download page: https://pypi.org/project/charset-normalizer/#files
  2. Download whl file for desired Python version x86_64. (e.g. charset_normalizer-3.0.1-cp311-cp311-musllinux_1_1_x86_64.whl). You need to check the compatible version of the package with the runtime of your lambda function.
  3. Create a folder with the name "python", don't use other names different than “python” because it won't work
  4. Move all the extracted files from whl to "python" folder.
  5. zip compress this "python" folder.

Now you can upload the zip file on Lambda to create the Layer by following these steps:

  1. Go to Lambda
  2. On the left side pane go to "Additional resources" -> "Layers"
  3. Click on "Create Layer" on the right
  4. Give your layer a name
  5. Upload the zip file you created
  6. In "Compatible architectures" choose x86_64
  7. In "Compatible runtimes" choose the code runtime you used in your lambda function
  8. Click on "Create"

Now you can add the Layer to your Lambda:

  1. Go to you Lambda function
  2. Scroll down to the bottom until you see the "Layers" banner
  3. Click on "Add a layer"
  4. Choose "Custom Layer"
  5. In the dropdown menu select the name of the layer you previously created
  6. In the "Version" dropdown menu select the version of the layer
  7. Click on "Add"

Now try to test your function again and see if the error is solved.

I attach this medium article that explains how to create pandas and numpy layer for Lambda, maybe it can be useful in case my answer is not clear: https://medium.com/@shimo164/lambda-layer-to-use-numpy-and-pandas-in-aws-lambda-function-8a0e040faa18

profile picture
answered a year ago
  • Thanks for the answer, i think my import errors are gone. Now i have this error when i test: "list indices must be integers or slices, not str", i have copied the code from the tutorial so i dont know what the error is. my code looks like this:

    def lambda_handler(event, context):

    # Put the user query into the query DSL for more accurate search results.
    # Note that certain fields are boosted (^).
    query = {
        "size": 25,
        "query": {
            "multi_match": {
                "query": event['queryStringParameters']['q'],
                "fields": ["title", "directors"]
            }
        }
    }
    
    # Elasticsearch 6.x requires an explicit Content-Type header
    headers = {"Content-Type": "application/json"}
    
    # Make the signed HTTP request
    r = requests.get(url, auth=awsauth, headers=headers,
                     data=json.dumps(query))
    
    # Create the response and add some extra content to support CORS
    response = {
        "statusCode": 200,
        "headers": {
            "Access-Control-Allow-Origin": '*'
        },
        "isBase64Encoded": False
    }
    
    # Add the search results to the response
    response['body'] = r.text
    return response
    

    The error is on this line: "query": event['queryStringParameters']['q']

0

Hello robbe, sorry for my late response, what I understand from the error is that Lambda sees event['queryStringParameters'] as a list and tries to access element at position 'q' that throws an error since only integers can be used as indices in a list. Could you please add a print(event) on the line before

query = {
    "size": 25,
    "query": {
        "multi_match": {
            "query": event['queryStringParameters']['q'],
            "fields": ["title", "directors"]
        }
    }
}

and provide me the output so I can read what Lamdba gets as input and have a better understanding of the error?

profile picture
answered a year ago
0

Hi Gaia, thank you for your clear instructions. Simple clear answers like yours seem to be a rare thing on these aws forums at times.
This worked a treat for me when updating our lambda from the 3.7 python runtime to 3.11.

answered 6 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions