How can I use axios in lambda?
Asked Answered
R

6

31

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?

Rockery answered 20/1, 2018 at 13:44 Comment(0)
S
11

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.

Springspringboard answered 20/1, 2018 at 14:52 Comment(3)
And when I upload a zip file. Can I no longer edit it online?Rockery
From 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.Springspringboard
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.Rockery
O
40

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

Create Layer enter image description here Take Layer ARN enter image description here Attach Layer to Lambda enter image description here

Organography answered 1/8, 2021 at 7:39 Comment(6)
Beautiful. Thanks for the article! I'm not sure it's necessary to remove the package.lock though. I didn't and it works fine.Neidaneidhardt
Thank 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.Organography
The key thing to note is that for nodejs the folder structure inside zip must be nodejs/node_modules/....Blockish
On 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.Wappes
This one should be taken as accepted answer. Top notchBerwickupontweed
mkdir nodejs cd nodejs npm init -y # 初始化 package.json 文件 npm install axios # 安装 axios 包 zip -r ../axios.zip node_modules # 直接压缩 node_modules 文件夹Burseraceous
S
11

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.

Springspringboard answered 20/1, 2018 at 14:52 Comment(3)
And when I upload a zip file. Can I no longer edit it online?Rockery
From 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.Springspringboard
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.Rockery
S
7

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! :)

Salcedo answered 31/3, 2021 at 18:26 Comment(0)
H
6

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.

Hartsell answered 20/1, 2018 at 15:3 Comment(2)
And when I upload a zip file. Can I no longer edit it online?Rockery
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.Hartsell
R
4

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.

  1. Create a nodejs directory (This name is not accidental, must follow the same)
  2. Install axios npm i axios
  3. Keep node_modules and remove everything else in the nodejs directory.
  4. Go one step back i.e cd.. or whatever
  5. Zip the nodejs directory
  6. Boom! your Layer is ready to be uploaded to AWS Lambda.
  7. 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.

Reliant answered 10/11, 2022 at 6:27 Comment(0)
F
0

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

Frayne answered 7/7, 2022 at 18:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.