thedreamsaver
thedreamsaver

Reputation: 1384

How can I use axios in lambda?

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?

Upvotes: 34

Views: 57762

Answers (6)

Malik Khalil Ahmad
Malik Khalil Ahmad

Reputation: 6925

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.

Upvotes: 6

Fe3back
Fe3back

Reputation: 1034

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

Upvotes: 0

SAndriy
SAndriy

Reputation: 808

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

Upvotes: 42

Oguz
Oguz

Reputation: 1926

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

Upvotes: 7

Aniket Thakur
Aniket Thakur

Reputation: 68935

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.

Upvotes: 6

oreoluwa
oreoluwa

Reputation: 5633

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.

Upvotes: 11

Related Questions