Prerequisites:
AWS account
Create an IAM User with admin access
Installation of AWS CLI and Terraform
Create Public and Private Subnets
What is a Public Subnet?
A public subnet is a portion of a computer network that is associated with a public IP address and can be directly accessed from the internet. In cloud computing environments, such as Amazon Web Services (AWS) or Microsoft Azure, public subnets are often used to host resources that need to be publicly accessible, such as web servers or load balancers
What is a Private Subnet?
A private subnet is a portion of a computer network that is not directly accessible from the internet. In contrast to public subnets, resources in a private subnet typically do not have public IP addresses and are shielded from direct exposure to external network traffic. Private subnets are often used to host internal resources that should be kept secure and are not intended for direct internet access.
provider "aws" {
region = "us-west-1"
}
resource "aws_subnet" "test_public_1" {
vpc_id = aws_vpc.test.id
cidr_block = "10.0.1.0/24"
map_public_ip_on_launch = true
availability_zone = "us-west-1b"
tags = {
Name = "test.public.1"
}
}
resource "aws_subnet" "test_public_2" {
vpc_id = aws_vpc.test.id
cidr_block = "10.0.2.0/24"
map_public_ip_on_launch = true
availability_zone = "us-west-1c"
tags = {
Name = "test.public.2"
}
}
Create an Internet Gateway and attach it VPC
What is VPC?
In Amazon Web Services (AWS), a Virtual Private Cloud (VPC) is a virtual network dedicated to your AWS account. It provides a way to create an isolated section of the AWS Cloud where you can launch AWS resources in a virtual network that you define.
What is an internet Gateway?
An Internet Gateway (IGW) in the context of cloud computing, particularly in services like Amazon Web Services (AWS), is a component that facilitates communication between resources within your Virtual Private Cloud (VPC) and the internet. It acts as a horizontally scaled, redundant, and highly available gateway that allows instances within your VPC to connect to the internet, and vice versa.
resource "aws_vpc" "test" {
cidr_block = "10.0.0.0/16"
instance_tenancy = "default"
enable_dns_support = true
enable_dns_hostnames = true
tags = {
Name = "test"
}
}
resource "aws_internet_gateway" "test_gw" {
vpc_id = aws_vpc.test.id
tags = {
Name = "test_gw"
}
}
Create two EC2 in the substances
Amazon Elastic Compute Cloud (Amazon EC2) is a core web service provided by Amazon Web Services (AWS) that enables users to rent virtual servers, known as "instances," on which they can run their own applications. EC2 instances provide scalable computing capacity in the cloud, allowing users to quickly scale up or down based on their application requirements.
resource "aws_instance" "terraformdem1" {
ami = "ami-010f8b02680f80998" # Replace with your desired AMI ID
instance_type = "t2.micro"
subnet_id = aws_subnet.test_public_1.id
tags = {
Name = "terraformdemo1"
}
}
resource "aws_instance" "terraformdemo2" {
ami = "ami-010f8b02680f80998" # Replace with your desired AMI ID
instance_type = "t2.micro"
subnet_id = aws_subnet.test_public_2.id
tags = {
Name = "terraformdemo2"
}
}
Create Two route tables and associate Subnets to the route tables
What is a route table?
In networking, a route table is a set of rules, often implemented in a router or a switch, that determines where network traffic is directed. In the context of cloud computing, including services like Amazon Web Services (AWS) Virtual Private Cloud (VPC), a route table is used to control the traffic leaving and entering subnets.
resource "aws_route_table" "route_test" {
vpc_id = aws_vpc.test.id
route {
cidr_block = "0.0.0.0/0" # Default route for internet access
gateway_id = aws_internet_gateway.test_gw.id
}
}
# Create Route Table Association
resource "aws_route_table_association" "test_public_1_a" {
subnet_id = aws_subnet.test_public_1.id
route_table_id = aws_route_table.route_test.id
}
resource "aws_route_table_association" "test_public_2_a" {
subnet_id = aws_subnet.test_public_2.id
route_table_id = aws_route_table.route_test.id
}
After using the Code, apply
terraform init
terraform validate
terraform plan
terraform apply
After applying these codes, go to your AWS account and check the VPC, Subnets, EC2, Internet Gateway, and route tables
In the End, Use the terraform destroy code to destroy the complete resources.