Do I have to install the axios module locally and then deploy it to lambda or is there a way to do it through the inline code editor as well in the browser?
6 Answers
You can publish a simple Node.js AWS Lambda layer with axios package and then attach created layer to your lambda.
List of commands to create .zip file for layer:
mkdir nodejs
cd nodejs
npm i axios
rm -rf package-lock.json
cd ..
zip -r axios.zip nodejs
This list of commands was taken from this article https://ljmocic.medium.com/publish-simple-node-js-aws-lambda-layer-a87c00afdd83

- 670
- 2
- 15
- 25
-
1Beautiful. Thanks for the article! I'm not sure it's necessary to remove the package.lock though. I didn't and it works fine. – Noah Gary Aug 25 '21 at 15:53
-
1Thank you! Yes, I think we can use it without deleting package.lock. The article is not mine and I though that person who wrote this article is trying to follow the best practice regarding making layer and it's a reason why he remove package.lock. – SAndriy Aug 28 '21 at 19:32
-
5The key thing to note is that for nodejs the folder structure inside zip must be `nodejs/node_modules/...`. – human Dec 13 '21 at 12:03
-
3On `macOS` in order to be able to install into an empty folder, I add to do `npm init` first to create the `packag.json` file. – Eli Zatlawy May 30 '22 at 10:43
-
This one should be taken as accepted answer. Top notch – robinHood013 Jan 02 '23 at 11:07
Lambda doesn't actually bundle your package dependencies for you, except the AWS package, so yes you'd need to install it locally, zip it together and upload to the Lambda console.

- 5,553
- 2
- 20
- 27
-
-
1From my experience with Lambda, you'd be able to edit the main entrypoint file, but not the other files, unless all your functions are bundled up in that particular file. So I think you can edit that file online after bundling it up and pushing to AWS. I know recently the Cloud9 integration was launched, for Lambda; but I've not looked into it, so don't know how it might be useful yet, but definitely something to also explore. In summary, you'd still be able to edit your main entry point file but not the others. – oreoluwa Jan 20 '18 at 20:19
-
You're right, previously I could edit my main file. But now, I guess we can no longer edit any of the files if they are being uploaded in zipped format. – thedreamsaver Jan 21 '18 at 18:34
You can also create a layer. With this approach, you can use the Axios module in many functions. You need to be careful with the path though. When you are zipping your modules, folder paths are important. For node14.*, it should be something like this. Your main folder name should be "nodejs".
nodejs/node14/node_modules/axios
nodejs/node14/node_modules/follow-redirects
After attaching your layer, you can directly reach it.
For example:
const axios = require("axios");
exports.handler = async(event) => {
// TODO implement
var response = await axios.post(process.env.URL, { "data": event.data }, {
headers: {
"authorization": process.env.PASS,
"content-type": "application/json",
}
}, { timeout: 10000 }).then(response => response)
.catch((error) => {
//console.log(error.response.status);
//console.log(error.response.data);
//console.log(error.response.headers);
return error;
});;
}
For more info: https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html
This approach helped me a lot, hope it helps others as well! :)

- 1,867
- 1
- 17
- 24
In the folder where your lambda script is present (index.js) run following command -
npm install axios
You should see node_modules
directory getting created in the same directory as index.js. Now zip both these together (index.js and npm_modules) and upload it you your lambda as zip. You can repeat this with other npm module dependencies you have. If you do not want to repeat these manual steps again for each module create package.json
file and add all your module dependencies there and just run npm install
once.

- 66,731
- 38
- 279
- 289
-
-
I have seen AWS team going back on forth with this. You can check if you see inline editor post uploading the zip. My guess is you would not. – Aniket Thakur Jan 20 '18 at 15:36
All we need to do is to install axios locally, zip the folder and upload it to the AWS-Lambda-Layers. Let me show you how.
- Create a
nodejs
directory (This name is not accidental, must follow the same) - Install axios
npm i axios
- Keep
node_modules
and remove everything else in thenodejs
directory. - Go one step back i.e
cd.. or whatever
- Zip the
nodejs
directory - Boom! your Layer is ready to be uploaded to AWS Lambda.
- After uploading and attaching layer to your function, you will be able to access axios in your handler using
const axios = require("axios");
If you want to know how to add/upload and attach Layers in AWS. Well, that's a separate debate.

- 6,454
- 2
- 39
- 33
You should follow @SAndriy solution to add dependencies into AWS lambda, and please read this article https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html

- 924
- 7
- 15