{"id":12612,"date":"2022-06-06T05:54:54","date_gmt":"2022-06-06T05:54:54","guid":{"rendered":"https:\/\/blog.cloudthat.com\/?p=12612"},"modified":"2024-06-25T10:59:22","modified_gmt":"2024-06-25T10:59:22","slug":"nat-server-amazon-ec2","status":"publish","type":"blog","link":"https:\/\/www.cloudthat.com\/resources\/blog\/nat-server-on-amazon-ec2","title":{"rendered":"NAT Server on Amazon EC2"},"content":{"rendered":"<table border=\"3\">\n<tbody>\n<tr>\n<td>\n<h2><span style=\"color: #000080;\"><strong>TABLE OF CONTENT<\/strong><\/span><\/h2>\n<\/td>\n<\/tr>\n<tr>\n<td><a href=\"#overview\">1. Overview<\/a><\/td>\n<\/tr>\n<tr>\n<td><a href=\"#architecturaldiagram\">2. Architectural Diagram <\/a><\/td>\n<\/tr>\n<tr>\n<td><a href=\"#stepstoimplementnatserver \">3. Steps to Implement NAT Server<\/a><\/td>\n<\/tr>\n<tr>\n<td><a href=\"#whynotpublicsubnet\">4. Why not simply place everything on a public subnet?<\/a><\/td>\n<\/tr>\n<tr>\n<td><a href=\"#conclusion\">5. Conclusion<\/a><\/td>\n<\/tr>\n<tr>\n<td><a href=\"#aboutcloudthat\">6. About CloudThat <\/a><\/td>\n<\/tr>\n<tr>\n<td><a href=\"#faqs\">7. FAQs<\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2 id=\"overview\">Overview<\/h2>\n<p>In modern multi-tier architectures, some applications are required to access the internet to serve users. Still, the backend services such as databases are not required to have internet access, and inbound traffic is not allowed. These backend services are placed in the private subnet, and the internet-facing applications are identified in the public subnet.<\/p>\n<p>NAT server provides the functionality of NAT Gateway using the instance by our own AMI that is running on EC2 in the public subnet so that access to the private subnet is enabled from the internet by restricting the required set of access. NAT instances are more cost-effective than dedicated NAT Gateway.<\/p>\n<p>Today we will see how to provide internet access to the database server that is present in the private subnet using the NAT instance.<\/p>\n<h2 id=\"architecturaldiagram\">Architecture Diagram<\/h2>\n<p><a href=\"https:\/\/d1f7lmxeo98xps.cloudfront.net\/resources\/wp-content\/uploads\/2022\/11\/Nat1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-12613\" src=\"https:\/\/d1f7lmxeo98xps.cloudfront.net\/resources\/wp-content\/uploads\/2022\/11\/Nat1.png\" alt=\"NAT Server Amazon EC2\" width=\"629\" height=\"391\" \/><\/a><\/p>\n<h2 id=\"stepstoimplementnatserver\">Steps to Implement NAT Server<\/h2>\n<p><strong>Step-1 \u2013 <\/strong>Create a custom VPC<\/p>\n<p><a href=\"https:\/\/d1f7lmxeo98xps.cloudfront.net\/resources\/wp-content\/uploads\/2022\/11\/nat2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-12614\" src=\"https:\/\/d1f7lmxeo98xps.cloudfront.net\/resources\/wp-content\/uploads\/2022\/11\/nat2.png\" alt=\"NAT Server Amazon EC2\" width=\"632\" height=\"308\" \/><\/a><\/p>\n<p><strong>Step-2 \u2013 <\/strong>Create Public and Private Subnets<\/p>\n<p><a href=\"https:\/\/d1f7lmxeo98xps.cloudfront.net\/resources\/wp-content\/uploads\/2022\/11\/nat3.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-12615\" src=\"https:\/\/d1f7lmxeo98xps.cloudfront.net\/resources\/wp-content\/uploads\/2022\/11\/nat3.png\" alt=\"NAT Server Amazon EC2\" width=\"631\" height=\"175\" \/><\/a><\/p>\n<p><strong>Step-3 \u2013 <\/strong>Create Public and Private Route Tables<\/p>\n<p><a href=\"https:\/\/d1f7lmxeo98xps.cloudfront.net\/resources\/wp-content\/uploads\/2022\/11\/nat4.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-12616\" src=\"https:\/\/d1f7lmxeo98xps.cloudfront.net\/resources\/wp-content\/uploads\/2022\/11\/nat4.png\" alt=\"NAT Server Amazon EC2\" width=\"631\" height=\"140\" \/><\/a><\/p>\n<p><strong>Step-4 \u2013 <\/strong>Create an Internet Gateway and attach it to the Demo-VPC<\/p>\n<p><a href=\"https:\/\/d1f7lmxeo98xps.cloudfront.net\/resources\/wp-content\/uploads\/2022\/11\/nat5.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-12617\" src=\"https:\/\/d1f7lmxeo98xps.cloudfront.net\/resources\/wp-content\/uploads\/2022\/11\/nat5.png\" alt=\"NAT Server Amazon EC2\" width=\"631\" height=\"187\" \/><\/a><\/p>\n<p><strong>Step-5 \u2013 <\/strong>On the private route, there is no Internet access available<\/p>\n<p><a href=\"https:\/\/d1f7lmxeo98xps.cloudfront.net\/resources\/wp-content\/uploads\/2022\/11\/nat6.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-12618\" src=\"https:\/\/d1f7lmxeo98xps.cloudfront.net\/resources\/wp-content\/uploads\/2022\/11\/nat6.png\" alt=\"NAT Server Amazon EC2\" width=\"631\" height=\"269\" \/><\/a><\/p>\n<p><strong>Step-6 \u2013 <\/strong>Create a DB instance or any private instance that you don\u2019t want any direct internet access<\/p>\n<p><a href=\"https:\/\/d1f7lmxeo98xps.cloudfront.net\/resources\/wp-content\/uploads\/2022\/11\/nat7.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-12619\" src=\"https:\/\/d1f7lmxeo98xps.cloudfront.net\/resources\/wp-content\/uploads\/2022\/11\/nat7.png\" alt=\"NAT Server Amazon EC2\" width=\"631\" height=\"317\" \/><\/a><\/p>\n<p><strong>Step-7 \u2013 <\/strong>Create a NAT server from <strong>Community AMI<\/strong> in AWS<\/p>\n<p><a href=\"https:\/\/d1f7lmxeo98xps.cloudfront.net\/resources\/wp-content\/uploads\/2022\/11\/nat8.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-12620\" src=\"https:\/\/d1f7lmxeo98xps.cloudfront.net\/resources\/wp-content\/uploads\/2022\/11\/nat8.png\" alt=\"NAT Server Amazon EC2\" width=\"631\" height=\"177\" \/><\/a><\/p>\n<p><strong>Step-8 \u2013 <\/strong>Make sure HTTP and HTTPS ports must be open to the Internet<\/p>\n<p><a href=\"https:\/\/d1f7lmxeo98xps.cloudfront.net\/resources\/wp-content\/uploads\/2022\/11\/nat9.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-12621\" src=\"https:\/\/d1f7lmxeo98xps.cloudfront.net\/resources\/wp-content\/uploads\/2022\/11\/nat9.png\" alt=\"NAT Server Amazon EC2\" width=\"631\" height=\"306\" \/><\/a><\/p>\n<p><strong>Step-9 \u2013 <\/strong>Make changes in source and destination check<\/p>\n<p><a href=\"https:\/\/d1f7lmxeo98xps.cloudfront.net\/resources\/wp-content\/uploads\/2022\/11\/nat10.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-12622\" src=\"https:\/\/d1f7lmxeo98xps.cloudfront.net\/resources\/wp-content\/uploads\/2022\/11\/nat10.png\" alt=\"NAT Server Amazon EC2\" width=\"631\" height=\"196\" \/><\/a><\/p>\n<p><strong>Step-10 \u2013 <\/strong>Click on the checkmark to stop the source and destination check<\/p>\n<p><a href=\"https:\/\/d1f7lmxeo98xps.cloudfront.net\/resources\/wp-content\/uploads\/2022\/11\/nat11.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-12623\" src=\"https:\/\/d1f7lmxeo98xps.cloudfront.net\/resources\/wp-content\/uploads\/2022\/11\/nat11.png\" alt=\"NAT Server Amazon EC2\" width=\"631\" height=\"337\" \/><\/a><\/p>\n<p><strong>Step-11 \u2013 <\/strong>Now add the instance id in the private route table to access the internet from the NAT server<\/p>\n<p><a href=\"https:\/\/d1f7lmxeo98xps.cloudfront.net\/resources\/wp-content\/uploads\/2022\/11\/nat12.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-12624\" src=\"https:\/\/d1f7lmxeo98xps.cloudfront.net\/resources\/wp-content\/uploads\/2022\/11\/nat12.png\" alt=\"NAT Server Amazon EC2\" width=\"631\" height=\"209\" \/><\/a><\/p>\n<p><strong>Step-12 \u2013 <\/strong>Now SSH into the private instance using Bastion Host and try to ping google.com<\/p>\n<p><a href=\"https:\/\/d1f7lmxeo98xps.cloudfront.net\/resources\/wp-content\/uploads\/2022\/11\/nat13.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-12626\" src=\"https:\/\/d1f7lmxeo98xps.cloudfront.net\/resources\/wp-content\/uploads\/2022\/11\/nat13.png\" alt=\"NAT Server Amazon EC2\" width=\"631\" height=\"271\" \/><\/a><\/p>\n<h2 id=\"whynotpublicsubnet\">Why not simply place everything on a public subnet?<\/h2>\n<p>We hear frequently and have considered this, particularly in non-critical contexts. It would fix the problem, and we would no longer require a NAT instance or NAT Gateway because each instance or service would have its own public IP address. We have decided against it for two key reasons.<\/p>\n<ul>\n<li><span style=\"text-decoration: underline;\"><strong>Reason1:<\/strong><\/span> Security should be tiered. We don&#8217;t want a single setup error to result in the resources becoming publicly exposed overnight. The ideal Security Group and NACL would effectively do the same thing, but it&#8217;s important to keep those resources separated from the internet regardless of what&#8217;s put up in the SG or NACL.<br \/>\nFurthermore, because our acceptance settings frequently include almost identical data as production environments, we are hesitant to accept a lesser degree of security.<\/li>\n<li><span style=\"text-decoration: underline;\"><strong>Reason 2:<\/strong> <\/span>We use Terraform to create our environments as code. Rather than migrating all resources to public subnets, it is significantly quicker and more consistent to just change out a NAT Gateway for a NAT instance.<\/li>\n<\/ul>\n<h2 id=\"conclusion\">Conclusion<\/h2>\n<p>We have seen the utility of NAT Gateways, it offers outbound traffic for private subnet machines\/instances, which is required for downloading packages, installing patches, and software updates, among other things.<\/p>\n<p>Another advantage of employing NAT Gateway is that it is very simple to add or remove it, i.e., by removing\/adding a route to NAT Gateway from the routing table that is depending on our needs.<\/p>\n<h2 id=\"aboutcloudthat\">About CloudThat<\/h2>\n<p><a href=\"https:\/\/www.cloudthat.com\/\"><strong>CloudThat\u00a0<\/strong><\/a>is also the official AWS (Amazon Web Services) Advanced Consulting Partner and Training partner and Microsoft gold partner, helping people develop knowledge of the cloud and help their businesses aim for higher goals using best in industry cloud computing practices and expertise. We are on a mission to build\u00a0a robust\u00a0cloud computing ecosystem by disseminating\u00a0knowledge on technological intricacies within the cloud space.\u00a0Our blogs, webinars,\u00a0case studies, and white papers\u00a0enable all the stakeholders in the cloud computing sphere.<\/p>\n<p>Drop a query if you have any questions regarding the NAT server, NAT instance, or Amazon services, and I will get back to you quickly. To get started, go through\u00a0our<strong>\u00a0<\/strong><a href=\"https:\/\/www.cloudthat.com\/expert-advisory\/?utm_source=blog-website&amp;utm-medium=text-link&amp;utm_campaign=expert-advisory\"><strong>Expert Advisory<\/strong>\u00a0<\/a>page\u00a0and\u00a0<a href=\"https:\/\/www.cloudthat.com\/managed-services-packages\/\"><strong>Managed Services Package<\/strong><\/a>\u00a0that is<strong>\u00a0<a href=\"https:\/\/cloudthat.com\/?utm_source=blog-website&amp;utm-medium=text-link&amp;utm_campaign=cloudthat.com\/\">CloudThat<\/a><\/strong>\u2019s\u00a0offerings.<\/p>\n<h2 id=\"faqs\">FAQs<\/h2>\n<p><span style=\"text-decoration: underline;\"><strong>Q1. Is it possible to move the NAT instance to the NAT Gateway?<\/strong><\/span><\/p>\n<p>Ans: You may construct a NAT gateway in the same subnet as your NAT instance and then replace the current route in your route table that refers to the NAT instance with the NAT gateway route.<\/p>\n<p><span style=\"text-decoration: underline;\"><strong>Q2. What is the difference between a NAT gateway and a NAT instance?<\/strong><\/span><\/p>\n<p>Ans: When a connection times out, a NAT gateway sends an RST packet to any resources trying to maintain the connection behind the NAT gateway (it does not send a FIN packet). When a connection times out, a NAT instance closes the connection by sending a FIN packet to resources behind the NAT instance.<\/p>\n<p><span style=\"text-decoration: underline;\"><strong>Q3. Which is more cost-effective: a NAT gateway or a NAT instance?<\/strong><\/span><\/p>\n<p>Ans: Since AWS charges cross-AZ traffic within your VPC, you will generally want one for each availability zone. However, the cost of the NAT Instances will be cheaper than the cost of a NAT Gateway if you are pushing enough traffic.<\/p>\n","protected":false},"author":248,"featured_media":12635,"parent":0,"comment_status":"open","ping_status":"open","template":"","blog_category":[3606,3607],"user_email":"bhanuprakashr@cloudthat.com","published_by":"324","primary-authors":"","secondary-authors":"","acf":[],"_links":{"self":[{"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/blog\/12612"}],"collection":[{"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/blog"}],"about":[{"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/types\/blog"}],"author":[{"embeddable":true,"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/users\/248"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/comments?post=12612"}],"version-history":[{"count":1,"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/blog\/12612\/revisions"}],"predecessor-version":[{"id":42708,"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/blog\/12612\/revisions\/42708"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/media?parent=12612"}],"wp:term":[{"taxonomy":"blog_category","embeddable":true,"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/blog_category?post=12612"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}