<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[aws_3_teir]]></title><description><![CDATA[aws_3_teir]]></description><link>https://aws3teir.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Tue, 01 Sep 2026 00:05:55 GMT</lastBuildDate><atom:link href="https://aws3teir.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Building a 3-tier Web Application Architecture with AWS CLI]]></title><description><![CDATA[As we know, a three-tier architecture has three main layers. Presentation layer, application/business layer, and logic layer. The Presentation layer contains the basic user interface, the application/business layer contains business logic, and the lo...]]></description><link>https://aws3teir.hashnode.dev/building-a-3-tier-web-application-architecture-with-aws-cli</link><guid isPermaLink="true">https://aws3teir.hashnode.dev/building-a-3-tier-web-application-architecture-with-aws-cli</guid><dc:creator><![CDATA[Vijeta Rajane]]></dc:creator><pubDate>Tue, 11 Mar 2025 19:39:48 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1741721969196/a3d84726-117f-4230-b171-213f6fb7b153.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>As we know, a three-tier architecture has three main layers. Presentation layer, application/business layer, and logic layer. The Presentation layer contains the basic user interface, the application/business layer contains business logic, and the logic layer contains data storage or a database. In this article, we are deep diving into how to build a three-tier web application architecture using the AWS CLI. There are two main ways to build a three-tier web application architecture on AWS, such as the AWS management console and the AWS CLI. But here we use the AWS CLI approach, which is the fastest and easiest way for setup and automation. If we use the AWS management console, which is a web-based interface, it will take too much time.</p>
<h2 id="heading-primary-terminology-related-to-3-tier-application-architecture">Primary terminology Related to 3 Tier Application Architecture</h2>
<p>The following are the some of the primary terminologies related to 3 Tier application architecture:</p>
<ul>
<li><p><a target="_blank" href="https://www.geeksforgeeks.org/how-to-install-aws-cli-on-ubuntu/"><strong>AWS CLI</strong></a><strong>:</strong> It is a powerful command-line interface that helps us interact with <a target="_blank" href="https://www.geeksforgeeks.org/top-aws-services/"><strong>AWS services</strong></a> and resources.</p>
</li>
<li><p><a target="_blank" href="https://www.geeksforgeeks.org/amazon-vpc-introduction-to-amazon-virtual-cloud/"><strong>VPC</strong></a><strong>:</strong> It is a logically isolated section of networking on the AWS cloud.</p>
</li>
<li><p><strong>Security group:</strong> It acts like a virtual firewall, which monitors and controls network traffic from the internet.</p>
</li>
<li><p><strong>Subnet:</strong> it is a logically isolated segment of VPC. Each subnet has a CIDR block.</p>
</li>
<li><p><strong>Internet gateway:</strong> It allows resources in your VPC to connect to the public internet for inbound and outbound traffic.</p>
</li>
<li><p><a target="_blank" href="https://www.geeksforgeeks.org/what-is-elastic-compute-cloud-ec2/"><strong>EC2</strong></a> <strong>instance</strong>: It is a virtual compute server that is used to run applications and services.</p>
</li>
<li><p><a target="_blank" href="https://www.geeksforgeeks.org/amazon-rds-introduction-to-amazon-relational-database-system/"><strong>RDS</strong></a> <strong>is a relational database:</strong> It service that is famous for complex queries and transactions. And it enables a multi-availability zone.</p>
</li>
<li><p><strong>Route table:</strong> It is a set of rules called routes that decides where your traffic will be directed, either through a through a subnet or an internet gateway.</p>
</li>
<li><p><strong>Elastic load balancer:</strong> It distributes traffic that comes from the internet and distributes it across multiple instances or targets. Here, the target can be an EC2 instance, <a target="_blank" href="https://www.geeksforgeeks.org/containerization-using-docker/"><strong>containers</strong></a>, etc.</p>
</li>
<li><p><a target="_blank" href="https://www.geeksforgeeks.org/create-and-configure-the-auto-scaling-group-in-ec2/"><strong>Auto-scaling group</strong></a><strong>:</strong> It is a simple set of instances that automatically scale up or down based on traffic or load.</p>
</li>
</ul>
<p><img src="https://media.geeksforgeeks.org/wp-content/uploads/20240626093233/CompressJPEGOnline_1_70kb_1789.jpg" alt="3 Tier Architecture" /></p>
<h2 id="heading-a-3-tier-web-application-architecture-overview">A 3 Tier Web Application Architecture - Overview</h2>
<p>In your AWS account, create one VPC (virtual private cloud) in any region. Here, I created it in the Asia-Pacific region (Mumbai). One thing that you know about pricing, Pricing can be varied based on the selection of location or geographical area. For example,</p>
<ul>
<li><p><strong>US Regions</strong> (Ohio, North Virginia, and Oregon) have cost-effectiveness as compared to other regions.</p>
</li>
<li><p><strong>South America</strong> (Sao Paulo) has the highest cost as compared to other regions.</p>
</li>
</ul>
<p>Region pricing factors are,</p>
<p><strong>1. Cost of Infrastructure</strong></p>
<ul>
<li><p>Investment in the land</p>
</li>
<li><p>Data center construction and maintenance</p>
</li>
</ul>
<p>If the above price is higher, then you need to pay a high for service in those regions.</p>
<p><strong>2. Demand and Supply</strong></p>
<ul>
<li><p><strong>Market demand:</strong> Slightly higher pricing due to increased resource utilization and the potential need for additional infrastructure investment.</p>
</li>
<li><p><strong>Supply Availability:</strong> If a region has limited resources but competition among customers is very high as compared to what is required for competition, then pricing will be high.</p>
</li>
</ul>
<p><strong>3. Operational Costs</strong></p>
<p><strong>Labor Costs:</strong> It will also affect the pricing of services.</p>
<p><img src="https://media.geeksforgeeks.org/wp-content/uploads/20240621225324/CompressJPEGOnline_1_70kb_5433.jpg" alt="operational Costs" /></p>
<p>Oncs you created VPC then Creates subnets.</p>
<p>It has main two types,</p>
<div class="hn-table">
<table>
<thead>
<tr>
<td><strong>Feature</strong></td><td><strong>Public Subnet</strong></td><td><strong>Private Subnet</strong></td></tr>
</thead>
<tbody>
<tr>
<td><strong>Internet Connectivity</strong></td><td>It is directly connected to the internet gateway.</td><td>It is not directly connected with internet gateway.</td></tr>
<tr>
<td><strong>Resource Placement</strong></td><td>It contains Web servers, load balancers, internet-facing resources</td><td>It contains Application servers, databases, internal resources</td></tr>
<tr>
<td><strong>Security</strong></td><td>It Requires careful security group configuration</td><td>It Offers higher security by default</td></tr>
<tr>
<td><strong>Route Table</strong></td><td>Route to internet gateway (0.0.0.0/0 -&gt; igw-id)</td><td>May or may not have route to internet gateway, might have route to NAT gateway</td></tr>
</tbody>
</table>
</div><ul>
<li><p>Here in this VPC we create subnets in two different availability zone.</p>
</li>
<li><p>ap-south-1a Availability zone has main three subnets, In that one public subnet and two private subnets.</p>
</li>
<li><p>ap-south-1b Availability zone has main three subnets, In that one public subnet and two private subnets.</p>
</li>
</ul>
<p><img src="https://media.geeksforgeeks.org/wp-content/uploads/20240621225327/CompressJPEGOnline_1_70kb_8716.jpg" alt="VPC with 3 Tier application" /></p>
<p>Here in the below diagram (Figure 1.3), it simply shows that we are creating four EC2 instances, with two uploading on the public subnet and the other two on the private subnet.</p>
<ul>
<li><p>Public subnets contain Web servers.</p>
</li>
<li><p>Private subnets contain application servers.</p>
</li>
</ul>
<p><img src="https://media.geeksforgeeks.org/wp-content/uploads/20240621225328/CompressJPEGOnline_1_70kb_9544.jpg" alt="AWS VPC Architecture of 3 Tier Application" /></p>
<ul>
<li><p>Now we are attaching an Internet gateway to our VPC and adding two databases on different private subnets.</p>
</li>
<li><p>Which cannot be directly accessed by a public IP address but only communicates with the application server.</p>
</li>
</ul>
<p><img src="https://media.geeksforgeeks.org/wp-content/uploads/20240621225327/CompressJPEGOnline_1_70kb_7278.jpg" alt="AWS 3 Tier Application Archtiecture" /></p>
<p>Now we are adding Route table between two subnets that are in the different availability zones.</p>
<ul>
<li><p>One Public route table</p>
</li>
<li><p>Two Private route table</p>
</li>
</ul>
<p><img src="https://media.geeksforgeeks.org/wp-content/uploads/20240621225323/CompressJPEGOnline_1_70kb_4325.jpg" alt="AWS 3 Tier Architecture" /></p>
<p>Each subnets layers contains one security groups.</p>
<ul>
<li><p>Web security group</p>
</li>
<li><p>App security group</p>
</li>
<li><p>Database security group</p>
</li>
</ul>
<p>Here first and second layer contains the load balancer for distribution of the traffic.</p>
<p><img src="https://media.geeksforgeeks.org/wp-content/uploads/20240621225322/CompressJPEGOnline_1_70kb_3452.jpg" alt="AWS 3 Tier Architecture" /></p>
<p>Here we added security groups in the First and second layer</p>
<p><img src="https://media.geeksforgeeks.org/wp-content/uploads/20240621225325/CompressJPEGOnline_1_70kb_6389.jpg" alt="3 Tier Application Architecture" /></p>
<ul>
<li>Here is the short demonstration on the workflow of 3 Tier Application Architecture:</li>
</ul>
<h2 id="heading-solution-overview">Solution Overview</h2>
<p>Here entire process is divided into three segments:</p>
<p>1. Network Layer Setup</p>
<ul>
<li><p>Step 1: Configuring AWS CLI</p>
</li>
<li><p>step 2: Creating a VPC and Internet Gateway and Attach to VPC</p>
</li>
<li><p>Step 4: Creating Route Table for Public Subnet an<br />  Step 3: Creating Internd Associate</p>
</li>
<li><p>Step 5: Modifying Subnet Attributes to Enable Public IP</p>
</li>
<li><p>Step 6: Creating Security Groups</p>
</li>
<li><p>Step 7: Configuring Security Group Rules</p>
</li>
<li><p>Step 8: Load Balancer and Auto Scaling Setup</p>
</li>
<li><p>Step 9: Creating Launch Template</p>
</li>
<li><p>Step 10: Creating Auto Scaling Group</p>
</li>
</ul>
<p>2. Application Layer Setup</p>
<ul>
<li><p>Step 1: Launching EC2 instances</p>
</li>
<li><p>Step 2: Load Balancer and Auto Scaling Setup</p>
</li>
<li><p>Step 3: Creating Launch Template</p>
</li>
<li><p>Step 4: Creating Auto Scaling Group</p>
</li>
</ul>
<p>3. Data Layer Setup</p>
<ul>
<li><p>Step 1: Creating database subnet group</p>
</li>
<li><p>Step 2: Launching RDS instance</p>
</li>
</ul>
<h2 id="heading-building-a-3-tier-web-application-with-aws-cli-a-step-by-step-guide">Building a 3-Tier Web Application with AWS CLI: A Step-By-Step Guide</h2>
<p>The following are the steps that guides in building a 3 tier web application architecture from AWS CLI:</p>
<h3 id="heading-step-1-configuring-aws-cli">Step 1: Configuring AWS CLI</h3>
<ul>
<li><p><strong>Install AWS CLI:</strong> Follow the instructions from the AWS CLI documentation for installing AWS CLI as per your operating system.</p>
</li>
<li><p><strong>Configure AWS CLI:</strong> After Installing AWS CLI you configure your AWS CLI through following command.</p>
</li>
</ul>
<pre><code class="lang-plaintext">aws configure
</code></pre>
<p><img src="https://media.geeksforgeeks.org/wp-content/uploads/20240529232745/CompressJPEGOnline_1_70kb_9668.jpg" alt="COnfiguring AWS CLi" /></p>
<p>After that you enter some necessary information such as,</p>
<ul>
<li><p>Your AWS access key,</p>
</li>
<li><p>Secret key,</p>
</li>
<li><p>Region, and output format.</p>
</li>
</ul>
<p><strong>Note:</strong> Whenever we are writing any variable at that time we start like, Variable_name = $(aws ec2 create...).</p>
<h3 id="heading-network-layer-setup">❖ Network Layer Setup</h3>
<h3 id="heading-step-1-creating-a-vpc">Step 1: Creating a VPC</h3>
<pre><code class="lang-plaintext">VPC_ID=$(aws ec2 create-vpc --cidr-block 10.0.0.0/16 --query 'Vpc.VpcId' --output text)
aws ec2 create-tags --resources $VPC_ID --tags Key=Name,Value=MyVPC
</code></pre>
<ul>
<li>Here we are creating VPC with MyVPC name and their cidr range is 10.0.0.0/16 means 16 bit network mask.</li>
</ul>
<p>VPC_ID is a variable.</p>
<p><img src="https://media.geeksforgeeks.org/wp-content/uploads/20240624210037/CompressJPEGOnline_1_70kb_7252.jpg" alt="CompressJPEGOnline_1_70kb_7252" /></p>
<h3 id="heading-step-2-creating-subnets"><strong>Step 2:</strong> Creating Subnets</h3>
<pre><code class="lang-plaintext"># Create Public Subnets
PUBLIC_SUBNET_ID_1=$(aws ec2 create-subnet --vpc-id $VPC_ID --cidr-block 10.0.1.0/24 --availability-zone ap-south-2a --query 'Subnet.SubnetId' --output text)
aws ec2 create-tags --resources $PUBLIC_SUBNET_ID_1 --tags Key=Name,Value=PublicSubnet-1a

PUBLIC_SUBNET_ID_2=$(aws ec2 create-subnet --vpc-id $VPC_ID --cidr-block 10.0.2.0/24 --availability-zone ap-south-2b --query 'Subnet.SubnetId' --output text)
aws ec2 create-tags --resources $PUBLIC_SUBNET_ID_2 --tags Key=Name,Value=PublicSubnet-1b
</code></pre>
<ul>
<li><p>Here we are creating public subnet in the first availability zone with PublicSubnet-1a name and their cidr range is 10.0.1.0/24 means 24 bit network mask.</p>
</li>
<li><p>And second public subnet in the second availability zone with PublicSubnet-1b name and their cidr range is 10.0.2.0/24 means 24 bit network mask.</p>
</li>
<li><p>PUBLIC_SUBNET_ID_1 is a variable.</p>
</li>
<li><p>PUBLIC_SUBNET_ID_2 is also second subnet variable.</p>
</li>
</ul>
<pre><code class="lang-plaintext"># Create Private Subnets for App Servers
PRIVATE_APP_SUBNET_ID_1=$(aws ec2 create-subnet --vpc-id $VPC_ID --cidr-block 10.0.3.0/24 --availability-zone ap-south-1a --query 'Subnet.SubnetId' --output text)
aws ec2 create-tags --resources $PRIVATE_APP_SUBNET_ID_1 --tags Key=Name,Value=AppSubnet-1a

PRIVATE_APP_SUBNET_ID_2=$(aws ec2 create-subnet --vpc-id $VPC_ID --cidr-block 10.0.4.0/24 --availability-zone ap-south-1b --query 'Subnet.SubnetId' --output text)
aws ec2 create-tags --resources $PRIVATE_APP_SUBNET_ID_2 --tags Key=Name,Value=AppSubnet-1b
</code></pre>
<p>Here we are creating private App subnet in the first availability zone with AppSubnet-1a name and their cidr range is 10.0.3.0/24 means 24 bit network mask.</p>
<p>And second private App subnet in the second availability zone with AppSubnet-1b name and their cidr range is 10.0.4.0/24 means 24 bit network mask.</p>
<p>PRIVATE_APP_SUBNET_ID_1 is a variable.</p>
<p>PRIVATE_APP_SUBNET_ID_2 is also second subnet variable.</p>
<pre><code class="lang-plaintext"># Create Private Subnets for Database
PRIVATE_DB_SUBNET_ID_1=$(aws ec2 create-subnet --vpc-id $VPC_ID --cidr-block 10.0.5.0/24 --availability-zone ap-south-1a --query 'Subnet.SubnetId' --output text)
aws ec2 create-tags --resources $PRIVATE_DB_SUBNET_ID_1 --tags Key=Name,Value=DbSubnet-1a

PRIVATE_DB_SUBNET_ID_2=$(aws ec2 create-subnet --vpc-id $VPC_ID --cidr-block 10.0.6.0/24 --availability-zone ap-south-1b --query 'Subnet.SubnetId' --output text)
aws ec2 create-tags --resources $PRIVATE_DB_SUBNET_ID_2 --tags Key=Name,Value=DbSubnet-1b
</code></pre>
<ul>
<li><p>Here we are creating private Database subnet in the first availability zone with AppSubnet-1a name and their cidr range is 10.0.5.0/24 means 24 bit network mask.</p>
</li>
<li><p>And second private Database subnet in the second availability zone with AppSubnet-1b name and their cidr range is 10.0.6.0/24 means 24 bit network mask.</p>
</li>
<li><p>PRIVATE_DB_SUBNET_ID_1 is a variable.</p>
</li>
<li><p>PRIVATE_DB_SUBNET_ID_2 is also second subnet variable.</p>
</li>
</ul>
<p><img src="https://media.geeksforgeeks.org/wp-content/uploads/20240624210036/CompressJPEGOnline_1_70kb_6876.jpg" alt="Creating VPC" /></p>
<ul>
<li>The following screenshot the successful creation of VPC:</li>
</ul>
<p><img src="https://media.geeksforgeeks.org/wp-content/uploads/20240624212257/CompressJPEGOnline_1_70kb_3134.jpg" alt="Created VPC and subnets" /></p>
<h3 id="heading-step-3-creating-internet-gateway-and-attach-to-vpc"><strong>Step 3:</strong> Creating Internet Gateway and Attach to VPC</h3>
<pre><code class="lang-plaintext"># Create Internet Gateway and Attach to VPC
IGW_ID=$(aws ec2 create-internet-gateway --query 'InternetGateway.InternetGatewayId' --output text)
aws ec2 attach-internet-gateway --vpc-id $VPC_ID --internet-gateway-id $IGW_ID
aws ec2 create-tags --resources $IGW_ID --tags Key=Name, Value=InternetGateway
</code></pre>
<ul>
<li><p>First we are Creating Internet Gateway and attaching with VPC.</p>
</li>
<li><p>Here --query 'InternetGateway.InternetGatewayId' is filtering the output of the create-internet-gateway command.</p>
</li>
<li><p>Here --output text that instructs the AWS CLI to output format should be in the plain text.</p>
</li>
<li><p>As we know that by default format is JSON or YAML.</p>
</li>
</ul>
<p><img src="https://media.geeksforgeeks.org/wp-content/uploads/20240624210033/CompressJPEGOnline_1_70kb_819.jpg" alt="Creating VPC" /></p>
<ul>
<li>The following screenshot illustrates on successful creation of Internet Gateway:</li>
</ul>
<p><img src="https://media.geeksforgeeks.org/wp-content/uploads/20240624210035/CompressJPEGOnline_1_70kb_4595.jpg" alt="Creating Internet Gateway" /></p>
<h3 id="heading-step-4-creating-route-table-for-public-subnet-and-associate">Step 4: Creating Route Table for Public Subnet and Associate</h3>
<pre><code class="lang-plaintext"># Create Route Table for Public Subnet and Associate
PUBLIC_ROUTE_TABLE_ID=$(aws ec2 create-route-table --vpc-id $VPC_ID --query 'RouteTable.RouteTableId' --output text)
aws ec2 create-route --route-table-id $PUBLIC_ROUTE_TABLE_ID --destination-cidr-block 0.0.0.0/0 --gateway-id $IGW_ID
aws ec2 associate-route-table --subnet-id $PUBLIC_SUBNET_ID_1 --route-table-id $PUBLIC_ROUTE_TABLE_ID
aws ec2 associate-route-table --subnet-id $PUBLIC_SUBNET_ID_2 --route-table-id $PUBLIC_ROUTE_TABLE_ID
</code></pre>
<ul>
<li><p>Here --query 'RouteTable.RouteTableId' is filtering the output of the create-route-table command. and in a simple way we say that it instructs the AWS CLI to look into the RouteTable object which contains the RouteTableId(unique ID).</p>
</li>
<li><p>Here --output text that instructs the AWS CLI to output format should be in the plain text.</p>
</li>
</ul>
<p><img src="https://media.geeksforgeeks.org/wp-content/uploads/20240624210036/CompressJPEGOnline_1_70kb_4931.jpg" alt="Creating Route Table for public subnet" /></p>
<p>The following screenhot illustrates on successful creation of customized vpc:</p>
<p><img src="https://media.geeksforgeeks.org/wp-content/uploads/20240624212230/CompressJPEGOnline_1_70kb_625.jpg" alt="Created custom VPC" /></p>
<h3 id="heading-step-5-modifying-subnet-attributes-to-enable-public-ip">Step 5: Modifying Subnet Attributes to Enable Public IP</h3>
<pre><code class="lang-plaintext"># Modify Subnet Attributes to Enable Public IP
aws ec2 modify-subnet-attribute --subnet-id $PUBLIC_SUBNET_ID_1 --map-public-ip-on-launch
aws ec2 modify-subnet-attribute --subnet-id $PUBLIC_SUBNET_ID_2 --map-public-ip-on-launch
</code></pre>
<ul>
<li>After creation of subnets we are modifying public subnets for enabling public-IP.</li>
</ul>
<p><img src="https://media.geeksforgeeks.org/wp-content/uploads/20240624210036/CompressJPEGOnline_1_70kb_4931.jpg" alt="Creating route table" /></p>
<p><img src="https://media.geeksforgeeks.org/wp-content/uploads/20240624212257/CompressJPEGOnline_1_70kb_3134.jpg" alt="CompressJPEGOnline_1_70kb_3134" /></p>
<h3 id="heading-step-6-creating-security-groups">Step 6: Creating Security Groups</h3>
<pre><code class="lang-plaintext"># Create Security Groups
WEBSG_ID=$(aws ec2 create-security-group --group-name WebSG --description "Web Security Group" --vpc-id $VPC_ID --query 'GroupId' --output text)
aws ec2 create-tags --resources $WEBSG_ID --tags Key=Name,Value=WebSecurityGroup

APPSG_ID=$(aws ec2 create-security-group --group-name AppSG --description "App Security Group" --vpc-id $VPC_ID --query 'GroupId' --output text)
aws ec2 create-tags --resources $APPSG_ID --tags Key=Name,Value=AppSecurityGroup

DBSG_ID=$(aws ec2 create-security-group --group-name DBSG --description "DB Security Group" --vpc-id $VPC_ID --query 'GroupId' --output text)
aws ec2 create-tags --resources $DBSG_ID --tags Key=Name,Value=DBSecurityGroup
</code></pre>
<ul>
<li><p>As we discuss the primary terminology, Security groups are act like virtual firewall. Here security groups contains main 4 key points</p>
</li>
<li><p>--group-name</p>
</li>
<li><p>--description</p>
</li>
<li><p>--vpc-id</p>
</li>
<li><p>--query</p>
</li>
<li><p>--output</p>
</li>
</ul>
<p><img src="https://media.geeksforgeeks.org/wp-content/uploads/20240624212258/CompressJPEGOnline_1_70kb_4177.jpg" alt="Creating route TABLE for public subnet" /></p>
<p><img src="https://media.geeksforgeeks.org/wp-content/uploads/20240624213155/sg.jpg" alt="creating sg" /></p>
<h3 id="heading-step-7-configuring-security-group-rules">Step 7: Configuring Security Group Rules</h3>
<pre><code class="lang-plaintext"># Configure Security Group Rules
# Web SG
aws ec2 authorize-security-group-ingress --group-id $WEBSG_ID --protocol tcp --port 80 --cidr 0.0.0.0/0
aws ec2 authorize-security-group-ingress --group-id $WEBSG_ID --protocol tcp --port 22 --cidr 0.0.0.0/0

# App SG
aws ec2 authorize-security-group-ingress --group-id $APPSG_ID --protocol tcp --port 80 --source-group $WEBSG_ID
aws ec2 authorize-security-group-ingress --group-id $APPSG_ID --protocol tcp --port 22 --cidr 49.36.88.95/32

# DB SG
aws ec2 authorize-security-group-ingress --group-id $DBSG_ID --protocol tcp --port 3306 --source-group $APPSG_ID
</code></pre>
<p>Here security group rules contains main 5 key points</p>
<ul>
<li><p>--group-id</p>
</li>
<li><p>--protocol</p>
</li>
<li><p>--port</p>
</li>
<li><p>--cidr</p>
</li>
<li><p>Here port number 80 is stands for HTTP traffic handler and port number 22 is stands for SSH traffic handler.</p>
</li>
</ul>
<p><img src="https://media.geeksforgeeks.org/wp-content/uploads/20240624214231/CompressJPEGOnline_1_70kb_1811.jpg" alt="Creating Security Groups" /></p>
<p><img src="https://media.geeksforgeeks.org/wp-content/uploads/20240624214232/CompressJPEGOnline_1_70kb_3479.jpg" alt="Creating Security Groups" /></p>
<p><img src="https://media.geeksforgeeks.org/wp-content/uploads/20240624214230/CompressJPEGOnline_1_70kb_65.jpg" alt="Creating Security Groups" /></p>
<h3 id="heading-step-8-load-balancer-setup">Step 8: Load Balancer Setup</h3>
<pre><code class="lang-plaintext"># Create Target Group
TARGET_GROUP_ARN=$(aws elbv2 create-target-group --name MyTargetGroup --protocol HTTP --port 80 --vpc-id $VPC_ID --target-type instance --query 'TargetGroups[0].TargetGroupArn' --output text)

# Create Load Balancer
LOAD_BALANCER_ARN=$(aws elbv2 create-load-balancer --name MyLoadBalancer --subnets $PUBLIC_SUBNET_ID_1 $PUBLIC_SUBNET_ID_2 --security-groups $WEBSG_ID --query 'LoadBalancers[0].LoadBalancerArn' --output text)

# Create Listener
aws elbv2 create-listener --load-balancer-arn $LOAD_BALANCER_ARN --protocol HTTP --port 80 --default-actions Type=forward,TargetGroupArn=$TARGET_GROUP_ARN
</code></pre>
<p><img src="https://media.geeksforgeeks.org/wp-content/uploads/20240624215940/CompressJPEGOnline_1_70kb_4231.jpg" alt="Creating Target groups" /></p>
<p><img src="https://media.geeksforgeeks.org/wp-content/uploads/20240624215941/CompressJPEGOnline_1_70kb_6242.jpg" alt="list of target groups" /></p>
<ul>
<li>The following screenshot illustrates on successful ccreation of load balancers:</li>
</ul>
<p><img src="https://media.geeksforgeeks.org/wp-content/uploads/20240624215939/CompressJPEGOnline_1_70kb_2868.jpg" alt="Creating Load balancers" /></p>
<h3 id="heading-step-9-creating-launch-template">Step 9: Creating Launch Template</h3>
<pre><code class="lang-plaintext"># Define User Data Script (secure alternative using Secrets Manager recommended)
USER_DATA=$(echo -n '#!/bin/bash
sudo yum update -y
sudo yum install -y httpd
sudo systemctl start httpd
sudo systemctl enable httpd
' | base64 -w 0)
# Create Launch Template
LAUNCH_TEMPLATE_ID=$(aws ec2 create-launch-template --launch-template-name MyWebLaunchTemplate --version-description "v1" --launch-template-data "{
  \"ImageId\": \"ami-0f58b397bc5c1f2e8\",
  \"InstanceType\": \"t2.micro\",
  \"KeyName\": \"webserver-gfg\",
  \"SecurityGroupIds\": [\"$WEBSG_ID\"],
  \"UserData\": \"$USER_DATA\"
}" --query 'LaunchTemplate.LaunchTemplateId' --output text)

echo "Launch Template ID: $LAUNCH_TEMPLATE_ID"
</code></pre>
<ul>
<li>Here you replace "ami-0f58b397bc5c1f2e8" with actual ImageId and "webserver-gfg" with actual your key-pairs.</li>
</ul>
<p><img src="https://media.geeksforgeeks.org/wp-content/uploads/20240624215940/CompressJPEGOnline_1_70kb_4022.jpg" alt="Defining Userdata" /></p>
<p><img src="https://media.geeksforgeeks.org/wp-content/uploads/20240624215941/CompressJPEGOnline_1_70kb_5130.jpg" alt="Listing Launch Tempates" /></p>
<h3 id="heading-step-10-creating-auto-scaling-group">Step 10: Creating Auto Scaling Group</h3>
<pre><code class="lang-plaintext"># Create Auto Scaling Group
aws autoscaling create-auto-scaling-group --auto-scaling-group-name MyWebASG --launch-template LaunchTemplateId=$LAUNCH_TEMPLATE_ID,Version=1 --min-size 1 --max-size 3 --desired-capacity 2 --vpc-zone-identifier $PUBLIC_SUBNET_ID_1,$PUBLIC_SUBNET_ID_2 --target-group-arns $TARGET_GROUP_ARN

# Attach Load Balancer to Auto Scaling Group
aws autoscaling attach-load-balancer-target-groups --auto-scaling-group-name MyWebASG --target-group-arns $TARGET_GROUP_ARN
</code></pre>
<p>Here we are creating auto scaling group that basically uses the lunchtemplate with maximum and minimum size.</p>
<p>After that we attach load balancer with it.</p>
<p><img src="https://media.geeksforgeeks.org/wp-content/uploads/20240624220656/CompressJPEGOnline_1_70kb_9079.jpg" alt="Creating Autoscaling" /></p>
<p><img src="https://media.geeksforgeeks.org/wp-content/uploads/20240624220658/CompressJPEGOnline_1_70kb_9863.jpg" alt="Listing Autoscaling Groups" /></p>
<h2 id="heading-application-layer-setup">Application Layer Setup</h2>
<h3 id="heading-step-1-launching-ec2-instances">Step 1: Launching EC2 instances</h3>
<pre><code class="lang-plaintext"># Launch EC2 Instances for Web Servers
WEB_INSTANCE_ID_1=$(aws ec2 run-instances --image-id ami-0abcdef1234567890 --count 1 --instance-type t2.micro --key-name MyKeyPair --security-group-ids $WEBSG_ID --subnet-id $PUBLIC_SUBNET_ID_1 --associate-public-ip-address --query 'Instances[0].InstanceId' --output text)
aws ec2 create-tags --resources $WEB_INSTANCE_ID_1 --tags Key=Name,Value=WebServer1

WEB_INSTANCE_ID_2=$(aws ec2 run-instances --image-id ami-0abcdef1234567890 --count 1 --instance-type t2.micro --key-name MyKeyPair --security-group-ids $WEBSG_ID --subnet-id $PUBLIC_SUBNET_ID_2 --associate-public-ip-address --query 'Instances[0].InstanceId' --output text)
aws ec2 create-tags --resources $WEB_INSTANCE_ID_2 --tags Key=Name,Value=WebServer2

# Launch EC2 Instances for App Servers
APP_INSTANCE_ID_1=$(aws ec2 run-instances --image-id ami-0abcdef1234567890 --count 1 --instance-type t2.micro --key-name MyKeyPair --security-group-ids $APPSG_ID --subnet-id $PRIVATE_APP_SUBNET_ID_1 --query 'Instances[0].InstanceId' --output text)
aws ec2 create-tags --resources $APP_INSTANCE_ID_1 --tags Key=Name,Value=AppServer1

APP_INSTANCE_ID_2=$(aws ec2 run-instances --image-id ami-0abcdef1234567890 --count 1 --instance-type t2.micro --key-name MyKeyPair --security-group-ids $APPSG_ID --subnet-id $PRIVATE_APP_SUBNET_ID_2 --query 'Instances[0].InstanceId' --output text)
aws ec2 create-tags --resources $APP_INSTANCE_ID_2 --tags Key=Name,Value=AppServer2
</code></pre>
<ul>
<li>Here we are launching public web severs for public subnets and private application servers for private subnets.</li>
</ul>
<p>Whenever we are launching EC2 instance that time we requires main 8 key points</p>
<ul>
<li><p>--image-id</p>
</li>
<li><p>--count</p>
</li>
<li><p>--instance-type</p>
</li>
<li><p>--key-name</p>
</li>
<li><p>--security-group-ids</p>
</li>
<li><p>--subnet-id</p>
</li>
<li><p>--query</p>
</li>
<li><p>--output</p>
</li>
</ul>
<p><img src="https://media.geeksforgeeks.org/wp-content/uploads/20240624214230/CompressJPEGOnline_1_70kb_348.jpg" alt="Launching Instance" /></p>
<p><img src="https://media.geeksforgeeks.org/wp-content/uploads/20240624214830/CompressJPEGOnline_1_70kb_8308.jpg" alt="Listing EC2 Instances" /></p>
<h3 id="heading-step-2-load-balancer-setup">Step 2: Load Balancer Setup</h3>
<pre><code class="lang-plaintext"># Create Auto Scaling Group for App Servers
aws autoscaling create-auto-scaling-group --auto-scaling-group-name AppASG --launch-template LaunchTemplateId=$LAUNCH_TEMPLATE_ID,Version=1 --min-size 1 --max-size 3 --desired-capacity 2 --vpc-zone-identifier $PRIVATE_APP_SUBNET_ID_1,$PRIVATE_APP_SUBNET_ID_2 --target-group-arns $APP_TARGET_GROUP_ARN

# Attach Load Balancer to Auto Scaling Group
aws autoscaling attach-load-balancer-target-groups --auto-scaling-group-name AppASG --target-group-arns $APP_TARGET_GROUP_ARN
</code></pre>
<p><img src="https://media.geeksforgeeks.org/wp-content/uploads/20240624220654/CompressJPEGOnline_1_70kb_7684.jpg" alt="Creating Target Groups" /></p>
<p><img src="https://media.geeksforgeeks.org/wp-content/uploads/20240624222727/CompressJPEGOnline_1_70kb_8753.jpg" alt="Listing Target Groups" /></p>
<p><img src="https://media.geeksforgeeks.org/wp-content/uploads/20240624222727/CompressJPEGOnline_1_70kb_8309.jpg" alt="Creating Load Balancers" /></p>
<h3 id="heading-step-3-creating-launch-template">Step 3: Creating Launch Template</h3>
<pre><code class="lang-plaintext"># Define User Data Script (secure alternative using Secrets Manager recommended)
USER_DATA=$(echo -n '#!/bin/bash
sudo yum update -y
sudo yum install -y httpd
sudo systemctl start httpd
sudo systemctl enable httpd
' | base64 -w 0)
# Create Launch Template
LAUNCH_TEMPLATE_ID=$(aws ec2 create-launch-template --launch-template-name AppLaunchTemplate  --version-description "v1" --launch-template-data "{
  \"ImageId\": \"ami-0f58b397bc5c1f2e8\",
  \"InstanceType\": \"t2.micro\",
  \"KeyName\": \"webserver-gfg\",
  \"SecurityGroupIds\": [\"$APPSG_ID\"],
  \"UserData\": \"$USER_DATA\"
}" --query 'LaunchTemplate.LaunchTemplateId' --output text)

echo "Launch Template ID: $LAUNCH_TEMPLATE_ID"
</code></pre>
<ul>
<li>Here you replace "ami-0f58b397bc5c1f2e8" with actual ImageId and "webserver-gfg" with actual your key-pairs.</li>
</ul>
<p><img src="https://media.geeksforgeeks.org/wp-content/uploads/20240624220657/CompressJPEGOnline_1_70kb_9734.jpg" alt="Creating Launching Templates" /></p>
<h3 id="heading-step-4-creating-auto-scaling-group">Step 4: Creating Auto Scaling Group</h3>
<pre><code class="lang-plaintext"># Create Auto Scaling Group for App Servers
aws autoscaling create-auto-scaling-group --auto-scaling-group-name AppASG --launch-template LaunchTemplateId=$APP_LAUNCH_TEMPLATE_ID,Version=1 --min-size 1 --max-size 3 --desired-capacity 2 --vpc-zone-identifier $PRIVATE_APP_SUBNET_ID_1,$PRIVATE_APP_SUBNET_ID_2 --target-group-arns $APP_TARGET_GROUP_ARN

# Attach Load Balancer to Auto Scaling Group
aws autoscaling attach-load-balancer-target-groups --auto-scaling-group-name AppASG --target-group-arns $APP_TARGET_GROUP_ARN
</code></pre>
<p><img src="https://media.geeksforgeeks.org/wp-content/uploads/20240624220656/CompressJPEGOnline_1_70kb_9079.jpg" alt="Creating Autoscaling Groups" /></p>
<p><img src="https://media.geeksforgeeks.org/wp-content/uploads/20240624222726/CompressJPEGOnline_1_70kb_7620.jpg" alt="Listing AutoScaling Groups" /></p>
<h3 id="heading-data-layer-setup">❖ Data Layer Setup</h3>
<h3 id="heading-step-1-creating-database-subnet-group">Step 1: Creating database subnet group</h3>
<pre><code class="lang-plaintext"># Create DB Subnet Group
aws rds create-db-subnet-group --db-subnet-group-name MyDBSubnetGroup --db-subnet-group-description "DB Subnet Group" --subnet-ids $PRIVATE_DB_SUBNET_ID_1 $PRIVATE_DB_SUBNET_ID_2
</code></pre>
<p>Whenever we are creating database subnet group contains main 3 key points</p>
<ul>
<li><p>--db-subnet-group-name</p>
</li>
<li><p>--db-subnet-group-description</p>
</li>
<li><p>--subnet-ids</p>
</li>
</ul>
<p><img src="https://media.geeksforgeeks.org/wp-content/uploads/20240624214828/CompressJPEGOnline_1_70kb_587.jpg" alt="Creating Subnets" /></p>
<p><img src="https://media.geeksforgeeks.org/wp-content/uploads/20240624214830/CompressJPEGOnline_1_70kb_7535.jpg" alt="Listing Subnet Groups" /></p>
<h3 id="heading-step-2-launching-rds-instance">Step 2: Launching RDS instance</h3>
<pre><code class="lang-plaintext"># Launch RDS Instance
DB_INSTANCE_ID=$(aws rds create-db-instance --db-instance-identifier mydbinstance --db-instance-class db.t2.micro --engine mysql --allocated-storage 20 --master-username admin --master-user-password yourpassword --vpc-security-group-ids $DBSG_ID --db-subnet-group-name MyDBSubnetGroup --query 'DBInstance.DBInstanceIdentifier' --output text)
</code></pre>
<p>Whenever we are Launching RDS instance that contains main 10 key points</p>
<ul>
<li><p>--db-instance-identifier</p>
</li>
<li><p>--db-instance-class</p>
</li>
<li><p>--engine</p>
</li>
<li><p>--allocated-storage</p>
</li>
<li><p>--master-username admin</p>
</li>
<li><p>--master-user-password</p>
</li>
<li><p>--vpc-security-group-ids</p>
</li>
<li><p>--db-subnet-group-name</p>
</li>
<li><p>--query</p>
</li>
<li><p>--output</p>
</li>
</ul>
<p><img src="https://media.geeksforgeeks.org/wp-content/uploads/20240624214828/CompressJPEGOnline_1_70kb_587.jpg" alt="Creating db subnet group" /></p>
<ul>
<li>The following screenshot illustrates on creation subnet:</li>
</ul>
<p><img src="https://media.geeksforgeeks.org/wp-content/uploads/20240624214829/CompressJPEGOnline_1_70kb_5753.jpg" alt="Creating database" /></p>
<h3 id="heading-how-much-charge-it-will-take-after-completion-of-architecture">How much charge it will take after completion of architecture?</h3>
<p>It will depends on below factors and your usage.</p>
<div class="hn-table">
<table>
<thead>
<tr>
<td><strong>Service</strong></td><td><strong>Cost</strong></td></tr>
</thead>
<tbody>
<tr>
<td><strong>VPC</strong></td><td>$0 (per hour)</td></tr>
<tr>
<td><strong>Public Subnet (after 1 EIP)</strong></td><td>$0.005 (per hour)</td></tr>
<tr>
<td><strong>Security Groups</strong></td><td>$0 (per hour)</td></tr>
<tr>
<td><strong>NAT Gateway</strong></td><td>$0.045 (per hour) + $0.045 per GB processed</td></tr>
<tr>
<td><strong>EC2 Instances</strong></td><td>Varies (on-demand, reserved, spot)</td></tr>
<tr>
<td><strong>RDS Instance</strong></td><td>Varies (type, storage, region)</td></tr>
<tr>
<td><strong>Load Balancers</strong></td><td>Varies (requests, data processed, hours)</td></tr>
</tbody>
</table>
</div>]]></content:encoded></item></channel></rss>