<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Nacho González-Garilleti on Medium]]></title>
        <description><![CDATA[Stories by Nacho González-Garilleti on Medium]]></description>
        <link>https://medium.com/@nachox7?source=rss-4fe967e48ad3------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*lJkFbHODysbK7xJXqsczAw.jpeg</url>
            <title>Stories by Nacho González-Garilleti on Medium</title>
            <link>https://medium.com/@nachox7?source=rss-4fe967e48ad3------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 05:51:10 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@nachox7/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Integrating AWS Lambda with Amazon SES in a VPC: A Practical Guide]]></title>
            <link>https://medium.com/2devs1cup/integrating-aws-lambda-with-amazon-ses-in-a-vpc-a-practical-guide-80fe7d78bbd2?source=rss-4fe967e48ad3------2</link>
            <guid isPermaLink="false">https://medium.com/p/80fe7d78bbd2</guid>
            <category><![CDATA[aws-simple-email-service]]></category>
            <category><![CDATA[lambda-function]]></category>
            <category><![CDATA[smtp]]></category>
            <category><![CDATA[nodemailer]]></category>
            <category><![CDATA[amazon-web-services]]></category>
            <dc:creator><![CDATA[Nacho González-Garilleti]]></dc:creator>
            <pubDate>Sat, 25 May 2024 12:17:28 GMT</pubDate>
            <atom:updated>2024-05-25T12:19:42.594Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3ki6B1u1k_zGM_9HmNHZOg.png" /></figure><p>When deploying serverless applications on AWS, integrating AWS Lambda with other AWS services in a VPC can significantly enhance security and network control. In this article, I’ll share my experience configuring AWS Lambda within a VPC to send emails using Amazon Simple Email Service (SES), including the complexities encountered and how I resolved them.</p><h3>Why use AWS Lambda with VPC?</h3><p>Using AWS Lambda in a Virtual Private Cloud (VPC) allows for controlled network access to internal and AWS services without exposure to the public internet. This setup is crucial for handling sensitive data or interfacing securely with services like databases, internal APIs, or, in my case, Amazon SES for sending emails.</p><h3>Initial Setup Challenges</h3><p>The initial configuration seemed straightforward. I aimed to set up a Lambda function to send emails through SES while ensuring the traffic remained within the AWS network for enhanced security and compliance. For that, I started following the documentation for Node.JS:</p><ul><li><a href="https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/ses-examples-sending-email.html">https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/ses-examples-sending-email.html</a></li></ul><pre>// Load the AWS SDK for Node.js<br>var AWS = require(&quot;aws-sdk&quot;);<br>// Set the region<br>AWS.config.update({ region: &quot;REGION&quot; });<br><br>// Create sendEmail params<br>var params = {<br>  Destination: {<br>    /* required */<br>    CcAddresses: [<br>      &quot;EMAIL_ADDRESS&quot;,<br>      /* more items */<br>    ],<br>    ToAddresses: [<br>      &quot;EMAIL_ADDRESS&quot;,<br>      /* more items */<br>    ],<br>  },<br>  Message: {<br>    /* required */<br>    Body: {<br>      /* required */<br>      Html: {<br>        Charset: &quot;UTF-8&quot;,<br>        Data: &quot;HTML_FORMAT_BODY&quot;,<br>      },<br>      Text: {<br>        Charset: &quot;UTF-8&quot;,<br>        Data: &quot;TEXT_FORMAT_BODY&quot;,<br>      },<br>    },<br>    Subject: {<br>      Charset: &quot;UTF-8&quot;,<br>      Data: &quot;Test email&quot;,<br>    },<br>  },<br>  Source: &quot;SENDER_EMAIL_ADDRESS&quot; /* required */,<br>  ReplyToAddresses: [<br>    &quot;EMAIL_ADDRESS&quot;,<br>    /* more items */<br>  ],<br>};<br><br>// Create the promise and SES service object<br>var sendPromise = new AWS.SES({ apiVersion: &quot;2010-12-01&quot; })<br>  .sendEmail(params)<br>  .promise();<br><br>// Handle promise&#39;s fulfilled/rejected states<br>sendPromise<br>  .then(function (data) {<br>    console.log(data.MessageId);<br>  })<br>  .catch(function (err) {<br>    console.error(err, err.stack);<br>  });</pre><p>Then I noticed that using SAM in my local setup was working, although after deploying it, it was not. After reading some forums, I realize that as my Lambda is connected to a VPC, I need to either make the Lambda connect to the public internet by using a NAT Gateway or VPC endpoints with PrivateLink. Due to my project requirements, the VPC endpoint is the preferred option. Below, I left a link to another article explaining the alternative.</p><h3>Step 1: Configuring the VPC Endpoint for SES</h3><p>Amazon SES supports VPC endpoints via AWS PrivateLink, but only for SMTP traffic. This detail is crucial because it means you cannot use the SES API directly within a VPC without internet access.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iPaynKnhcwiv6PMEhcXJgg.png" /><figcaption>VPC Endpoint Creation</figcaption></figure><h3>Step 2: Security Group Configuration</h3><p>I created a security group for the SES VPC endpoint, allowing inbound traffic on the SMTP ports (25, 465, and 587). The Lambda security group needs to have access, as per the screenshot.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XCNYkH0-u6iUKH5VxAfWRg.png" /><figcaption>Security Group Settings</figcaption></figure><h3>Step 3: Switching to SMTP with Nodemailer</h3><p>As we aim to use SMTP instead of AWS SDK for SES, which is HTTP, we need to switch toNodemailer, a Node.js library that can send emails over SMTP. This required configuring Nodemailer to use the SES SMTP interface.</p><pre>const nodemailer = require(&#39;nodemailer&#39;);<br><br>// SMTP transport configuration<br>const transporter = nodemailer.createTransport({<br>    host: &quot;email-smtp.us-west-2.amazonaws.com&quot;,<br>    port: 587,<br>    secure: false,<br>    auth: {<br>        user: process.env.SES_SMTP_USER,<br>        pass: process.env.SES_SMTP_PASSWORD,<br>    },<br>});</pre><h3>Step 4: Testing and Final Adjustments</h3><p>After adjusting the Lambda function to send emails via SMTP, tests were successful, and emails were sent through SES without needing to route traffic outside the AWS network.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Kx0fWuInfGsMLqDoTBesWw.png" /><figcaption>SES Dashboard Metrics</figcaption></figure><h3>Lessons Learned and Best Practices</h3><ol><li>Understand Service Limitations: Always check the documentation for limitations, such as the supported protocols for services when used within a VPC.</li><li>Security Is Key: Properly configure security groups and network ACLs to safeguard your resources.</li><li>Monitoring and Logging: Utilize CloudWatch for monitoring and logging to troubleshoot and optimize configurations.</li></ol><h3>Conclusion</h3><p>Configuring AWS Lambda with SES in a VPC required overcoming unexpected challenges, primarily due to the limitations of the VPC endpoint for SES. By switching to SMTP and using Nodemailer, I was able to achieve the desired functionality securely and efficiently.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cteoCjpqP7VJN8Llxgk6yg.jpeg" /></figure><p>I hope these insights help others navigate similar configurations.</p><h3>Alternative solution using NAT Gateway instead of VPC endpoint</h3><p>It is worth mentioning that in case you prefer to use the AWS API and don’t mind connecting the Lambda to the public internet, I found this article where it is pretty well covered:</p><p><a href="https://medium.com/@sysharma899/unlocking-ses-in-your-vpc-the-secret-to-secure-email-sending-9b4416aa0fc7">https://medium.com/@sysharma899/unlocking-ses-in-your-vpc-the-secret-to-secure-email-sending-9b4416aa0fc7</a></p><h3>References</h3><ul><li><a href="https://docs.aws.amazon.com/ses/latest/dg/send-email-set-up-vpc-endpoints.html">https://docs.aws.amazon.com/ses/latest/dg/send-email-set-up-vpc-endpoints.html</a></li><li><a href="https://docs.aws.amazon.com/vpc/latest/privatelink/create-interface-endpoint.html">https://docs.aws.amazon.com/vpc/latest/privatelink/create-interface-endpoint.html</a></li><li><a href="https://aws.amazon.com/blogs/aws/new-amazon-simple-email-service-ses-for-vpc-endpoints/">https://aws.amazon.com/blogs/aws/new-amazon-simple-email-service-ses-for-vpc-endpoints/</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=80fe7d78bbd2" width="1" height="1" alt=""><hr><p><a href="https://medium.com/2devs1cup/integrating-aws-lambda-with-amazon-ses-in-a-vpc-a-practical-guide-80fe7d78bbd2">Integrating AWS Lambda with Amazon SES in a VPC: A Practical Guide</a> was originally published in <a href="https://medium.com/2devs1cup">2devs1cup</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>