Netflify lambda function working locally but not in production

Viewed 491

enter image description here

I'm trying to use netlify and its lambda function feature to run a node function . Based on https://css-tricks.com/using-netlify-forms-and-netlify-functions-to-build-an-email-sign-up-widget/ , I have in my functions/submission-created.js:

const https = require("https");

exports.handler = async event => {

const email = JSON.parse(event.body).payload.data.EMAIL
const asking = JSON.parse(event.body).payload.data.ASKING

var formData = {
    'email': email,
    'first_name': '',
    'last_name': asking,
    'lists[]': 'NUM'
};
var encoded = Object.entries(formData).map(([k, v]) => `${k}=${encodeURIComponent(v)}`).join("&");

var endpoint = 'https://api.sendfox.com/contacts/?' + encoded;

const data = JSON.stringify(formData);
const options = {

    method: 'POST',
    connection: 'keep-alive',

    headers: {
    'Authorization': 'Bearer hhhhh',
    'Content-Type': 'application/json',
    },
    'content-length': data.length,

};
console.log(email);

const req = https.request(endpoint, options, (res) => {
    console.log('statusCode:', res.statusCode);
    console.log('headers:', res.headers);

    res.on('data', (d) => {
    console.log(d);
    });
});

req.on('error', (e) => {
    console.error(e);
});

req.write(data);
req.end();

return {
    statusCode: 200,
    body: data
};
}

This works as expected when I run it locally with netlify dev, but when pushed to the github repo used by netlify to build the site, it does not work in production. How can I fix this?

The package structure looks like the screenshot:

EDIT:

the netlify.toml :

[build]
  functions = "./functions

No errors. The output on sites function tab is:

8:57:43 PM: 2020-12-08T01:57:43.384Z    undefined   INFO    to here
8:57:43 PM: 2020-12-08T01:57:43.390Z    8ca26edc-1f20-4c20-b038- 
79ecdf206d92    INFO    yt@ghj.org
8:57:43 PM: 2020-12-08T01:57:43.390Z    8ca26edc-1f20-4c20-b038- 
79ecdf206d92    INFO    999
8:57:43 PM: 2020-12-08T01:57:43.390Z    8ca26edc-1f20-4c20-b038- 
79ecdf206d92    INFO    yt@ghj.org
8:57:43 PM: Duration: 39.71 ms  Memory Usage: 69 MB Init Duration: 
176.22 ms   
2 Answers

As noted, the information you give are insufficient regarding what has gone wrong. However, I will try to answer based on what I have.

In the tutorial, I noticed the usage of dotenv package. The package is used in order to setup different configuration in different environments.

It utilizes different production files related to the environments, allowing you for example to setup a .env file for local development, and a different one (e.g.,.env.production) for your production.

Based on the setup, variables setup in the respective .env file utilized on each environment, are loaded in the process.env object.

Now, on your tutorial, I noticed that you have the loading of crucial variables being loaded from .env such as EMAIL_TOKEN. I suspect that your setup expects a separate dotenv file for production - and by not finding it, it loads the parameters required as empty, silently. Please revise what environment it loads and what your configuration is at your environments, respectively.

Also, consider the following tutorial for working with dotenv vars.

This could be because of incorrect settings on the API Gateway. Check what all traffic is allowed. Sometimes you may have just allowed TCP traffic. Change it to all traffic.

I could also be that you have a malformed lambda response. Check this AWS documentation Resolve malformed 502 Api gateway

Related