4

I've deployed a lambda (python3.7) which uses pg8000

import pg8000
..
def get_connection():
    """
        Method to establish the connection.
    """
    try:
        print ("Connecting to database")
        # Create a low-level client with the service name for rds
        client = boto3.client("rds")
        # Read the environment variables to get DB EndPoint
        DBEndPoint = os.environ.get("DBEndPoint")
        # Read the environment variables to get the Database name
        DatabaseName = os.environ.get("DatabaseName")
        # Read the environment variables to get the Database username which has access to database.
        DBUserName = os.environ.get("DBUserName")
        # Generates an auth token used to connect to a db with IAM credentials.
        password = client.generate_db_auth_token(
            DBHostname=DBEndPoint, Port=5432, DBUsername=DBUserName
        )
        # Establishes the connection with the server using the token generated as password
        conn = pg8000.connect(
            host=DBEndPoint,
            user=DBUserName,
            database=DatabaseName,
            password=password,
            ssl={"sslmode": "verify-full", "sslrootcert": "rds-ca-2015-root.pem"},
        )
        print("Succesful connection!")
        return conn
    except Exception as e:
        print ("While connecting failed due to :{0}".format(str(e)))
        return None
...

I have a requirements.txt which contains:

pg8000==1.13.2
boto3==1.9.67

I'm performing a sam build, sam package and sam deploy. Does sam build not handle the download of dependencies like pg8000?

Exact commands:

sam build
sam package --template-file ./template.yml --output-template-file output.yml --s3-bucket xxx-bucket
sam deploy --template-file ./output.yml --stack-name demo --capabilities CAPABILITY_IAM

Error after triggering lambda:

[ERROR] Runtime.ImportModuleError: Unable to import module 'index': No module named 'pg8000'
John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
DenCowboy
  • 13,884
  • 38
  • 114
  • 210

1 Answers1

5

According to doc

sam build looks for a manifest file (such as requirements.txt) that contains the dependencies, and automatically creates deployment artifacts.

It seems like your modules haven't been deployed at all.

I'll explain how to fix it step by step:

  • sam build created a folder .aws-sam/build with your lambda function in it including dependencies and one more very important file: your use this template.yml with CodeUri (path to lambdas src code) relative to .aws-sam/build folder.

  • sam package command should pass the location of your template.yml as argument, meaning sam build destination folder url . run sam package --template-file .\.aws-sam\build\template.yml --output-template-file .\.aws-sam\build\output.yml --s3-bucket your_bucket

  • Finally run sam deploy --template-file .\.aws-sam\build\output.yml --stack-name demo --capabilities CAPABILITY_IAM

When you executed this command: sam package --template-file ./template.yml --output-template-file output.yml --s3-bucket xxx-bucket ,template file ./template.yml point to your lambda funcion without the dependencies which are located in .\.aws-sam\build\your_lambda.

One more thing:

If you are using multiple modules you should consider using Layers

Hope this helps

Assael Azran
  • 2,863
  • 1
  • 9
  • 13