You want to run awscli commands from within Lambda function but the Lambda execution environment doesn’t provide pre-installed AWS CLI and it can’t be installed using pip package. What options do we have?
You can create a Lambda Layer that contains all the necessary dependencies and make it available to the function. First, install AWS CLI in a local virtual environment, package AWS CLI and all its dependencies to a zip file and create a Lambda Layer.
You will need Python3, pip, zip and virtualenv on your machine.
Note: These instructions are tested on WSL on Windows.
Verify whether you already have these on your machine, else install them-
–> Open the WSL terminal and run the following commands in the terminal.
–> Run following commands to create an isolated virtual environment using vitualenv and activate it-
Now your prompt will look like below (it will have awscli-venv at the beginning)-
—> Now install AWS CLI and its dependencies
–> The AWS CLI executable is the file named aws and its first line provides the path to the Python interpreter. In a virtual environment, it will have a path local to the environment: #!/PATH_TO_WORKING_DIR/awscli/awscli-venv/bin/python3
This path needs to be replaced with the path to the Python interpreter in the Lambda execution environment, which is #!/var/lang/bin/python.
You can open
aws in a text editor and replace it manually or run this command to replace it-
–> Now deactivate virtualenv
–> Run following commands to package AWS CLI and its dependencies in a zip file-
–> As package zip file is created, delete the temp directories which were created-
–> Now go to the Lambda console, add Lambda layer following the wizard to create a layer. Upload lambda-layer.zip and select python3.6 as runtime-
–> Create a Lambda function using Python 3.6 which is used for packaging AWS CLI. Once the function is created, in Designer, click on Layers, click Add layer and select the custom layer created-
–> Paste the following code and execute the function by clicking the Test button
Note: You have to provide the /opt/aws path to invoke the cli.
Information from following post used to setup this lambda layer –