Search code examples
pythonamazon-web-servicesaws-lambdaimporterrorparamiko

AWS Lambda function can't import paramiko - Unable to import module 'lambda_function': No module named


I try to import paramiko in my AWS Lambda function, written with Python 3.9. I installed paramiko in a folder with pip install paramiko -t . and made a .zip file out of it. Then I added this .zip file to a Lambda Layer and connected it with my Lambda function. And in the Lambda function, I imported paramiko. But now when I test the code the compiler still tells me: Unable to import module 'lambda_function': No module named 'paramiko'.

This is the lambda function:

import io
import boto3
import paramiko

ACCESS_KEY = '...'
PRIVATE_ACCESS_KEY = '...'
AWS_REGION = 'eu-central-1'
INSTANCE = ['...']
EC2_IP = '...'

def lambda_handler(event, context):
    
    #start EC2 instance
    ec2 = boto3.client('ec2', region_name=AWS_REGION)
    ec2.start_instances(InstanceIds=INSTANCE)
    
    print('Started the instance: ' + str(INSTANCE))

Of course, the dots are filled with my credentials.

Error message


Solution

  • Lambda unzips layers to /opt.
    The Python path contains /opt/python.
    You therefore need to sure that your directory structure inside your zip layer should include a top-level python directory and the libraries should all be in that python directory.