Sagar Davara
Sagar Davara

Reputation: 454

How to allow outgoing request from gcp cloud functions with node 16?

I am using node 16 with cloud functions. and I am using Twilio and outlook APIs. I can access this third party apis in my local system using firebase function serve,

but when I deploy this functions on GCP, all outgoing request like accessing third party APIs not working and gives error related,

Connection timed out and code: 'ECONNABORTED'.

So how can I fix this ?

Upvotes: 0

Views: 189

Answers (1)

Hemanth Kumar
Hemanth Kumar

Reputation: 3774

In order to allow outgoing requests from Google Cloud Functions with Node.js version 16, you need to configure the environment variable 'FUNCTIONTRIGGERTYPE' to 'http'.

This can be done by adding the following to the cloud function's configuration file:

environment_variables:
  FUNCTION_TRIGGER_TYPE: http

Once the environment variable has been set, you can then use the http module to make outgoing requests from inside your cloud function. For example:

const http = require('http');

exports.myFunction = (req, res) => {
  const options = {
    hostname: 'example.com',
    port: 80,
    path: '/',
    method: 'GET'
  };
  const req = http.request(options, (res) => {
    // handle response
  });
  req.end();
};

Additionally, check below points also :

  • you should make sure that the network access control lists of your VPC allow the outbound requests from the cloud function. You can do this by adding an egress rule to the VPC.
  • You can also use the Node.js http or https modules to make requests. You can find more information about making requests with Node.js in the Node.js documentation

Upvotes: 1

Related Questions