Skip to content

Send Mail on AWS Lambda NodeJS with Nodemailer

0

I have the code bellow. It works on my machine, but on AWS Lambda the error below is thrown. I checked on VPC Security groups and granted all traffic outbound. Does anybody know the reason of this error and any possibile solution?

ERROR:

connect ETIMEDOUT
        "errno": -110,
        "code": "ESOCKET",
        "syscall": "connect",
        "command": "CONN"

CODE:

        const transporter = nodemailer.createTransport({
            host: 'smtp.zoho.com',
            port: 465,
            secure: true,
            auth: {
                user: credentials.user,
                pass: credentials.password
            }
        });
1 Answer
1
Accepted Answer

Hello.

Is Lambda connected to VPC?
In that case Lambda Please configure NAT Gateway so that you can access "smtp.zoho.com" from.
When Lambda connects to a VPC, it requires a NAT Gateway to access the public internet.
https://repost.aws/knowledge-center/internet-access-lambda-function

Also, if Lambda does not need to connect to AWS services (EC2 or RDS) in the VPC, there is no need to connect to the VPC.

EXPERT
answered 2 years ago
EXPERT
reviewed a year ago
  • Lambda is hosted on Public Subnet with Internet Gateway. Does I have to install a NAT gateway on this subnet??

  • Even if Lambda is placed in a public subnet, the ENI does not have a public IP address attached to it, so it must be routed to the public internet through a NAT Gateway. Therefore, you need to place Lambda in a private subnet and configure settings to route it via NAT Gateway.

  • Instead of creating a NAT Gateway (price USD67) I created a NAT Instance with EC2 t4g.micro (price USD6) and it works very well. Thank you!

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.