'Snowflake Connector for Python - Doesn't work in Lambda Function

UPDATE - I have gotten past the "unable to find snowflake-connector" issue, but now it appears that when I build the Lambda function, some of the dependencies (that the connector relies on) cannot be resolved, even though I can see them in my virtual environment.

Running PythonPipBuilder:ResolveDependencies

Build Failed Error: PythonPipBuilder:ResolveDependencies - {cffi==1.15.0(wheel), cryptography==3.4.8(wheel)} SAM Build has failed: Command did not exit successfully, exit code: 1 170 has failed: Command did not exit successfully, exit code: 1

Any ideas why it would not be able to resolve these two when it can resolve the other modules in my virtual environment?

Original question - I am working in Pycharm and trying to connect to snowflake from lambda. I have installed the snowflake connector in the project's virtual environment. I have other packages that I can access from the same lambda without an issue. When I try to use the this one I get this error, indicating that it cannot find the package.

{"errorMessage": "Unable to import module 'app': No module named 'snowflake.connector'; 'snowflake' is not a package", "errorType": "Runtime.ImportModuleError", "stackTrace": []}

 import snowflake.connector
 import os

 def lambda_handler(event, context):       
        # Gets the version
        ctx = snowflake.connector.connect(
            user='my-user',
            account='my-account',
            password="my-password",
            authenticator='externalbrowser',
            warehouse="my-warehouse",
            database="my-database",
            schema="my-schema"
        )
        cs = ctx.cursor()
        try:
            cs.execute("SELECT current_version()")
            one_row = cs.fetchone()
            print(one_row[0])
        finally:
            cs.close()
        ctx.close()

I tried adding a python file to the root of the project and tried the same code and it was able to connect without a problem so that tells me the the snowflake connector is installed correctly and that lambda just cannot find that one package.

Can anyone help me resolve this issue?



Solution 1:[1]

Make sure that your requirements.txt file contains the dependencies you want to install

snowflake-connector-python

Keep rest of the code as is

import snowflake.connector
import os

 def lambda_handler(event, context):       
        # Gets the version
        ctx = snowflake.connector.connect(
            user='my-user',
            account='my-account',
            password="my-password",
            authenticator='externalbrowser',
            warehouse="my-warehouse",
            database="my-database",
            schema="my-schema"
        )
        cs = ctx.cursor()
        try:
            cs.execute("SELECT current_version()")
            one_row = cs.fetchone()
            print(one_row[0])
        finally:
            cs.close()
        ctx.close()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 PM 77-1