<?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: Ved Dandotia</title>
    <description>The latest articles on DEV Community by Ved Dandotia (@techxved).</description>
    <link>https://dev.to/techxved</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.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3579885%2F3644f449-cdbb-4df0-bfea-948bfe534ef6.jpg</url>
      <title>DEV Community: Ved Dandotia</title>
      <link>https://dev.to/techxved</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/techxved"/>
    <language>en</language>
    <item>
      <title>Building a Highly Available Web Tier on AWS — Application Load Balancer + Auto Scaling Group</title>
      <dc:creator>Ved Dandotia</dc:creator>
      <pubDate>Tue, 28 Jul 2026 06:16:55 +0000</pubDate>
      <link>https://dev.to/techxved/building-a-highly-available-web-tier-on-aws-application-load-balancer-auto-scaling-group-4kjj</link>
      <guid>https://dev.to/techxved/building-a-highly-available-web-tier-on-aws-application-load-balancer-auto-scaling-group-4kjj</guid>
      <description>&lt;h1&gt;
  
  
  ⚖️ Application Load Balancer + Auto Scaling Group on AWS
&lt;/h1&gt;

&lt;p&gt;So far in this series, we've built a custom VPC with public/private subnets and added a NAT Gateway for outbound access. Now it's time to put that network to real use: deploying a &lt;strong&gt;highly available, self-healing web tier&lt;/strong&gt; using an &lt;strong&gt;Application Load Balancer (ALB)&lt;/strong&gt; and an &lt;strong&gt;Auto Scaling Group (ASG)&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;By the end of this post, you'll have multiple EC2 instances running in private subnets, sitting behind a public-facing load balancer, automatically distributing traffic — and automatically replacing any instance that fails.&lt;/p&gt;




&lt;h2&gt;
  
  
  🧠 The Core Pattern
&lt;/h2&gt;

&lt;p&gt;This is one of the most important architecture patterns in AWS:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Internet → ALB (public subnet) → Target Group → EC2 Instances (private subnet)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;The &lt;strong&gt;ALB&lt;/strong&gt; is the only internet-facing component — it lives in the &lt;strong&gt;public subnet&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;EC2 instances&lt;/strong&gt; never touch the internet directly — they live in the &lt;strong&gt;private subnet&lt;/strong&gt;, reachable only through the ALB.&lt;/li&gt;
&lt;li&gt;This is a defense-in-depth principle: never expose more than you have to. Even if someone discovers an instance's private IP, there's no route in from outside the VPC.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Traffic flow, step by step:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;A user hits the ALB's DNS name&lt;/li&gt;
&lt;li&gt;The ALB (in the public subnet, reachable via the Internet Gateway) receives the request&lt;/li&gt;
&lt;li&gt;The ALB forwards it internally — over the VPC's private network — to a healthy instance in the private subnet, via the Target Group&lt;/li&gt;
&lt;li&gt;The instance responds back through the same private path&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  🖼️ Architecture
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fnic10wr365f7oesa1yjz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fnic10wr365f7oesa1yjz.png" alt=" " width="798" height="214"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                         Internet
                             │
                             ▼
                    ┌──────────────────┐
                    │   demo-alb        │  (Public Subnets)
                    │ (alb-sg: 80 open) │
                    └─────────┬─────────┘
                              │
                              ▼
                    ┌──────────────────┐
                    │   demo-tg          │
                    │  (Target Group)    │
                    └─────────┬─────────┘
                              │
              ┌───────────────┴────────────────┐
              ▼                                ▼
     EC2 Instance (private)           EC2 Instance (private)
     instance-sg: 80 from alb-sg      instance-sg: 80 from alb-sg
     (Auto Scaling Group: demo-asg, desired 2, min 2, max 4)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  🧩 A Key Design Choice: No Nginx, No Internet Needed
&lt;/h2&gt;

&lt;p&gt;Our private subnets have &lt;strong&gt;no NAT Gateway&lt;/strong&gt;, meaning the instances have no outbound internet access. A typical &lt;code&gt;yum install nginx&lt;/code&gt; in User Data would fail here, since &lt;code&gt;nginx&lt;/code&gt; isn't preinstalled and package repos are external.&lt;/p&gt;

&lt;p&gt;Instead, we'll use &lt;strong&gt;Python's built-in HTTP server&lt;/strong&gt;, which ships with Amazon Linux 2023 out of the box — no internet required. This keeps the whole demo self-contained.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;💡 If your use case later needs real outbound access (installing packages, calling external APIs), add a NAT Gateway in the public subnet and route the private subnet's &lt;code&gt;0.0.0.0/0&lt;/code&gt; traffic through it — see my previous NAT Gateway blog here for the full walkthrough.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  ✅ Prerequisites
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;A custom VPC with &lt;strong&gt;two public subnets&lt;/strong&gt; and &lt;strong&gt;two private subnets&lt;/strong&gt;, each pair spread across two Availability Zones (ALB requires subnets in 2+ AZs)&lt;/li&gt;
&lt;li&gt;An Internet Gateway attached to the VPC&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  The Build Order
&lt;/h2&gt;

&lt;p&gt;We'll go: &lt;strong&gt;Security Groups → Target Group → Launch Template → ALB → Auto Scaling Group&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 1: Create Two Security Groups
&lt;/h2&gt;

&lt;h3&gt;
  
  
  A) ALB Security Group
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;EC2 Console → Security Groups → Create Security Group&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Name: &lt;code&gt;alb-sg&lt;/code&gt;, Description: "ALB security group", VPC: your demo VPC&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Inbound rules:&lt;/strong&gt; Add rule → Type: HTTP, Port 80, Source: Anywhere-IPv4 (&lt;code&gt;0.0.0.0/0&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Outbound rules:&lt;/strong&gt; leave default (all traffic allowed)&lt;/li&gt;
&lt;li&gt;Create&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  B) Instance Security Group
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Create another security group&lt;/li&gt;
&lt;li&gt;Name: &lt;code&gt;instance-sg&lt;/code&gt;, Description: "EC2 instance SG", same VPC&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Inbound rules:&lt;/strong&gt; Add rule → Type: HTTP, Port 80, &lt;strong&gt;Source: Custom → select &lt;code&gt;alb-sg&lt;/code&gt;&lt;/strong&gt; (not a CIDR range — this restricts traffic to only what's coming from the ALB)&lt;/li&gt;
&lt;li&gt;Create&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This is the crucial detail: instances only accept HTTP traffic that's coming from the load balancer, never directly from the internet.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 2: Create the Target Group
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;EC2 Console → Target Groups → Create Target Group&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Target type: &lt;strong&gt;Instances&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Name: &lt;code&gt;demo-tg&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Protocol: HTTP, Port: 80&lt;/li&gt;
&lt;li&gt;VPC: your demo VPC&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Health checks:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Protocol: HTTP, Path: &lt;code&gt;/&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Advanced settings: Healthy threshold &lt;code&gt;2&lt;/code&gt;, Unhealthy threshold &lt;code&gt;2&lt;/code&gt;, Timeout &lt;code&gt;5&lt;/code&gt;, Interval &lt;code&gt;10&lt;/code&gt;, Success codes &lt;code&gt;200&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Next → skip "Register targets" (the ASG will handle this automatically) → &lt;strong&gt;Create target group&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  Step 3: Create the Launch Template
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;EC2 Console → Launch Templates → Create Launch Template&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Name: &lt;code&gt;demo-lt&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;AMI: &lt;strong&gt;Amazon Linux 2023&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Instance type: &lt;code&gt;t3.micro&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Key pair: optional — only needed if you want SSH access for debugging&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Network settings:&lt;/strong&gt; leave subnet blank (the ASG assigns subnets); Security groups: select &lt;code&gt;instance-sg&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Advanced details → User data:&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/bin/bash&lt;/span&gt;
&lt;span class="nb"&gt;mkdir&lt;/span&gt; &lt;span class="nt"&gt;-p&lt;/span&gt; /var/www/demo
&lt;span class="nb"&gt;cat&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;EOF&lt;/span&gt;&lt;span class="sh"&gt; &amp;gt; /var/www/demo/index.html
&amp;lt;html&amp;gt;&amp;lt;body&amp;gt;&amp;lt;h1&amp;gt;Hello from &lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;hostname&lt;/span&gt; &lt;span class="nt"&gt;-f&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;&lt;span class="sh"&gt;&amp;lt;/h1&amp;gt;&amp;lt;/body&amp;gt;&amp;lt;/html&amp;gt;
&lt;/span&gt;&lt;span class="no"&gt;EOF
&lt;/span&gt;&lt;span class="nb"&gt;cd&lt;/span&gt; /var/www/demo
&lt;span class="nb"&gt;nohup &lt;/span&gt;python3 &lt;span class="nt"&gt;-m&lt;/span&gt; http.server 80 &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; /var/log/demo-server.log 2&amp;gt;&amp;amp;1 &amp;amp;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This serves a page showing the instance's hostname — so when you refresh the ALB's URL repeatedly, you can literally watch it load-balance across different instances.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create the template&lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Auto Scaling guidance checkbox:&lt;/strong&gt; In the wizard, check "Provide guidance to help me set up a template for use with EC2 Auto Scaling." It doesn't restrict anything functionally — it just hides/discourages settings that don't make sense for ASG-launched instances (like picking a specific subnet or attaching a public IP), and nudges you toward leaving those to the ASG. Since our instances live in private subnets with no public IP needed, this is exactly the right guidance.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Step 4: Create the Application Load Balancer
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;EC2 Console → Load Balancers → Create Load Balancer → Application Load Balancer&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Name: &lt;code&gt;demo-alb&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Scheme: &lt;strong&gt;Internet-facing&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;IP address type: IPv4&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Network mapping:&lt;/strong&gt; select your VPC → check &lt;strong&gt;both public subnets&lt;/strong&gt; (one per AZ — required, since the ALB needs subnets in 2+ Availability Zones)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Security groups:&lt;/strong&gt; select &lt;code&gt;alb-sg&lt;/code&gt; (remove any default SG that gets auto-added)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Listeners and routing:&lt;/strong&gt; Listener HTTP:80 → Default action: Forward to → select &lt;code&gt;demo-tg&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Create&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Wait a minute or two for the state to become &lt;strong&gt;Active&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 5: Create the Auto Scaling Group
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;EC2 Console → Auto Scaling Groups → Create Auto Scaling Group&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Name: &lt;code&gt;demo-asg&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Launch template: select &lt;code&gt;demo-lt&lt;/code&gt; → Next&lt;/li&gt;
&lt;li&gt;VPC: your demo VPC → &lt;strong&gt;Availability Zones and subnets:&lt;/strong&gt; select &lt;strong&gt;both private subnets&lt;/strong&gt; (one per AZ)&lt;/li&gt;
&lt;li&gt;Next → &lt;strong&gt;Attach to an existing load balancer → Choose from your load balancer target groups → select &lt;code&gt;demo-tg&lt;/code&gt;&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Health checks:&lt;/strong&gt; turn &lt;strong&gt;ON&lt;/strong&gt; "Turn on Elastic Load Balancing health checks" — without this, the ASG only checks raw EC2 status, not whether your app is actually responding&lt;/li&gt;
&lt;li&gt;Health check grace period: &lt;code&gt;60&lt;/code&gt; seconds (gives the instance time to boot before checks start counting against it)&lt;/li&gt;
&lt;li&gt;Next → &lt;strong&gt;Group size:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Desired capacity: &lt;code&gt;2&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Minimum: &lt;code&gt;2&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Maximum: &lt;code&gt;4&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;(Optional) Add a scaling policy like "Target tracking on CPU 50%," or skip for now&lt;/li&gt;
&lt;li&gt;Review → &lt;strong&gt;Create Auto Scaling group&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  Step 6: Verify
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Wait ~2–3 minutes for instances to launch and pass health checks&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;EC2 Console → Target Groups → &lt;code&gt;demo-tg&lt;/code&gt; → Targets tab&lt;/strong&gt; — both instances should show &lt;strong&gt;healthy&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;EC2 Console → Load Balancers → &lt;code&gt;demo-alb&lt;/code&gt;&lt;/strong&gt; — copy the &lt;strong&gt;DNS name&lt;/strong&gt; (something like &lt;code&gt;demo-alb-123456789.us-east-1.elb.amazonaws.com&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Paste that DNS name into your browser and refresh a few times — you should see the hostname in the page change between instances as the ALB load-balances requests&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fancdwv6wh8k2ws691ktc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fancdwv6wh8k2ws691ktc.png" alt=" " width="800" height="128"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F66vv6i4e4x4ra96mpvs7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F66vv6i4e4x4ra96mpvs7.png" alt=" " width="800" height="172"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  🔍 Quick Recap
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Resource&lt;/th&gt;
&lt;th&gt;Purpose&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;alb-sg&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Allows HTTP from the internet, attached to the ALB&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;instance-sg&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Allows HTTP &lt;strong&gt;only from &lt;code&gt;alb-sg&lt;/code&gt;&lt;/strong&gt;, attached to instances&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;demo-tg&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Target group routing traffic to healthy instances&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;demo-lt&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Launch template defining how each instance boots (AMI, SG, User Data)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;demo-alb&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Internet-facing load balancer in public subnets&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;demo-asg&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Auto Scaling Group maintaining 2–4 instances in private subnets&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  🧹 Cleanup
&lt;/h2&gt;

&lt;p&gt;To avoid ongoing charges:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Delete the Auto Scaling Group (this terminates its instances too)&lt;/li&gt;
&lt;li&gt;Delete the Load Balancer&lt;/li&gt;
&lt;li&gt;Delete the Target Group&lt;/li&gt;
&lt;li&gt;Delete the Launch Template&lt;/li&gt;
&lt;li&gt;Delete the two security groups (&lt;code&gt;alb-sg&lt;/code&gt;, &lt;code&gt;instance-sg&lt;/code&gt;)&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  🏁 Wrap-Up
&lt;/h2&gt;

&lt;p&gt;You've now built a genuinely production-style pattern: a public-facing load balancer distributing traffic across multiple private, auto-healing instances — with the instances never directly exposed to the internet. This "one door in" architecture is the backbone of most real-world AWS web applications.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Full project files are available on GitHub.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>cloud</category>
      <category>aws</category>
      <category>ec2</category>
      <category>loadbalancer</category>
    </item>
    <item>
      <title>Hosting a Static Website on AWS S3 — Step-by-Step Guide</title>
      <dc:creator>Ved Dandotia</dc:creator>
      <pubDate>Sun, 26 Jul 2026 10:50:53 +0000</pubDate>
      <link>https://dev.to/techxved/hosting-a-static-website-on-aws-s3-step-by-step-guide-4ca8</link>
      <guid>https://dev.to/techxved/hosting-a-static-website-on-aws-s3-step-by-step-guide-4ca8</guid>
      <description>&lt;h2&gt;
  
  
  🪣 Hosting a Static Website on AWS S3
&lt;/h2&gt;

&lt;p&gt;Not every website needs a server. If your site is just HTML, CSS, and JavaScript — no backend, no database — &lt;strong&gt;Amazon S3&lt;/strong&gt; can host it directly, cheaply, and reliably. No EC2 instance, no server management, no patching.&lt;/p&gt;

&lt;p&gt;In this post, we'll take a simple 3-file website (&lt;code&gt;index.html&lt;/code&gt;, &lt;code&gt;error.html&lt;/code&gt;, &lt;code&gt;script.js&lt;/code&gt;) and host it on S3 as a public static website.&lt;/p&gt;




&lt;h2&gt;
  
  
  🧠 Why S3 for Static Hosting?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;No servers to manage or patch&lt;/li&gt;
&lt;li&gt;Extremely low cost (pay only for storage + requests)&lt;/li&gt;
&lt;li&gt;Scales automatically — handles a handful of visitors or a viral spike the same way&lt;/li&gt;
&lt;li&gt;Great for portfolios, landing pages, documentation sites, or single-page apps&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🖼️ Architecture
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Flow:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Browser Request → S3 Bucket (Static Website Hosting enabled)
                         │
                         ├── index.html   (default page)
                         ├── error.html   (shown on 404 / errors)
                         └── script.js    (site logic)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  ✅ Prerequisites
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;An AWS account&lt;/li&gt;
&lt;li&gt;Three simple files ready to upload:

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;index.html&lt;/code&gt; — your homepage&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;error.html&lt;/code&gt; — shown when a page/object isn't found&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;script.js&lt;/code&gt; — any JavaScript your site uses&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example minimal files, if you don't have your own yet:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;index.html&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&lt;/span&gt;My S3 Website&lt;span class="nt"&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"script.js"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;Hello from my S3 Static Website!&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;code&gt;error.html&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&lt;/span&gt;Page Not Found&lt;span class="nt"&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;Oops! That page doesn't exist.&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;code&gt;script.js&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Site loaded successfully from S3!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Step 1: Create the S3 Bucket
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Go to &lt;strong&gt;S3 Console → Create Bucket&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Bucket name:&lt;/strong&gt; must be globally unique, e.g. &lt;code&gt;my-static-site-demo-2026&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Region:&lt;/strong&gt; choose your preferred region (e.g., &lt;code&gt;ap-south-1&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Block Public Access settings:&lt;/strong&gt; &lt;strong&gt;Uncheck&lt;/strong&gt; "Block all public access" — a static website needs to be publicly readable
&amp;gt; ⚠️ Acknowledge the warning checkbox that appears. This is expected and required for public static hosting.&lt;/li&gt;
&lt;li&gt;Leave other settings as default → &lt;strong&gt;Create Bucket&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  Step 2: Upload Your Website Files
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Open your new bucket → &lt;strong&gt;Upload&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Add your three files: &lt;code&gt;index.html&lt;/code&gt;, &lt;code&gt;error.html&lt;/code&gt;, &lt;code&gt;style.css&lt;/code&gt;_ &lt;code&gt;script.js&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Click &lt;strong&gt;Upload&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F7nmcn05cd2tfi470wka4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F7nmcn05cd2tfi470wka4.png" alt="Real S3 Bucket viwe, Ignore git file here not needed for the deployment" width="799" height="256"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 3: Enable Static Website Hosting
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;In your bucket, go to &lt;strong&gt;Properties&lt;/strong&gt; tab&lt;/li&gt;
&lt;li&gt;Scroll to &lt;strong&gt;Static website hosting → Edit&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Settings:

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Static website hosting:&lt;/strong&gt; Enable&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Hosting type:&lt;/strong&gt; Host a static website&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Index document:&lt;/strong&gt; &lt;code&gt;index.html&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Error document:&lt;/strong&gt; &lt;code&gt;error.html&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Save changes&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;AWS will now show you a &lt;strong&gt;Bucket website endpoint&lt;/strong&gt; URL (something like &lt;code&gt;http://my-static-site-demo-2026.s3-website.ap-south-1.amazonaws.com&lt;/code&gt;) — keep this handy, you'll need it in Step 5.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fdoaiof17e2lsqbxsf4m8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fdoaiof17e2lsqbxsf4m8.png" alt=" " width="800" height="202"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 4: Add a Bucket Policy for Public Read Access
&lt;/h2&gt;

&lt;p&gt;Even with "Block Public Access" disabled, S3 objects aren't public until a policy explicitly allows it.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Go to &lt;strong&gt;Permissions&lt;/strong&gt; tab → &lt;strong&gt;Bucket Policy → Edit&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Paste the following (replace &lt;code&gt;YOUR-BUCKET-NAME&lt;/code&gt; with your actual bucket name):
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"Version"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"2012-10-17"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"Statement"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"Sid"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"PublicReadGetObject"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"Effect"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Allow"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"Principal"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"*"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"Action"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"s3:GetObject"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"Resource"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"arn:aws:s3:::YOUR-BUCKET-NAME/*"&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Save changes&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This policy allows anyone to &lt;strong&gt;read (GET)&lt;/strong&gt; objects in the bucket — exactly what's needed for a public website, and nothing more (no write/delete access).&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 5: Test Your Website
&lt;/h2&gt;

&lt;p&gt;Open the &lt;strong&gt;Bucket website endpoint&lt;/strong&gt; URL from Step 3 in your browser:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;http://YOUR-BUCKET-NAME.s3-website.&amp;lt;region&amp;gt;.amazonaws.com
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You should see your &lt;code&gt;index.html&lt;/code&gt; page render, and if you open the browser console, you should see the message from &lt;code&gt;script.js&lt;/code&gt; logged.&lt;/p&gt;

&lt;p&gt;Try visiting a non-existent path (e.g., add &lt;code&gt;/random-page&lt;/code&gt; to the URL) — you should see your custom &lt;code&gt;error.html&lt;/code&gt; page instead of a generic AWS error.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fvzxdt7w4ngzodim9zlvs.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fvzxdt7w4ngzodim9zlvs.png" alt=" " width="800" height="403"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  🔍 Quick Recap
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Step&lt;/th&gt;
&lt;th&gt;What Happened&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Created bucket&lt;/td&gt;
&lt;td&gt;Storage container for website files&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Uploaded files&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;index.html&lt;/code&gt;, &lt;code&gt;error.html&lt;/code&gt;, &lt;code&gt;script.js&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Enabled static hosting&lt;/td&gt;
&lt;td&gt;Told S3 which file is the homepage &amp;amp; error page&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Added bucket policy&lt;/td&gt;
&lt;td&gt;Made objects publicly readable&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Tested endpoint&lt;/td&gt;
&lt;td&gt;Confirmed the site loads and errors are handled&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  🔐 A Note on Security
&lt;/h2&gt;

&lt;p&gt;Public bucket policies only grant &lt;strong&gt;read&lt;/strong&gt; access (&lt;code&gt;s3:GetObject&lt;/code&gt;) — visitors can view your site, but can't upload, modify, or delete anything. Still, double check:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Only the specific files/paths needed are public&lt;/li&gt;
&lt;li&gt;Sensitive files are never placed in a publicly hosted bucket&lt;/li&gt;
&lt;li&gt;Consider adding &lt;strong&gt;CloudFront&lt;/strong&gt; in front of the bucket for HTTPS support (S3 static website endpoints don't support HTTPS natively) — a good topic for a follow-up post!&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🧹 Cleanup
&lt;/h2&gt;

&lt;p&gt;To avoid any storage charges:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Empty the bucket (&lt;strong&gt;Bucket → Empty&lt;/strong&gt;)&lt;/li&gt;
&lt;li&gt;Delete the bucket (&lt;strong&gt;Bucket → Delete&lt;/strong&gt;)&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  🏁 Wrap-Up
&lt;/h2&gt;

&lt;p&gt;You've now hosted a fully working static website on S3 — no servers, no scaling concerns, just fast and cheap. This same pattern powers countless portfolios, docs sites, and landing pages in production today.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Full project files are available on &lt;a href="https://github.com/TechxVed" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>aws</category>
      <category>s3</category>
      <category>cloud</category>
      <category>webdev</category>
    </item>
    <item>
      <title>🔀 Adding a NAT Gateway to Your AWS VPC — Giving Private Subnets Internet Access (Safely)</title>
      <dc:creator>Ved Dandotia</dc:creator>
      <pubDate>Sat, 25 Jul 2026 05:43:42 +0000</pubDate>
      <link>https://dev.to/techxved/adding-a-nat-gateway-to-your-aws-vpc-giving-private-subnets-internet-access-safely-4e34</link>
      <guid>https://dev.to/techxved/adding-a-nat-gateway-to-your-aws-vpc-giving-private-subnets-internet-access-safely-4e34</guid>
      <description>&lt;p&gt;In my previous tutorial, we built a &lt;strong&gt;custom VPC&lt;/strong&gt; with &lt;strong&gt;public and private subnets&lt;/strong&gt;, an &lt;strong&gt;Internet Gateway&lt;/strong&gt;, and an &lt;strong&gt;EC2 instance&lt;/strong&gt; running in the public subnet.&lt;/p&gt;

&lt;p&gt;While the private subnet was secure, it had one major limitation—it couldn't access the internet at all.&lt;/p&gt;

&lt;p&gt;In real-world AWS environments, private resources such as application servers and databases often need &lt;strong&gt;outbound internet access&lt;/strong&gt; to install updates, download packages, or communicate with external APIs, while remaining inaccessible from the public internet.&lt;/p&gt;

&lt;p&gt;This is exactly where a &lt;strong&gt;NAT Gateway&lt;/strong&gt; comes in.&lt;/p&gt;

&lt;p&gt;By the end of this guide, you'll configure a NAT Gateway that allows instances in a private subnet to securely access the internet without exposing them to inbound traffic.&lt;/p&gt;




&lt;h2&gt;
  
  
  🚀 What You'll Build
&lt;/h2&gt;

&lt;p&gt;In this tutorial, we'll create:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;✅ An Elastic IP&lt;/li&gt;
&lt;li&gt;✅ A NAT Gateway&lt;/li&gt;
&lt;li&gt;✅ Updated Private Route Table&lt;/li&gt;
&lt;li&gt;✅ A Private EC2 Instance&lt;/li&gt;
&lt;li&gt;✅ Secure outbound internet connectivity for the private subnet&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🧠 What is a NAT Gateway?
&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;Network Address Translation (NAT) Gateway&lt;/strong&gt; enables instances in a &lt;strong&gt;private subnet&lt;/strong&gt; to initiate outbound connections to the internet while preventing inbound connections initiated from the internet.&lt;/p&gt;

&lt;p&gt;Think of it like a &lt;strong&gt;one-way door&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;✅ Private Subnet → Internet&lt;/li&gt;
&lt;li&gt;❌ Internet → Private Subnet&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This allows servers to download software updates, install packages, or communicate with external services without exposing them to public access.&lt;/p&gt;




&lt;h2&gt;
  
  
  🏗️ Architecture
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F8r33c29nqo0hurd9v43p.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F8r33c29nqo0hurd9v43p.png" alt=" " width="714" height="647"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  📋 Prerequisites
&lt;/h2&gt;

&lt;p&gt;Before continuing, ensure you already have:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A custom VPC (&lt;code&gt;demo-vpc&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;A Public Subnet (&lt;code&gt;demo-public-subnet-1a&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;A Private Subnet (&lt;code&gt;demo-private-subnet-1a&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;An Internet Gateway attached to the VPC&lt;/li&gt;
&lt;li&gt;A Route Table for each subnet&lt;/li&gt;
&lt;li&gt;An EC2 instance running in the public subnet&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you haven't completed those steps, check out my previous article on building a custom VPC.&lt;/p&gt;

&lt;p&gt;here:- [&lt;a href="https://dev.to/techxved/building-a-custom-vpc-on-aws-public-private-subnets-route-tables-and-an-ec2-instance-4eh1"&gt;https://dev.to/techxved/building-a-custom-vpc-on-aws-public-private-subnets-route-tables-and-an-ec2-instance-4eh1&lt;/a&gt;]&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 1 — Allocate an Elastic IP
&lt;/h2&gt;

&lt;p&gt;A NAT Gateway requires a &lt;strong&gt;static public IP address&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Navigate to:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;VPC → Elastic IPs → Allocate Elastic IP Address&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Leave the default settings and click &lt;strong&gt;Allocate&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Give it a name such as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;demo-natgw-eip
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Step 2 — Create the NAT Gateway
&lt;/h2&gt;

&lt;p&gt;Navigate to:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;VPC → NAT Gateways → Create NAT Gateway&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Use the following configuration.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Setting&lt;/th&gt;
&lt;th&gt;Value&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Name&lt;/td&gt;
&lt;td&gt;demo-NATGW&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Subnet&lt;/td&gt;
&lt;td&gt;demo-public-subnet-1a&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Connectivity Type&lt;/td&gt;
&lt;td&gt;Public&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Elastic IP&lt;/td&gt;
&lt;td&gt;demo-natgw-eip&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Click &lt;strong&gt;Create NAT Gateway&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;It usually takes a couple of minutes before its status changes to &lt;strong&gt;Available&lt;/strong&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;⚠️ &lt;strong&gt;Important&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A NAT Gateway &lt;strong&gt;must always be deployed in a public subnet&lt;/strong&gt; so it can communicate with the Internet Gateway.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Step 3 — Update the Private Route Table
&lt;/h2&gt;

&lt;p&gt;Now we'll allow the private subnet to send outbound traffic through the NAT Gateway.&lt;/p&gt;

&lt;p&gt;Navigate to:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;VPC → Route Tables → demo-private-RT&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Edit the routes and configure:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Destination&lt;/th&gt;
&lt;th&gt;Target&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;10.0.0.0/16&lt;/td&gt;
&lt;td&gt;local&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;0.0.0.0/0&lt;/td&gt;
&lt;td&gt;demo-NATGW&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Save the changes.&lt;/p&gt;

&lt;p&gt;Your private subnet can now initiate outbound internet connections while remaining inaccessible from the internet.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 4 — Launch an EC2 Instance in the Private Subnet
&lt;/h2&gt;

&lt;p&gt;Launch another EC2 instance using the following configuration.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Setting&lt;/th&gt;
&lt;th&gt;Value&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;AMI&lt;/td&gt;
&lt;td&gt;Amazon Linux 2&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Instance Type&lt;/td&gt;
&lt;td&gt;t3.micro&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;VPC&lt;/td&gt;
&lt;td&gt;demo-vpc&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Subnet&lt;/td&gt;
&lt;td&gt;demo-private-subnet-1a&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Auto Assign Public IP&lt;/td&gt;
&lt;td&gt;Disabled&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;For the Security Group:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Allow SSH (22) &lt;strong&gt;only from the public EC2 instance or Bastion Host&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Do &lt;strong&gt;not&lt;/strong&gt; allow SSH from &lt;code&gt;0.0.0.0/0&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Launch the instance.&lt;/p&gt;

&lt;p&gt;Notice that the instance has &lt;strong&gt;no Public IPv4 address&lt;/strong&gt;, which means it cannot be accessed directly from the internet.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 5 — Verify NAT Gateway Connectivity
&lt;/h2&gt;

&lt;p&gt;Since the private instance doesn't have a public IP, we'll first connect to the public EC2 instance and then access the private instance from there.&lt;/p&gt;

&lt;h2&gt;
  
  
  5.1 Copy the Key Pair
&lt;/h2&gt;

&lt;p&gt;From your local machine:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;scp &lt;span class="nt"&gt;-i&lt;/span&gt; demo_ec2.pem demo_ec2.pem ec2-user@&amp;lt;public-instance-ip&amp;gt;:~/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;💡 &lt;strong&gt;Tip&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In production environments, &lt;strong&gt;SSH Agent Forwarding&lt;/strong&gt; is preferred instead of copying private keys.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  5.2 Connect to the Public EC2
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;ssh &lt;span class="nt"&gt;-i&lt;/span&gt; demo_ec2.pem ec2-user@&amp;lt;public-instance-ip&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  5.3 Connect to the Private EC2
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;chmod &lt;/span&gt;400 demo_ec2.pem

ssh &lt;span class="nt"&gt;-i&lt;/span&gt; demo_ec2.pem ec2-user@&amp;lt;private-instance-private-ip&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  5.4 Test Internet Access
&lt;/h2&gt;

&lt;p&gt;Run the following commands from the private EC2 instance.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;ping 8.8.8.8

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If everything is configured correctly, you'll receive a response similar to:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
Pinging 8.8.8.8 with 32 bytes of data:
Reply from 8.8.8.8: bytes=32 time=60ms TTL=117
Reply from 8.8.8.8: bytes=32 time=47ms TTL=117
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This confirms that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;✅ The instance has internet access.&lt;/li&gt;
&lt;li&gt;✅ Traffic is passing through the NAT Gateway.&lt;/li&gt;
&lt;li&gt;✅ The instance still has no public IP.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  5.5 Verify the Instance is Still Private
&lt;/h2&gt;

&lt;p&gt;Try connecting directly to the private instance from your laptop.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;ssh ec2-user@&amp;lt;private-instance-private-ip&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The connection should fail because the instance remains isolated from inbound internet traffic.&lt;/p&gt;

&lt;p&gt;This is exactly how private application servers and databases are deployed in production environments.&lt;/p&gt;




&lt;h2&gt;
  
  
  🔍 Quick Recap
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Resource&lt;/th&gt;
&lt;th&gt;Purpose&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;demo-natgw-eip&lt;/td&gt;
&lt;td&gt;Static Public IP&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;demo-NATGW&lt;/td&gt;
&lt;td&gt;Provides outbound internet access&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;demo-private-RT&lt;/td&gt;
&lt;td&gt;Routes traffic through the NAT Gateway&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Private EC2&lt;/td&gt;
&lt;td&gt;Secure instance without a Public IP&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  💰 Cost Note
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;⚠️ &lt;strong&gt;NAT Gateways are not included in the AWS Free Tier.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;AWS charges:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Hourly usage&lt;/li&gt;
&lt;li&gt;Data processing per GB&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For learning purposes, remember to delete the NAT Gateway and release the Elastic IP when you're finished to avoid unnecessary charges.&lt;/p&gt;




&lt;h2&gt;
  
  
  🧹 Cleanup
&lt;/h2&gt;

&lt;p&gt;Once you're done experimenting:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Terminate both EC2 instances.&lt;/li&gt;
&lt;li&gt;Delete the NAT Gateway.&lt;/li&gt;
&lt;li&gt;Release the Elastic IP.&lt;/li&gt;
&lt;li&gt;Remove the &lt;code&gt;0.0.0.0/0 → demo-NATGW&lt;/code&gt; route from the private Route Table.&lt;/li&gt;
&lt;li&gt;(Optional) Delete the subnets, Internet Gateway, and VPC.&lt;/li&gt;
&lt;/ol&gt;




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

&lt;p&gt;Congratulations! You've successfully extended your custom AWS VPC by adding a &lt;strong&gt;NAT Gateway&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In this tutorial, you learned how to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Allocate an Elastic IP&lt;/li&gt;
&lt;li&gt;Deploy a NAT Gateway&lt;/li&gt;
&lt;li&gt;Configure a Private Route Table&lt;/li&gt;
&lt;li&gt;Launch a private EC2 instance&lt;/li&gt;
&lt;li&gt;Enable outbound-only internet access&lt;/li&gt;
&lt;li&gt;Maintain secure network isolation&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This architecture is widely used in production AWS environments because it balances &lt;strong&gt;security&lt;/strong&gt; and &lt;strong&gt;connectivity&lt;/strong&gt;. Public-facing services remain accessible, while backend systems can safely communicate with the internet without being directly exposed.&lt;/p&gt;




&lt;h2&gt;
  
  
  🚀 What's Next?
&lt;/h2&gt;

&lt;p&gt;In the next project, we'll explore how to make this architecture even more resilient by introducing services such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Application Load Balancer (ALB)&lt;/li&gt;
&lt;li&gt;Auto Scaling Groups&lt;/li&gt;
&lt;li&gt;Multi-AZ deployments&lt;/li&gt;
&lt;li&gt;Amazon RDS in a private subnet&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Together, these components form the foundation of highly available and production-ready AWS architectures.&lt;/p&gt;

&lt;p&gt;Happy Cloud Learning! ☁️🚀&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Source Code &amp;amp; Project Files&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The complete project, including all three stages—&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Amazon EC2 Deployment&lt;/li&gt;
&lt;li&gt;Custom VPC with Public &amp;amp; Private Subnets&lt;/li&gt;
&lt;li&gt;NAT Gateway Configuration&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;—is available on my GitHub repository.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>🌐 Building a Custom VPC on AWS — Public &amp; Private Subnets, Route Tables, and an EC2 Instance</title>
      <dc:creator>Ved Dandotia</dc:creator>
      <pubDate>Fri, 24 Jul 2026 17:02:16 +0000</pubDate>
      <link>https://dev.to/techxved/building-a-custom-vpc-on-aws-public-private-subnets-route-tables-and-an-ec2-instance-4eh1</link>
      <guid>https://dev.to/techxved/building-a-custom-vpc-on-aws-public-private-subnets-route-tables-and-an-ec2-instance-4eh1</guid>
      <description>&lt;p&gt;In my previous post, I launched my first Amazon EC2 instance using AWS's default VPC. While that's a great way to get started, production environments rarely rely on the default network configuration.&lt;/p&gt;

&lt;p&gt;In this tutorial, we'll build a &lt;strong&gt;custom Virtual Private Cloud (VPC)&lt;/strong&gt; from scratch, configure &lt;strong&gt;public and private subnets&lt;/strong&gt;, connect the VPC to the internet using an &lt;strong&gt;Internet Gateway&lt;/strong&gt;, create &lt;strong&gt;Route Tables&lt;/strong&gt;, and finally launch an &lt;strong&gt;EC2 instance&lt;/strong&gt; inside the public subnet.&lt;/p&gt;

&lt;p&gt;By the end of this guide, you'll have a solid understanding of one of the most fundamental networking concepts in AWS.&lt;/p&gt;




&lt;h2&gt;
  
  
  🚀 What You'll Build
&lt;/h2&gt;

&lt;p&gt;In this tutorial, we'll create:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;✅ A Custom VPC&lt;/li&gt;
&lt;li&gt;✅ A Public Subnet&lt;/li&gt;
&lt;li&gt;✅ A Private Subnet&lt;/li&gt;
&lt;li&gt;✅ An Internet Gateway&lt;/li&gt;
&lt;li&gt;✅ Public and Private Route Tables&lt;/li&gt;
&lt;li&gt;✅ An EC2 Instance in the Public Subnet&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🧠 Why Create a Custom VPC?
&lt;/h2&gt;

&lt;p&gt;AWS provides a &lt;strong&gt;default VPC&lt;/strong&gt; in every region, which is perfect for learning or quick deployments. However, real-world cloud architectures require more control over networking.&lt;/p&gt;

&lt;p&gt;With a custom VPC, you can:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Define your own IP address ranges using CIDR blocks&lt;/li&gt;
&lt;li&gt;Separate internet-facing resources from internal resources&lt;/li&gt;
&lt;li&gt;Control routing between subnets and the internet&lt;/li&gt;
&lt;li&gt;Improve security by isolating sensitive workloads&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A common production architecture places &lt;strong&gt;web servers in public subnets&lt;/strong&gt; while keeping &lt;strong&gt;databases and backend services in private subnets&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  🏗️ Architecture
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Faxk10n6arrcwm6hwkup9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Faxk10n6arrcwm6hwkup9.png" alt=" " width="800" height="526"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  📋 Prerequisites
&lt;/h2&gt;

&lt;p&gt;Before starting, make sure you have:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;An AWS Account&lt;/li&gt;
&lt;li&gt;Basic understanding of CIDR and subnets&lt;/li&gt;
&lt;li&gt;Basic familiarity with Amazon EC2 (optional but recommended)&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Step 1 — Create the VPC
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Open the &lt;strong&gt;AWS Management Console&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Navigate to &lt;strong&gt;VPC&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Select &lt;strong&gt;Your VPCs&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Click &lt;strong&gt;Create VPC&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Choose &lt;strong&gt;VPC only&lt;/strong&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Use the following configuration:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Setting&lt;/th&gt;
&lt;th&gt;Value&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Name&lt;/td&gt;
&lt;td&gt;demo-vpc&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;IPv4 CIDR Block&lt;/td&gt;
&lt;td&gt;10.0.0.0/16&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;IPv6&lt;/td&gt;
&lt;td&gt;None&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Tenancy&lt;/td&gt;
&lt;td&gt;Default&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Click &lt;strong&gt;Create VPC&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;/16 CIDR block&lt;/strong&gt; provides &lt;strong&gt;65,536 IP addresses&lt;/strong&gt;, giving you plenty of room to divide your network into multiple subnets.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 2 — Create Public and Private Subnets
&lt;/h2&gt;

&lt;p&gt;We'll create two subnets inside our VPC.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Subnet&lt;/th&gt;
&lt;th&gt;CIDR&lt;/th&gt;
&lt;th&gt;Purpose&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;demo-public-subnet-1a&lt;/td&gt;
&lt;td&gt;10.0.1.0/24&lt;/td&gt;
&lt;td&gt;Internet-facing resources&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;demo-private-subnet-1a&lt;/td&gt;
&lt;td&gt;10.0.2.0/24&lt;/td&gt;
&lt;td&gt;Internal resources&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;To create each subnet:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Open &lt;strong&gt;Subnets&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Click &lt;strong&gt;Create Subnet&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Select &lt;strong&gt;demo-vpc&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Enter the subnet name.&lt;/li&gt;
&lt;li&gt;Choose an Availability Zone (for example, &lt;strong&gt;ap-south-1a&lt;/strong&gt;).&lt;/li&gt;
&lt;li&gt;Enter the CIDR block.&lt;/li&gt;
&lt;li&gt;Click &lt;strong&gt;Create Subnet&lt;/strong&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
&lt;p&gt;💡 &lt;strong&gt;Tip&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;/24 subnet&lt;/strong&gt; provides &lt;strong&gt;256 IP addresses&lt;/strong&gt;, of which &lt;strong&gt;251 are usable&lt;/strong&gt; because AWS reserves five IP addresses in every subnet.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Step 3 — Create an Internet Gateway
&lt;/h2&gt;

&lt;p&gt;An &lt;strong&gt;Internet Gateway (IGW)&lt;/strong&gt; allows resources inside your VPC to communicate with the internet.&lt;/p&gt;

&lt;p&gt;To create one:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Navigate to &lt;strong&gt;Internet Gateways&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Click &lt;strong&gt;Create Internet Gateway&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Name it &lt;strong&gt;demo-IGW&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Create the gateway.&lt;/li&gt;
&lt;li&gt;Select the Internet Gateway.&lt;/li&gt;
&lt;li&gt;Click &lt;strong&gt;Actions → Attach to VPC&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Choose &lt;strong&gt;demo-vpc&lt;/strong&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Without an Internet Gateway, resources inside your VPC cannot communicate with the public internet.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 4 — Create Route Tables
&lt;/h2&gt;

&lt;p&gt;Route Tables determine where network traffic is directed.&lt;/p&gt;

&lt;p&gt;We'll create two Route Tables.&lt;/p&gt;

&lt;h1&gt;
  
  
  Public Route Table
&lt;/h1&gt;

&lt;p&gt;Create a new Route Table.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Setting&lt;/th&gt;
&lt;th&gt;Value&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Name&lt;/td&gt;
&lt;td&gt;demo-public-RT&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;VPC&lt;/td&gt;
&lt;td&gt;demo-vpc&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Add the following routes.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Destination&lt;/th&gt;
&lt;th&gt;Target&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;10.0.0.0/16&lt;/td&gt;
&lt;td&gt;local&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;0.0.0.0/0&lt;/td&gt;
&lt;td&gt;demo-IGW&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Associate this Route Table with:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;demo-public-subnet-1a&lt;/strong&gt;&lt;/p&gt;




&lt;h1&gt;
  
  
  Private Route Table
&lt;/h1&gt;

&lt;p&gt;Create another Route Table.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Setting&lt;/th&gt;
&lt;th&gt;Value&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Name&lt;/td&gt;
&lt;td&gt;demo-private-RT&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;VPC&lt;/td&gt;
&lt;td&gt;demo-vpc&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Leave only the default route.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Destination&lt;/th&gt;
&lt;th&gt;Target&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;10.0.0.0/16&lt;/td&gt;
&lt;td&gt;local&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Associate it with:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;demo-private-subnet-1a&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;⚠️ &lt;strong&gt;Important&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A subnet is considered &lt;strong&gt;private&lt;/strong&gt; because it has &lt;strong&gt;no route to an Internet Gateway&lt;/strong&gt;.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Step 5 — Launch an EC2 Instance
&lt;/h2&gt;

&lt;p&gt;Open the &lt;strong&gt;EC2 Console&lt;/strong&gt; and click &lt;strong&gt;Launch Instance&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Use the following configuration.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Setting&lt;/th&gt;
&lt;th&gt;Value&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;AMI&lt;/td&gt;
&lt;td&gt;Amazon Linux 2&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Instance Type&lt;/td&gt;
&lt;td&gt;t3.micro&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;VPC&lt;/td&gt;
&lt;td&gt;demo-vpc&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Subnet&lt;/td&gt;
&lt;td&gt;demo-public-subnet-1a&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Auto Assign Public IP&lt;/td&gt;
&lt;td&gt;Enabled&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Create or select a Security Group allowing:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Type&lt;/th&gt;
&lt;th&gt;Port&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;SSH&lt;/td&gt;
&lt;td&gt;22&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;HTTP&lt;/td&gt;
&lt;td&gt;80&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Create or choose an existing Key Pair and launch the instance.&lt;/p&gt;

&lt;p&gt;Because this EC2 instance is placed inside the &lt;strong&gt;public subnet&lt;/strong&gt; and the subnet is associated with a Route Table pointing to the Internet Gateway, AWS automatically allows internet connectivity.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 6 — Verify Connectivity
&lt;/h2&gt;

&lt;p&gt;Once the instance reaches the &lt;strong&gt;Running&lt;/strong&gt; state:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Copy its &lt;strong&gt;Public IPv4 Address&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Verify that a Public IP has been assigned&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Connect using SSH.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;ssh &lt;span class="nt"&gt;-i&lt;/span&gt; demo_ec2.pem ec2-user@&amp;lt;your-public-ip&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the connection succeeds, your networking configuration is working correctly.&lt;/p&gt;

&lt;p&gt;Now imagine launching another EC2 instance inside the &lt;strong&gt;private subnet&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;You'll notice:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;No Public IP Address&lt;/li&gt;
&lt;li&gt;No direct internet connectivity&lt;/li&gt;
&lt;li&gt;Accessible only from inside the VPC or through a Bastion Host/NAT Gateway&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That's exactly how secure production architectures are designed.&lt;/p&gt;




&lt;h2&gt;
  
  
  🔍 Quick Recap
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Resource&lt;/th&gt;
&lt;th&gt;Purpose&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;demo-vpc&lt;/td&gt;
&lt;td&gt;Custom Virtual Private Cloud&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;demo-public-subnet-1a&lt;/td&gt;
&lt;td&gt;Hosts public resources&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;demo-private-subnet-1a&lt;/td&gt;
&lt;td&gt;Hosts internal resources&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;demo-IGW&lt;/td&gt;
&lt;td&gt;Provides internet connectivity&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;demo-public-RT&lt;/td&gt;
&lt;td&gt;Routes public traffic to the IGW&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;demo-private-RT&lt;/td&gt;
&lt;td&gt;Keeps private resources isolated&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;EC2 Instance&lt;/td&gt;
&lt;td&gt;Runs inside the public subnet&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h1&gt;
  
  
  🧹 Cleanup
&lt;/h1&gt;

&lt;p&gt;To avoid unnecessary AWS charges:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Terminate the EC2 instance.&lt;/li&gt;
&lt;li&gt;Delete Route Table associations.&lt;/li&gt;
&lt;li&gt;Delete both subnets.&lt;/li&gt;
&lt;li&gt;Detach and delete the Internet Gateway.&lt;/li&gt;
&lt;li&gt;Delete the VPC.&lt;/li&gt;
&lt;/ol&gt;




&lt;h1&gt;
  
  
  🎉 Conclusion
&lt;/h1&gt;

&lt;p&gt;Congratulations! You've successfully built a custom AWS Virtual Private Cloud from scratch.&lt;/p&gt;

&lt;p&gt;In this tutorial, you learned how to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create a custom VPC&lt;/li&gt;
&lt;li&gt;Configure public and private subnets&lt;/li&gt;
&lt;li&gt;Attach an Internet Gateway&lt;/li&gt;
&lt;li&gt;Create Route Tables&lt;/li&gt;
&lt;li&gt;Launch an EC2 instance inside the public subnet&lt;/li&gt;
&lt;li&gt;Understand how AWS networking works behind the scenes&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This architecture forms the foundation of almost every production workload deployed on AWS.&lt;/p&gt;




&lt;h2&gt;
  
  
  🚀 What's Next?
&lt;/h2&gt;

&lt;p&gt;In the next tutorial, we'll add a &lt;strong&gt;NAT Gateway&lt;/strong&gt; so instances inside the private subnet can securely access the internet for software updates and package downloads—without being directly exposed to incoming internet traffic.&lt;/p&gt;

&lt;p&gt;Stay tuned, and happy cloud learning! ☁️&lt;/p&gt;

</description>
      <category>cloud</category>
      <category>aws</category>
      <category>vpc</category>
      <category>subnet</category>
    </item>
    <item>
      <title>Deploying Your First Web Server on AWS EC2 (Step-by-Step with User Data)</title>
      <dc:creator>Ved Dandotia</dc:creator>
      <pubDate>Fri, 24 Jul 2026 08:04:44 +0000</pubDate>
      <link>https://dev.to/techxved/deploying-your-first-web-server-on-aws-ec2-step-by-step-with-user-data-pjb</link>
      <guid>https://dev.to/techxved/deploying-your-first-web-server-on-aws-ec2-step-by-step-with-user-data-pjb</guid>
      <description>&lt;h1&gt;
  
  
  🚀 Launch Your First Amazon EC2 Instance to Host a Web Server
&lt;/h1&gt;

&lt;p&gt;If you're starting your cloud journey with AWS, &lt;strong&gt;Amazon EC2 (Elastic Compute Cloud)&lt;/strong&gt; is one of the first services you'll work with. It allows you to launch virtual machines in the cloud and host applications with complete control over the operating system, networking, and storage.&lt;/p&gt;

&lt;p&gt;In this tutorial, you'll learn how to launch an EC2 instance, configure its networking, automatically install an Apache web server using &lt;strong&gt;EC2 User Data&lt;/strong&gt;, and verify that your website is accessible from anywhere on the internet.&lt;/p&gt;

&lt;p&gt;By the end of this guide, you'll have your own web server running on AWS. 🚀&lt;/p&gt;




&lt;h2&gt;
  
  
  📚 What You'll Learn
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Launch an Amazon EC2 instance&lt;/li&gt;
&lt;li&gt;Configure a Security Group&lt;/li&gt;
&lt;li&gt;Use EC2 User Data to automate server setup&lt;/li&gt;
&lt;li&gt;Deploy an Apache web server&lt;/li&gt;
&lt;li&gt;Verify the deployment&lt;/li&gt;
&lt;li&gt;Clean up AWS resources&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🏗️ Architecture
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F08q2ki5cnmemtqiszbj3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F08q2ki5cnmemtqiszbj3.png" alt=" " width="800" height="176"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Deployment Flow&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Internet
    │
    ▼
Security Group
(SSH :22 | HTTP :80)
    │
    ▼
Amazon EC2 Instance
(Apache Web Server)
    │
    ▼
Static HTML Page
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  ✅ Prerequisites
&lt;/h2&gt;

&lt;p&gt;Before starting, make sure you have:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;An AWS Account (Free Tier eligible)&lt;/li&gt;
&lt;li&gt;Basic knowledge of the AWS Console&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;An SSH client&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Windows: PowerShell, Windows Terminal, or PuTTY&lt;/li&gt;
&lt;li&gt;macOS/Linux: Terminal&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  Step 1 — Launch an EC2 Instance
&lt;/h1&gt;

&lt;ol&gt;
&lt;li&gt;Open the &lt;strong&gt;AWS Management Console&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Navigate to &lt;strong&gt;EC2&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Click &lt;strong&gt;Launch Instance&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Enter an instance name (for example, &lt;strong&gt;demoEC2&lt;/strong&gt;).&lt;/li&gt;
&lt;li&gt;Choose the &lt;strong&gt;Amazon Linux 2 AMI&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Select &lt;strong&gt;t3.micro&lt;/strong&gt; as the instance type.&lt;/li&gt;
&lt;li&gt;Create or select an existing &lt;strong&gt;Key Pair&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Under &lt;strong&gt;Network Settings&lt;/strong&gt;, configure the Security Group.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Configure Inbound Rules
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Type&lt;/th&gt;
&lt;th&gt;Port&lt;/th&gt;
&lt;th&gt;Source&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;SSH&lt;/td&gt;
&lt;td&gt;22&lt;/td&gt;
&lt;td&gt;My IP &lt;em&gt;(Recommended)&lt;/em&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;HTTP&lt;/td&gt;
&lt;td&gt;80&lt;/td&gt;
&lt;td&gt;0.0.0.0/0&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;AWS automatically creates a Security Group (for example, &lt;strong&gt;launch-wizard-2&lt;/strong&gt;).&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;📷 &lt;strong&gt;Add a screenshot of the Launch Instance page here.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h1&gt;
  
  
  Step 2 — Configure User Data
&lt;/h1&gt;

&lt;p&gt;Instead of manually connecting to the server and installing software, we'll use &lt;strong&gt;User Data&lt;/strong&gt;. The script below runs automatically the first time the EC2 instance boots.&lt;/p&gt;

&lt;p&gt;Navigate to:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Advanced Details → User Data&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Paste the following script.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/bin/bash&lt;/span&gt;

&lt;span class="c"&gt;# Update packages&lt;/span&gt;
yum update &lt;span class="nt"&gt;-y&lt;/span&gt;

&lt;span class="c"&gt;# Install Apache&lt;/span&gt;
yum &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;-y&lt;/span&gt; httpd

&lt;span class="c"&gt;# Start Apache&lt;/span&gt;
systemctl start httpd

&lt;span class="c"&gt;# Enable Apache after reboot&lt;/span&gt;
systemctl &lt;span class="nb"&gt;enable &lt;/span&gt;httpd

&lt;span class="c"&gt;# Create sample webpage&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"&amp;lt;h1&amp;gt;Hello World from EC2 User Data!&amp;lt;/h1&amp;gt;"&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; /var/www/html/index.html
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  What does this script do?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Updates installed packages&lt;/li&gt;
&lt;li&gt;Installs Apache HTTP Server&lt;/li&gt;
&lt;li&gt;Starts the Apache service&lt;/li&gt;
&lt;li&gt;Enables Apache to start automatically after every reboot&lt;/li&gt;
&lt;li&gt;Creates a sample HTML page&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Once the instance finishes booting, the web server is ready without any manual setup.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;📷 &lt;strong&gt;Add a screenshot of the User Data section here.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h1&gt;
  
  
  Step 3 — Launch the Instance
&lt;/h1&gt;

&lt;p&gt;Click &lt;strong&gt;Launch Instance&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Within a minute, the instance state should become &lt;strong&gt;Running&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Take note of:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Public IPv4 Address&lt;/li&gt;
&lt;li&gt;Public DNS&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ec2-xx-xx-xx-xx.ap-south-1.compute.amazonaws.com
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;📷 &lt;strong&gt;Add a screenshot of the EC2 Instance Summary here.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h1&gt;
  
  
  Step 4 — Verify the Security Group
&lt;/h1&gt;

&lt;p&gt;Open your instance and navigate to:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Security → Security Groups → Inbound Rules&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Verify that the following rules exist.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Type&lt;/th&gt;
&lt;th&gt;Port&lt;/th&gt;
&lt;th&gt;Source&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;SSH&lt;/td&gt;
&lt;td&gt;22&lt;/td&gt;
&lt;td&gt;Your Public IP&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;HTTP&lt;/td&gt;
&lt;td&gt;80&lt;/td&gt;
&lt;td&gt;0.0.0.0/0&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The default outbound rule (&lt;strong&gt;Allow All Traffic&lt;/strong&gt;) is sufficient for this tutorial.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;⚠️ &lt;strong&gt;Security Best Practice&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Avoid exposing SSH (Port 22) to the entire internet in production. Restrict it to your public IP whenever possible.&lt;/p&gt;

&lt;p&gt;📷 &lt;strong&gt;Add a screenshot of the Security Group rules here.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h1&gt;
  
  
  Step 5 — Test the Web Server
&lt;/h1&gt;

&lt;p&gt;Open your browser and visit either:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;http://&amp;lt;your-public-ip&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;http://&amp;lt;your-public-dns&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If everything is configured correctly, you'll see:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Hello World from EC2 User Data!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;📷 &lt;strong&gt;Add a screenshot showing the webpage in your browser.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h1&gt;
  
  
  Step 6 — (Optional) Connect Using SSH
&lt;/h1&gt;

&lt;p&gt;Connect to your EC2 instance:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;ssh &lt;span class="nt"&gt;-i&lt;/span&gt; your-key.pem ec2-user@&amp;lt;your-public-ip&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Verify Apache is running.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;systemctl status httpd
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Expected output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Active: active (running)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  🧹 Cleanup
&lt;/h2&gt;

&lt;p&gt;To avoid unwanted AWS charges:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Open the EC2 Console.&lt;/li&gt;
&lt;li&gt;Select your instance.&lt;/li&gt;
&lt;li&gt;Click &lt;strong&gt;Instance State&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Choose &lt;strong&gt;Terminate Instance&lt;/strong&gt;.&lt;/li&gt;
&lt;/ol&gt;




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

&lt;p&gt;Congratulations! 🎉&lt;/p&gt;

&lt;p&gt;You successfully:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;✅ Launched an Amazon EC2 instance&lt;/li&gt;
&lt;li&gt;✅ Configured a Security Group&lt;/li&gt;
&lt;li&gt;✅ Automated server setup using EC2 User Data&lt;/li&gt;
&lt;li&gt;✅ Installed and configured Apache&lt;/li&gt;
&lt;li&gt;✅ Hosted your first web page on AWS&lt;/li&gt;
&lt;li&gt;✅ Accessed it using a public IP address&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Learning EC2 is a major milestone in your AWS journey. From here, you can explore more advanced services such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Elastic Load Balancer (ELB)&lt;/li&gt;
&lt;li&gt;Auto Scaling&lt;/li&gt;
&lt;li&gt;Amazon RDS&lt;/li&gt;
&lt;li&gt;Route 53&lt;/li&gt;
&lt;li&gt;Elastic Beanstalk&lt;/li&gt;
&lt;li&gt;Amazon ECS&lt;/li&gt;
&lt;li&gt;AWS CloudFormation&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  💬 Final Thoughts
&lt;/h2&gt;

&lt;p&gt;This is one of the simplest yet most important AWS projects for beginners. It introduces you to compute, networking, security, and automation—all fundamental cloud concepts.&lt;/p&gt;

&lt;p&gt;If you found this tutorial helpful, leave a ❤️, share it with others learning AWS, and let me know what AWS service you'd like to explore next.&lt;/p&gt;

&lt;p&gt;Happy Cloud Learning! ☁️🚀&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Tags:&lt;/strong&gt; &lt;code&gt;#aws&lt;/code&gt; &lt;code&gt;#cloud&lt;/code&gt; &lt;code&gt;#ec2&lt;/code&gt; &lt;code&gt;#devops&lt;/code&gt; &lt;code&gt;#beginners&lt;/code&gt;&lt;/p&gt;

</description>
      <category>cloud</category>
      <category>aws</category>
      <category>ec2</category>
    </item>
    <item>
      <title>Modified Binary Search</title>
      <dc:creator>Ved Dandotia</dc:creator>
      <pubDate>Thu, 23 Oct 2025 11:19:12 +0000</pubDate>
      <link>https://dev.to/techxved/modified-binary-search-51km</link>
      <guid>https://dev.to/techxved/modified-binary-search-51km</guid>
      <description>&lt;h2&gt;
  
  
  1). &lt;strong&gt;Simple Search in Sorted Array&lt;/strong&gt; &lt;em&gt;(Easy)&lt;/em&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;_Given an array of integers nums which is sorted in ascending order, and an integer target, write a function to search target in nums. If target exists, then return its index. Otherwise, return -1._&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;[(&lt;a href="https://leetcode.com/problems/binary-search?envType=problem-list-v2&amp;amp;envId=binary-search)" rel="noopener noreferrer"&gt;https://leetcode.com/problems/binary-search?envType=problem-list-v2&amp;amp;envId=binary-search)&lt;/a&gt;]&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;This is the basic and easiest form of binary search algorithm which is used to search an particular element in an array, Binary search is an alternative of Linear Search to reduce the time complexity of searching algorithm, It is the most optimized searching algorithm till now in DSA.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;Pattern: Try to search the element in 7 or less tries.&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note: Binary Search are not only bound to use with array, It can be implemented in Sorted array, array divided into two sorted array or in sorted input space.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Algorithm:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;1). &lt;em&gt;Set Zeroth index as Low &amp;amp; Last index as High.&lt;/em&gt;&lt;br&gt;
&lt;code&gt;Low = arr[0] &amp;amp; High = arr[n-1]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;2).&lt;em&gt;Find mid i.e middle index of High and Low.&lt;/em&gt;&lt;br&gt;
&lt;code&gt;mid = (low+high)/2&lt;/code&gt; &lt;br&gt;
But&lt;br&gt;
&lt;code&gt;mid = low+(high-low)/2&lt;/code&gt; is advised to use to avoid the integer overflow.&lt;/p&gt;

&lt;p&gt;3). &lt;em&gt;While &lt;code&gt;low&amp;lt;=high&lt;/code&gt;&lt;/em&gt; -&lt;br&gt;
Compare the arr[mid] element with target.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;if &lt;code&gt;arr[mid]==target&lt;/code&gt;&lt;br&gt;
return mid&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;else if (arr[mid]&amp;lt;target)&lt;/code&gt;&lt;br&gt;
low = mid+1&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;else&lt;br&gt;
high = mid-1&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;4). &lt;em&gt;If target is not in the array then low will become greater than high, thus loop will break&lt;/em&gt;&lt;br&gt;
&lt;code&gt;return -1&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int search(int* nums, int numsSize, int target) {


    int low = 0;
    int high =numsSize-1;

    while(low&amp;lt;=high){
         int mid = (low+high)/2;

        if(nums[mid]==target){
            return mid;
        }
        else if(nums[mid]&amp;gt;target){
            high = mid-1;
        }
        else {
            low=mid+1;
        }

    }
    return -1;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Example 1:&lt;br&gt;
Input: nums = [-1,0,3,5,9,12], target = 9&lt;br&gt;
Output: 4&lt;br&gt;
Explanation: 9 exists in nums and its index is 4&lt;/p&gt;


&lt;h2&gt;
  
  
  2). &lt;strong&gt;Search Insert Position&lt;/strong&gt; &lt;em&gt;(Easy)&lt;/em&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;Given a sorted array of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order._&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Example: &lt;br&gt;
Input: arr = [1,3,5,6], target = 2&lt;br&gt;
Output: 1&lt;br&gt;
Explanation: target is not present in the array and it is best fitted at &lt;code&gt;1st Index&lt;/code&gt; so Output is &lt;code&gt;1&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;[&lt;a href="https://leetcode.com/problems/search-insert-position?envType=problem-list-v2&amp;amp;envId=binary-search" rel="noopener noreferrer"&gt;https://leetcode.com/problems/search-insert-position?envType=problem-list-v2&amp;amp;envId=binary-search&lt;/a&gt;]&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;This Problem is an Modified version of binary search in which a little modification in the binary search algorithm is needed in order to solve this problem.
This problem is asking to return the index of target element if present in  the array else, If target is not present in array then return the position index where the target should be best fitted in the array. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Modification:&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;In order to return the index where the target element should be present if it was in the array then we have to return low in last when the loop breaks i.e when&lt;/code&gt;&lt;strong&gt;low&amp;lt;=high&lt;/strong&gt;&lt;code&gt;fails then the low will be the index where the target should be inserted in the array.&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Why to return low? Why not high?&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;When the loop ends the condition becomes &lt;code&gt;low = high+1&lt;/code&gt; -&lt;br&gt;
So now:&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;All elements before low are less than target&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;All elements after or equal to low are greater than target&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;💡 In other words:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;low is exactly the index where target should be inserted.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Algorithm:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;1). &lt;em&gt;Set Zeroth index as Low &amp;amp; Last index as High.&lt;/em&gt;&lt;br&gt;
&lt;code&gt;Low = arr[0] &amp;amp; High = arr[n-1]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;2).&lt;em&gt;Find mid i.e middle index of High and Low.&lt;/em&gt;&lt;br&gt;
&lt;code&gt;mid = (low+high)/2&lt;/code&gt; &lt;br&gt;
But&lt;br&gt;
&lt;code&gt;mid = low+(high-low)/2&lt;/code&gt; is advised to use to avoid the integer overflow.&lt;/p&gt;

&lt;p&gt;3). &lt;em&gt;While &lt;code&gt;low&amp;lt;=high&lt;/code&gt;&lt;/em&gt; -&lt;br&gt;
Compare the arr[mid] element with target.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;if &lt;code&gt;arr[mid]==target&lt;/code&gt;&lt;br&gt;
return mid&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;else if (arr[mid]&amp;lt;target)&lt;/code&gt;&lt;br&gt;
low = mid+1&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;else&lt;br&gt;
high = mid-1&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;4). &lt;em&gt;If target is not in the array then low will become greater than high, thus loop will break&lt;/em&gt;&lt;br&gt;
&lt;code&gt;return low&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int searchInsert(int* nums, int numsSize, int target) {
    int low=0; int high=numsSize-1;

    while(low&amp;lt;=high){
        int mid = (low+high)/2;

        if(nums[mid]==target){
            return mid;
        }

        else if(nums[mid]&amp;gt;target){
            high = mid - 1;
        }

        else{
            low = mid + 1;
        }
    }

    return low;

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  1). &lt;strong&gt;First and last Position of element in sorted array&lt;/strong&gt; &lt;em&gt;(Medium)&lt;/em&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;Given an array of integers nums sorted in non-decreasing order, find the starting and ending position of a given target value.&lt;br&gt;
If target is not found in the array, return [-1, -1].&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Example: &lt;br&gt;
Input: nums = [5,7,7,8,8,10], target = 8&lt;br&gt;
Output: [3,4]&lt;br&gt;
Explanation: target 8 is first found at index 3 and then at index 4. So, [3,4]&lt;/p&gt;

&lt;p&gt;[&lt;a href="https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array" rel="noopener noreferrer"&gt;https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array&lt;/a&gt;]&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;This problem also requires some modification in binary search algorithm in order to solve this problem, In this question we just need to return the index of target element when it is occurred first time and then the last time.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Modification -&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;In this type of problem we just need to use binary search two times at once. The first binary search will return the index of target at which target is first occurred and second binary search will give return the index of second occurrence.&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Algorithm:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;0). Initialize an array of size 2 and set as arr=[-1,-1].&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;For First Occurrence....&lt;/code&gt;&lt;br&gt;
1). &lt;em&gt;Set Zeroth index as Low &amp;amp; Last index as High.&lt;/em&gt;&lt;br&gt;
&lt;code&gt;Low = arr[0] &amp;amp; High = arr[n-1]&lt;/code&gt;&lt;br&gt;
2).&lt;em&gt;Find mid i.e middle index of High and Low.&lt;/em&gt;&lt;br&gt;
&lt;code&gt;mid = (low+high)/2&lt;/code&gt; &lt;br&gt;
But&lt;br&gt;
&lt;code&gt;mid = low+(high-low)/2&lt;/code&gt; is advised to use to avoid the integer overflow.&lt;br&gt;
3). &lt;em&gt;While &lt;code&gt;low&amp;lt;=high&lt;/code&gt;&lt;/em&gt; -&lt;br&gt;
Compare the arr[mid] element with target.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;if &lt;code&gt;arr[mid]==target&lt;/code&gt; {
set arr[0]=mid;  // assume the index as first occurrence
and 
high = mid-1;   // Check more in left side if target is present there
}&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;else if (arr[mid]&amp;lt;target)&lt;/code&gt;
low = mid+1&lt;/li&gt;
&lt;li&gt;else
high = mid-1&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;For Second Occurrence....&lt;/code&gt;&lt;br&gt;
1). &lt;em&gt;Set Zeroth index as Low &amp;amp; Last index as High.&lt;/em&gt;&lt;br&gt;
&lt;code&gt;Low = arr[0] &amp;amp; High = arr[n-1]&lt;/code&gt;&lt;br&gt;
2).&lt;em&gt;Find mid i.e middle index of High and Low.&lt;/em&gt;&lt;br&gt;
&lt;code&gt;mid = (low+high)/2&lt;/code&gt; &lt;br&gt;
But&lt;br&gt;
&lt;code&gt;mid = low+(high-low)/2&lt;/code&gt; is advised to use to avoid the integer overflow.&lt;br&gt;
3). &lt;em&gt;While &lt;code&gt;low&amp;lt;=high&lt;/code&gt;&lt;/em&gt; -&lt;br&gt;
Compare the arr[mid] element with target.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;if &lt;code&gt;arr[mid]==target&lt;/code&gt; {
set arr[1]=mid;  // assume the index as second occurrence
and 
low = mid+1;   // Check more in right side if target is present there
}&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;else if (arr[mid]&amp;lt;target)&lt;/code&gt;
low = mid+1&lt;/li&gt;
&lt;li&gt;else
high = mid-1&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;p&gt;4). &lt;em&gt;If target is not in the array then low will become greater than high, thus loop will break&lt;/em&gt;&lt;br&gt;
&lt;code&gt;return arr&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution {
public:
    vector&amp;lt;int&amp;gt; searchRange(vector&amp;lt;int&amp;gt;&amp;amp; nums, int target) {

        vector&amp;lt;int&amp;gt; arr = {-1,-1};
        int n =nums.size();
        if(n==0)
        return arr;

        int low =0;
        int high = n-1;

        while(low&amp;lt;=high){
            int mid = (high+low)/2;

            if(nums[mid]==target){
            arr[0]=mid;
            high = mid-1;
        }

           else if(nums[mid]&amp;gt;target)
            high = mid-1;

            else
            low=mid+1;


        }

         low=0;
         high = n-1; 
         while(low&amp;lt;=high){
            int mid = (high+low)/2;

            if(nums[mid]==target){
            arr[1]=mid;
            low = mid+1;
            }


           else if(nums[mid]&amp;gt;target)
            high = mid-1;

            else
            low=mid+1;


        }
        return arr;
    }
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>algorithms</category>
      <category>cpp</category>
      <category>programming</category>
      <category>career</category>
    </item>
  </channel>
</rss>
