Sure! Here's a simple explanation of what this Terraform code does:
-
Creating a Virtual Network (VPC):
- A new virtual network called
demo_vpc
is created with a range of IP addresses from10.0.0.0
to10.0.255.255
. This is like setting up a private, isolated section of a larger network where you can place your resources.
- A new virtual network called
-
Setting Up an Internet Gateway:
- An internet gateway called
gw
is created and attached to thedemo_vpc
. This gateway allows resources within the VPC to communicate with the internet.
- An internet gateway called
-
Creating a Route Table:
- A route table named
route_table
is created for the VPC. This route table has a rule that directs all outbound traffic (0.0.0.0/0
means "anywhere on the internet") to go through the internet gateway.
- A route table named
-
Creating a Subnet:
- A subnet called
my_subnet
is created within the VPC. Think of a subnet as a smaller network within your larger virtual network. This subnet uses the IP range10.0.1.0
to10.0.1.255
and is located in the availability zoneus-east-1a
.
- A subnet called
-
Associating the Subnet with the Route Table:
- The subnet
my_subnet
is linked to the route tableroute_table
, which ensures that resources in this subnet follow the routing rules defined in the route table.
- The subnet
-
Setting Up a Security Group:
- A security group named
instance_sg
is created to control traffic to and from resources within the VPC. It allows:- Inbound SSH traffic (port 22) from anywhere.
- Inbound HTTP traffic (port 80) from anywhere.
- Inbound HTTPS traffic (port 443) from anywhere.
- All outbound traffic (all ports) to go anywhere.
- A security group named
-
Launching a Web Server Instance:
- An instance (virtual machine) named
webserver
is launched using an Amazon Machine Image (AMI) identified byami-053b0d53c279acc90
. This instance is at2.micro
type and uses an SSH key namedterrakey
. - This instance is located in the subnet
my_subnet
and the availability zoneus-east-1a
. - When the instance starts, a script runs to:
- Update the system packages.
- Install the Apache web server.
- Start the Apache web server.
- Create a simple web page with the message "Congratulations! on your first Webserver Installation."
- An instance (virtual machine) named
-
Outputting the Public IP:
- Finally, the code outputs the public IP address of the
webserver
instance, which you can use to access the web server from the internet.
- Finally, the code outputs the public IP address of the
In summary, this Terraform script sets up a basic infrastructure on AWS: a virtual network, an internet gateway, a subnet, security rules, and a virtual machine running a web server.