How do I deploy Lambda functions with external libraries using AWS Cloud9?

Last updated: 2021-09-10

I want to add external libraries to an AWS Lambda function that I developed using AWS Cloud9. How do I do that?

Resolution

Note: The following procedure uses the Python runtime as an example. A similar procedure can be used for Node.js Lambda functions.

After you've created a Lambda function in your AWS Cloud9 environment, do the following:

Import the external libraries' modules to your Lambda function's parent folder in your AWS Cloud9 environment

1.    Open the AWS Cloud9 console.

2.    Open your AWS Cloud9 environment.

3.    In the left navigation pane, choose Environment. The Environment window opens.

4.    Open the context (right-click) menu for your function's parent folder.
Note: Your function's parent folder is listed with the same name as your function.

5.    Choose Open Terminal Here. A new terminal session starts.

6.    Import an external library module by running the following pip command in the new terminal:

Important: Replace ModuleName with the name of the module that you want to import.

python -m pip install --target=./ ModuleName

Note: To make sure that the correct versions of Python packages are installed, it's a best practice to specify a requirements.txt file in the pip command. For more information, see the Requirements file format section of the pip documentation.

7.    Repeat step six for each module that you want to import. The imported modules appear in your function's parent folder.

Deploy your Lambda function using the AWS Toolkit

Follow the instructions in Deploying a serverless application in the AWS Cloud9 User Guide. After deploying your function, you can run your code using the additional modules that you imported.

For more information, see Working with AWS serverless applications using the AWS Toolkit.