Google Cloud function - Sendgrid 443 Error [duplicate] - node.js

This question already has answers here:
What's the cause of the error 'getaddrinfo EAI_AGAIN'?
(15 answers)
Closed 3 years ago.
When I tried to send an email from google function its throws following err
{ Error: getaddrinfo EAI_AGAIN api.sendgrid.com:443 at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:67:26) errno: 'EAI_AGAIN', code: 'EAI_AGAIN', syscall: 'getaddrinfo', hostname: 'api.sendgrid.com', host: 'api.sendgrid.com', port: 443 }
But I tried exact same code on my local machine it works totally fine
Here is my function
const sgMail = require('#sendgrid/mail');
sgMail.setApiKey(SENDGRID_API_KEY);
var sendMail = function (templateId, receivers, dynamicData, sender, mailSubject) {
var mailMsg ={
to :receivers[0].email,
from : sender[0].email ,
subject : mailSubject,
templateId : templateId,
dynamic_template_data : dynamicData
}
sgMail.send(mailMsg).then(() => console.log("mail sent"))
.catch(err => console.log(err));
}
Thank You in advance!!!!

This is another related question: link. See the discussion where it is said that the free "Spark" plan "allows outbound network requests only to Google-owned services".

Related

Executing ksql query using node npm ksqlDb-client throwing an timeout error

Hi am trying to execute the KSQL query using npm package ksqlDb-client package, it throws an timeout error. I have attached the code as well, please let me know any issues over there.
when am hit this GET URL below method will execute https://localhost:5000/testKsql
exports.getKSQLStream = async () => {
const options = {
authorization: {
username: "admin",
password: "pw",
ssl: {
ca: CAs,
crt: myClientCert,
key: myClientKey,
}
},
host: 'https://ixxxx.xcxx.net',
timeout: 20000
}
const client = new KsqldbClient(options);
await client.connect();
const streamRes = await client.query('list streams;');
console.log("streams",streamRes);
await client.disconnect();
}
when I hit that URL from postman am getting below response in node console.
Error on Http2 client. Error: connect ETIMEDOUT 16.2XX.X4.xxx:8088
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1159:16) {
errno: -4039,
code: 'ETIMEDOUT',
syscall: 'connect',
address: '16.2XX.X4.xxx',
port: 8088
}
I faced recently with the same issue and found that library has bug with authorization. I pushed PR in repo, but you can patch your local copy in the same way.
https://github.com/Streaminy/ksqldb-client/pull/4

"EC2 Metadata roleName request returned error" using aws encryption library with NestJS

I am trying to use aws encryption sdk in a NestJS application, the next code source shows the implementation.
import { Controller, Get } from '#nestjs/common';
import {KmsKeyringNode, encrypt} from '#aws-crypto/client-node'
#Controller('encryption')
export class EncryptionController {
// constructor() {}
#Get()
async crypt() {
const generatorKeyId = "generatior key";
const masterKeyId = "master key id";
const keyring = new KmsKeyringNode({keyIds:[masterKeyId], generatorKeyId: generatorKeyId});
const plainText = "My passwords for senstive data";
const context = {
accountId: "100",
purpose: "youtube demo",
country: "Sri Lanka"
};
const { result } = await encrypt(keyring, plainText, { encryptionContext: context });
console.log(result)
}
}
but when I execute a request to see the result of my implementation I receive the next error
Error: connect EHOSTUNREACH 169.254.169.254:80
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1128:14) {
message: 'Missing credentials in config, if using AWS_CONFIG_FILE, set AWS_SDK_LOAD_CONFIG=1',
errno: 'EHOSTUNREACH',
code: 'CredentialsError',
syscall: 'connect',
address: '169.254.169.254',
port: 80,
time: 2020-06-09T11:41:47.638Z,
originalError: {
message: 'Could not load credentials from any providers',
errno: 'EHOSTUNREACH',
code: 'CredentialsError',
syscall: 'connect',
address: '169.254.169.254',
port: 80,
time: 2020-06-09T11:41:47.638Z,
originalError: {
message: 'EC2 Metadata roleName request returned error',
errno: 'EHOSTUNREACH',
code: 'EHOSTUNREACH',
syscall: 'connect',
address: '169.254.169.254',
port: 80,
time: 2020-06-09T11:41:47.637Z,
originalError: [Object]
}
}
}
However I copy a nodejs script from this tutorial to see if it works with my aws sdk set up
and I did not receive any error, also if a execute aws kms encryption methods in the cli I hadn't any error.
I tried to export AWS_SDK_LOAD_CONFIG=1 variable how I saw in other similar errors.
Does anyone know what's going on?
I had the same problem. It cost me quite some head ache because I had this running in AWS Fargate and debugging is not that easy there.
The error means the Javascript SDK can not find the AWS credentials.
Here you can see in what order the SDK tries to load the credentials from:
https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/setting-credentials-node.html
My error was quite embarrassing, I just had a typo in my environment variables. My variable was AWS_ACCESSS_KEY_ID instead of AWS_ACCESS_KEY_ID. (Quite hard to see the difference, right?)
So probably double check the names of your environment variables (or config files)

getaddrinfo EAI_AGAIN account-d.docusign.com account-d.docusign.com:443

I am using superagent to hit the docusign API with code received on the response of concent
const respo = await superagent
.post('https://account-d.docusign.com/oauth/token')
.set('Authorization', `${auth}`)
.send({
grant_type: 'authorization_code',
code: '.........'
})
err : { Error: getaddrinfo EAI_AGAIN account-d.docusign.com account-d.docusign.com:443
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:56:26)
errno: 'EAI_AGAIN',
code: 'EAI_AGAIN',
syscall: 'getaddrinfo',
hostname: 'account-d.docusign.com',
host: 'account-d.docusign.com',
port: 443,
response: undefined }
Based on Error: getaddrinfo EAI_AGAIN
"EAI_AGAIN is a DNS lookup timed out error, means it is a network connectivity error or proxy related error."
You may want to check if you have a proxy/firewall or other issues on the box making the API calls. This is a networking issue you need to fix that has nothing to do with DocuSign directly.
Thank you for the suggestion. But it was because I was not sending proper authentication.
I did use curl though for this :
curl
.setHeaders([
`Authorization: Basic ${auth}`,
"Content-Type: application/x-www-form-urlencoded"
])
.setBody({
grant_type: "authorization_code",
code: code
})
.post("https://account-d.docusign.com/oauth/token")
.then(({ statusCode, body, headers }) => {})
.catch(e => {});

How to fire POST request using inline editor in dialogflow?

Code:
var rp = require('request-promise');
var options = {
method: 'POST',
uri: 'http://c663fe13.ngrok.io/ap/lighton',
body: {"color": 'white'},
json: true // Automatically stringifies the body to JSON
};
rp(options)
.then(function (parsedBody) {
// POST succeeded...+
console.log("parsedBody", parsedBody);
})
.catch(function (err) {
// POST failed...
console.log("err", err);
});
but this gives me the following error:
{ RequestError: Error: getaddrinfo EAI_AGAIN c663fe13.ngrok.io:80
at new RequestError (/srv/node_modules/request-promise-core/lib/errors.js:14:15)
at Request.plumbing.callback (/srv/node_modules/request-promise-core/lib/plumbing.js:87:29)
at Request.RP$callback [as _callback] (/srv/node_modules/request-promise-core/lib/plumbing.js:46:31)
. . .
name: 'RequestError', message: 'Error: getaddrinfo EAI_AGAIN
c663fe13.ngrok.io:80',
cause: { Error: getaddrinfo EAI_AGAIN
c663fe13.ngrok.io:80
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:67:26)
errno: 'EAI_AGAIN',
code: 'EAI_AGAIN',
syscall: 'getaddrinfo',
hostname: 'c663fe13.ngrok.io',
host: 'c663fe13.ngrok.io',
port: 80 },
I trie to call that API with postman and it's working fine.
If you are using a free account you will not be able to hit any 3rd party services from Firebase cloud functions. It would be better if you write down your own webhook code and using fulfillment integrate that webhook with your Dialogflow agent if you are going to use the free account.
UPDATE
Check out the code snippets I have shared here. You can use that to integrate ExpressJS and then add your POST code and host it locally. Expose the local server using ngrok and then put that URL in fulfillment.

sendgrid set up with #sendgrid/mail

This error message pops up sometimes when I try to send emails with npm package sendgrid/mail. This works most of the times.
{ Error: connect ETIMEDOUT 169.45.89.179:443
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1104:14) errno: 'ETIMEDOUT', code: 'ETIMEDOUT', syscall: 'connect',
address: '169.45.89.179', port: 443 } [bugsnag] Reported an
unhandled rejection… Error: Error: connect ETIMEDOUT 169.45.89.179:443
at SendGrid.send.then.catch.e (/home/leoqiu/foodnome-api/build/src/utils/emailHelpers.js:143:11)
My node server send it out with the following code:
export const sendVerifyMail = (to: string, token: string) =>
SendGrid.send({
to,
from: { email: '..' },
subject: 'Verify you..',
dynamic_template_data: {
header: 'Verify your account',
text:
'Please use the button below to continue the process.',
c2a_link: `${serverAddress}/api/user-account/verify?token=${token}`,
c2a_button: 'Verify'
},
template_id: 'd-0f6411434fbc4896bf389e3945affd5d'
} as any)
.then(d => d)
.catch(e => {
console.log(e);
throw new Error(e);
});
I don't think this is an issue with your code or on your side of the network, unless you had general connectivity issues at the same time.
ip 169.45.89.179 resolves to sendgrid's domain so this is probably an issue on their end, to double check this you could set up a continuous ping to 8.8.8.8 or run some other network monitoring set up to make sure your connection out is stable.
If your connection is not the issue, I would just report it to them along with any logs you are prepared to share, your source ip and the time the timeout errors occur would probably be useful to them

Resources