Reputation: 45
I am trying to run AWS CLI commands on a Lambda function. I referred to How to use AWS CLI within a Lambda function (aws s3 sync from Lambda) :: Ilya Bezdelev and generated a zip file with the awscli package. When I try to run the lambda function I get the following error:
START RequestId: d251660b-4998-4061-8886-67c1ddbbc98c Version: $LATEST
[INFO] 2020-06-22T19:15:45.232Z d251660b-4998-4061-8886-67c1ddbbc98c
Running shell command: /opt/aws --version
Traceback (most recent call last):
File "/opt/aws", line 19, in <module>
import awscli.clidriver
ModuleNotFoundError: No module named 'awscli'
What could be the issue here?
Upvotes: 0
Views: 1820
Reputation: 770
AS @tvmaynard said, you first need to add all the packages inside the same path as aws script
of the AWS-CLI
, by using this command:
cp -r ../${VIRTUAL_ENV_DIR}/lib/python${PYTHON_VERSION}/site-packages/. .
But, Even after that you will face a problem that there is some libraries that AWS-CLI
is dependent on and must be installed in the Runtime Python as PyYAML
, to install it you need to have access to Python Runtime inside the lambda, which is Not allowed.
Even if, you try to solve this by telling the interpreter where to search for the PyYAML
library and installed it inside /tmp/
, as follow:
import sys
import subprocess
stderr = subprocess.PIPE
stdout = subprocess.PIPE
cmd_1 = "pip install PyYAML -t /tmp/ --no-cache-dir"
subprocess.Popen(
args=cmd_1, start_new_session=True, shell=True, text=True, cwd=None,
stdout=stdout, stderr=stderr
)
sys.path.insert(1, '/tmp/')
import yaml
You will be able to use the library by importing it only inside the lambda function context as if you added a layer to the lambda, But not from the underlying command line which is linked to the python interpreter inside the lambda Runtime and have a default path to search for the libraries it needs.
You will also, Pay More Money to install this and the may needed other libraries, every time you trigger your lambda, Which if on Production ENV, will add more money to your Bill, that doesn't generate value.
Upvotes: 0
Reputation: 11
Everything in the 'site-packages' folder needs to be directly in the zip, and subsequently the /opt/ folder for the lambda, NOT nested inside a 'site-packages' folder which is what the tutorial results in unfortunately when you use his commands verbatim.
Upvotes: 1