aws-secrets-manager-rotation-lambdas icon indicating copy to clipboard operation
aws-secrets-manager-rotation-lambdas copied to clipboard

[ERROR] Runtime.ImportModuleError: Unable to import module 'lambda_function': No module named 'pg'

Open hemantproTR opened this issue 3 years ago • 3 comments

Hi, in CloudWatch getting this error:

[ERROR] Runtime.ImportModuleError: Unable to import module 'lambda_function': No module named 'pg' Traceback (most recent call last):

hemantproTR avatar Aug 25 '21 17:08 hemantproTR

Thank you for opening this issue - we are looking into it.

whygoyal avatar Mar 02 '22 20:03 whygoyal

Please let me know if any updates. Thanks.

babuknb avatar Mar 17 '22 21:03 babuknb

@whygoyal Any update on this?

nomadme avatar May 19 '22 22:05 nomadme

I had the same issue with pymysql, fixed it by installing the package and zipping it with the function itself. You can follow up the AWS documentation for this - https://docs.aws.amazon.com/lambda/latest/dg/python-package.html

Monikaa7 avatar Nov 23 '22 10:11 Monikaa7

Is there anything required in order to fix this problem when lambda is deployed as part of RotationSchedule ?

RotationSchedule:
  Type: AWS::SecretsManager::RotationSchedule
  Properties:
    HostedRotationLambda:
      RotationLambdaName: DBSecretRotationScheduleLambda
      RotationType: PostgreSQLSingleUser
      # ...

Logged error message is basically the same:

[ERROR] Runtime.ImportModuleError: Unable to import module 'lambda_function': No module named '_pg'

veelenga avatar Jan 05 '23 07:01 veelenga

sudo apt-get install libpq-dev

add requirements.txt:

PyGreSQL == 5.0.7

pip3 install --target ./package -r requirements.txt

include the package directory with the lambda_function.py in a zip file use that as the lambda zip file

luchees avatar Jan 11 '23 05:01 luchees

@luchees this is doable if the lambda is a part of the cloudformation template, but in my case, it is a hosted one, meaning cloud formation creates a substack automatically when a rotation schedule is created. I don't have any control over the lambda dependencies.

However, my problem was solved by re-creating RotationSchedule. Not sure why it was broken.

veelenga avatar Jan 11 '23 08:01 veelenga

Don't want to plus one this, but I've had this same issue today, on one AWS account, our function was working properly, for some reason, on the other, it was not.

Exporting from the working AWS account to the none working resolved it, but they were the same size before and after, so I'm not actually sure what that changed.

One of those creepy magic fixes....

To note: This occurred after a PG version update, but only on one of our AWS accounts, despite being updated in the same way, via CloudFormation.

nicholasgriffintn avatar Jan 30 '23 12:01 nicholasgriffintn

Hello everyone! Please review https://repost.aws/knowledge-center/secrets-manager-lambda-rotation. TLDR, if this is still impacting your lambda function, add the Runtime key to your AWS::SecretsManager::RotationSchedule resource (see docs here) with the version of python that the binary .so files are compiled against.

rstevens011 avatar May 15 '23 19:05 rstevens011