0

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

2
  • Please include error text in your post rather than a link to an external image. Commented Jul 18, 2022 at 14:49
  • You could use something like this to create a paramiko layer regardless of your host platform. Commented Jul 19, 2022 at 3:39

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much that solved the first problem. But now I'm getting this error: "errorMessage": "Unable to import module 'lambda_function': cannot import name 'asn1' from 'cryptography.hazmat.bindings._rust' (unknown location)". Any idea where this is coming from?
@J.Fasterling you are most likely not running on a linux system!? In that case pip installs the wrong binary dependencies, you would need to build the zip file e.g. using docker: github.com/jetbridge/paramiko-lambda-layer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.