<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Eadan Fahey</title>
    <description>The latest articles on DEV Community by Eadan Fahey (@eadanfahey).</description>
    <link>https://dev.to/eadanfahey</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F186202%2F39683bdb-d658-45e8-af1a-0504720bdd65.jpeg</url>
      <title>DEV Community: Eadan Fahey</title>
      <link>https://dev.to/eadanfahey</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/eadanfahey"/>
    <language>en</language>
    <item>
      <title>AWS Lambda VPC Networking</title>
      <dc:creator>Eadan Fahey</dc:creator>
      <pubDate>Tue, 25 Jun 2019 21:36:02 +0000</pubDate>
      <link>https://dev.to/eadanfahey/aws-lambda-vpc-networking-p3a</link>
      <guid>https://dev.to/eadanfahey/aws-lambda-vpc-networking-p3a</guid>
      <description>&lt;p&gt;Let’s face it, AWS networking isn’t exactly the most exciting thing in the world. Often, I’ve had a Lambda function that needs to connect to a database inside my VPC while still allowing connections to the internet. To put it kindly, configuring AWS networking to handle this common situation is quite involved.  This article is short manual to get it done.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create a public subnet&lt;/li&gt;
&lt;li&gt;Create a NAT gateway&lt;/li&gt;
&lt;li&gt;Create private subnets&lt;/li&gt;
&lt;li&gt;Configure VPC gateway endpoints&lt;/li&gt;
&lt;li&gt;Configure the Lambda function&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Create a public subnet
&lt;/h2&gt;

&lt;p&gt;Subnets divide the range of IP addresses available to a VPC into sub-sections, defined by &lt;a href="https://en.wikipedia.org/wiki/Classless_Inter-Domain_Routing" rel="noopener noreferrer"&gt;CIDR&lt;/a&gt; blocks. For example, &lt;code&gt;172.31.0.0/16&lt;/code&gt; is a CIDR block representing 65,536 addresses from &lt;code&gt;172.31.0.0&lt;/code&gt; to &lt;code&gt;172.31.255.255&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;A subnet can be referred to as public or private. A &lt;em&gt;public subnet&lt;/em&gt; has its traffic routed through an internet gateway, whereas a &lt;em&gt;private subnet&lt;/em&gt; does not. An internet gateway is a VPC component that allows communication to the internet. The &lt;a href="https://docs.aws.amazon.com/vpc/latest/userguide/default-vpc.html" rel="noopener noreferrer"&gt;default&lt;/a&gt; VPC, automatically created for your AWS account, comes pre-configured with a public subnet in each availability zone.&lt;/p&gt;

&lt;p&gt;Subnets have a &lt;em&gt;route table&lt;/em&gt; that describes how its network traffic is directed. The route table of a public subnet is shown below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F2fcn8xbs8er2rg3nvjc1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F2fcn8xbs8er2rg3nvjc1.png" alt="Route table of a public subnet"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this example, all traffic destined to addresses within the VPC is kept local to the VPC. All other traffic (&lt;code&gt;0.0.0.0/0&lt;/code&gt;) is sent to an internet gateway.&lt;/p&gt;

&lt;p&gt;To create a public subnet:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Navigate to the VPC service on the AWS console.&lt;/li&gt;
&lt;li&gt;Create a route table called &lt;code&gt;rt-to-igw&lt;/code&gt;. Add a route to direct non-local traffic to an internet gateway, as in the example above.&lt;/li&gt;
&lt;li&gt;Create a new subnet called &lt;code&gt;lambda-public-subnet-1&lt;/code&gt;. 

&lt;ul&gt;
&lt;li&gt;Choose the same VPC as that of the AWS resources you want to connect to.&lt;/li&gt;
&lt;li&gt;Specify a CIDR block that the subnet will own, for example, &lt;code&gt;172.31.16.0/24&lt;/code&gt; (256 addresses). There cannot be any overlap with the blocks owned by other subnets, so you may need to reallocate the addresses used elsewhere.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;After the subnet is created, change its route table to the one we created — &lt;code&gt;rt-to-igw&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Create a NAT gateway
&lt;/h2&gt;

&lt;p&gt;EC2 instances can be assigned a public IP address. This is what allows you to, for example, SSH into it from your development machine, and to communicate with the outside internet. However, Lambda functions cannot be directly assigned a public IP address. Instead, we can use a &lt;em&gt;Network Address Translation&lt;/em&gt; (NAT) gateway to enable a Lambda function to communicate with the internet.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Navigate to the VPC service on the AWS console and choose "NAT Gateways".&lt;/li&gt;
&lt;li&gt;Create a new NAT gateway. Choose &lt;code&gt;lambda-public-subnet-1&lt;/code&gt; for its subnet and assign it an elastic IP.&lt;/li&gt;
&lt;li&gt;After the NAT gateway is created, name it &lt;code&gt;lambda-nat-1&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Create a new route table called &lt;code&gt;rt-to-lambda-nat-1&lt;/code&gt;. Add a route directing all non-local traffic — &lt;code&gt;0.0.0.0/0&lt;/code&gt; — to the NAT gateway &lt;code&gt;lambda-nat-1&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Be aware that a NAT gateway has associated &lt;a href="https://aws.amazon.com/vpc/pricing/" rel="noopener noreferrer"&gt;costs&lt;/a&gt; — an hourly rate and a cost per GB of data sent through it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Create private subnets
&lt;/h2&gt;

&lt;p&gt;The Lambda function will operate within a private subnet whose route table directs non-local traffic to the NAT gateway we created in the last section. AWS recommends that Lambda functions in a VPC be assigned to at least two subnets in different availability zones for higher availability. &lt;/p&gt;

&lt;p&gt;Next, create two private subnets which direct external traffic to the NAT gateway:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Navigate to the VPC service on the AWS console and choose "Subnets"&lt;/li&gt;
&lt;li&gt;Create two new subnets called &lt;code&gt;lambda-private-subnet-1&lt;/code&gt; and &lt;code&gt;lambda-private-subnet-2&lt;/code&gt;.

&lt;ul&gt;
&lt;li&gt;Choose the same VPC as that of the AWS resources you want to connect to.&lt;/li&gt;
&lt;li&gt;Specify a CIDR block for each subnet, for example, &lt;code&gt;172.31.17.0/24&lt;/code&gt; and &lt;code&gt;172.31.18.0/24&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;After the subnets are created, change their route tables to &lt;code&gt;rt-to-lambda-nat-1&lt;/code&gt;, which we created in the last section.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Configure VPC gateway endpoints
&lt;/h2&gt;

&lt;p&gt;This last step is optional, but recommended to reduce the NAT gateway's data-transfer costs.&lt;br&gt;
The route table assigned to your private subnets should resemble the following:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fs92ck2psvz7qmovyi8za.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fs92ck2psvz7qmovyi8za.png" alt="Private subnet route table"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It directs all external traffic to the NAT gateway, including any traffic the Lambda function sends to S3 or DynamoDB. Since the NAT gateway charges per-GB of data processed, it could be expensive to use these services with our current network configuration. However, we can use &lt;a href="https://docs.aws.amazon.com/vpc/latest/userguide/vpce-gateway.html" rel="noopener noreferrer"&gt;VPC Gateway Endpoints&lt;/a&gt; to bypass the NAT on traffic going to S3 and DynamoDB. There is no charge for data passing through these endpoints, so your AWS bill will be lower.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Navigate to the VPC service on the AWS console and choose “Endpoints".&lt;/li&gt;
&lt;li&gt;Create a new endpoint.&lt;/li&gt;
&lt;li&gt;Select the S3 service endpoint, whose name resembles &lt;code&gt;com.amazonaws.&amp;lt;REGION&amp;gt;.s3&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Associate this endpoint with the route table &lt;code&gt;rt-to-lambda-nat-1&lt;/code&gt; (the private subnets' route table).&lt;/li&gt;
&lt;li&gt;Repeat steps 2–4 for the DynamoDB endpoint.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;After completing these steps, the route table &lt;code&gt;rt-to-lambda-nat-1&lt;/code&gt; should resemble the following:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fnbm8fri6rqnh28k8692v.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fnbm8fri6rqnh28k8692v.png" alt="Route table with gateway endpoints for S3 and DynamoDB"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Configure the Lambda function
&lt;/h2&gt;

&lt;p&gt;Finally, configure the Lambda function to use the private subnets:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Navigate to your Lambda function in the AWS console.&lt;/li&gt;
&lt;li&gt;Under the "Network" pane, choose the VPC that you have been using throughout this guide for the Lambda function.&lt;/li&gt;
&lt;li&gt;Choose the subnets &lt;code&gt;lambda-private-subnet-1&lt;/code&gt; and &lt;code&gt;lambda-private-subnet-2&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;For the security group, depending on your use-case, choose one which allows access to your RDS instance, Redshift cluster or ElastiCache instance. &lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Configuring a Lambda function to access resources in a VPC while still allowing it to talk to the internet is quite involved, as you have seen. But, this guide should provide you with a working solution that can be adapted to your needs. The VPC network that we configured is depicted below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fi8o2p7xcq3h7mmdwk6i7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fi8o2p7xcq3h7mmdwk6i7.png" alt="VPC network diagram for the Lambda function"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In summary, the public subnet &lt;code&gt;lambda-public-subnet-1&lt;/code&gt; directs traffic to an internet gateway through the route table &lt;code&gt;rt-to-igw&lt;/code&gt;. We created a NAT gateway &lt;code&gt;lambda-nat-1&lt;/code&gt; inside the public subnet. The private subnets &lt;code&gt;lambda-private-subnet-1&lt;/code&gt; and &lt;code&gt;lambda-private-subnet-2&lt;/code&gt; direct their external traffic to the NAT gateway through the route table &lt;code&gt;rt-to-nat-lambda-nat-1&lt;/code&gt;. The Lambda function is configured to live within the private subnets.&lt;/p&gt;

&lt;p&gt;Some further resources you may find helpful:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://docs.aws.amazon.com/vpc/latest/userguide/VPC_Networking.html" rel="noopener noreferrer"&gt;VPC networking components docs&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://aws.amazon.com/premiumsupport/knowledge-center/internet-access-lambda-function/" rel="noopener noreferrer"&gt;How can I grant internet access to my VPC Lambda function?&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.verypossible.com/blog/aws-development-dark-art-of-vpc-networking" rel="noopener noreferrer"&gt;The Dark Art of AWS VPC Networking&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>aws</category>
      <category>lambda</category>
      <category>vpc</category>
      <category>nat</category>
    </item>
  </channel>
</rss>
