Reputation: 1384
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
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.
nodejs
directory (This name is not accidental, must follow the same)npm i axios
node_modules
and remove everything else in the nodejs
directory.cd.. or whatever
nodejs
directoryconst 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
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
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
Take Layer ARN
Attach Layer to Lambda
Upvotes: 42
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
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
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