1

I have a basic MongoDB Atlas trigger in NodeJS :

exports = function(changeEvent) {
  
 // Make an http request
  
};

My goal is to call an AWS lambda function from this trigger, so i would like to know the syntax to make a basic GET request to a specific endpoint from the NodeJS code.

EDIT :

I am unable to run axios, the dependency is indeed installed but the get method doesn't seem to exist.

enter image description here

2 Answers 2

2

NPM to install axios in your node js app -> npm i axios

i have put data key in axios to send some data to another server if needed:

const axios = require("axios");

async function Api() {
  const response = await axios.get("url",{
  headers : {
    "Content-Type" : "application/json"
  },
  data : {name : "someName"}
  })
  
  //receive data from request
  console.log(response.data);
  return response.data;
}


Api()

Sign up to request clarification or add additional context in comments.

5 Comments

I am unable to make axios working. Please see the edit of my question
ok please tell me what you get if you do console.log(axios) at line number 4 in your screenshot. what output you are getting in console ??
and tell me which axios version you have in package.json?
did you get any result?
Alex in the below answer gave the solution using default. I have no package.json since i am in Atlas trigger
1
+50

First of all, for Lambda functions you will be better off with Event Bridge which is designed exactly for this purpose and utilizes native AWS services directly.

Secondly, you cannot trigger lambda directly without exposing aws console credentials. The better option is to set up an API gateway which accepts http requests and triggers the lambda.

Finally, assuming the API gateway is already there, the Atlas trigger would be as simple as:

exports = async function(changeEvent) {
  const requests=require("requests");
  return await requests("<API gateway url>", {method: "POST", data:{changeEvent}});
};

UPDATE

Syntax for axios version:

exports = async function(changeEvent) {
  const axios = require('axios');  
  return axios.default.post('<API gateway url>',changeEvent);
};

5 Comments

Thanks for the explanations, indeed i have not specified but i am using API gateway ! :) I have tried your code but the requests module seems to not be built in : FunctionError: Cannot find module 'requests'
I have added the dependency and it's working i see the request log in CloudWatch. Also, how can i console.log the output json of this request please ? I am unable to find any documentation of this module, only the request package that has been depecrated. Is it the official way to make http requests in Node JS ?
github.com/unshiftio/requests but you can use any, e.g. axios as suggested by jerry
I updated with axios example. For future readers - there must be fetch awailble natively soon. EventBridge is still a better option - you will have all logs in your cloudWatch. console.log still works, you just don't have access to read the logs for Atlas-owned triggers.
Thanks it's working fine now using the default attribute.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.