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?
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.
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
nodejs/node_modules/...
. –
Blockish 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 mkdir nodejs
cd nodejs
npm init -y # 初始化 package.json 文件
npm install axios # 安装 axios 包
zip -r ../axios.zip node_modules # 直接压缩 node_modules 文件夹
–
Burseraceous 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.
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! :)
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.
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.
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
© 2022 - 2024 — McMap. All rights reserved.