0% found this document useful (0 votes)
11 views8 pages

Lab Terraform Day3 1

Uploaded by

d
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views8 pages

Lab Terraform Day3 1

Uploaded by

d
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Lab: Terraform project structure

Project Overview
We'll deploy:
 A VPC.
 Two subnets (public and private).
 An Internet Gateway for the public subnet.
 An EC2 instance in the public subnet.

Directory Structure

terraform-aws-project/
├── main.tf # Core resources (VPC, subnets, EC2, etc.)
├── variables.tf # Input variables for configuration
├── outputs.tf # Outputs for key resource values
├── provider.tf # AWS provider configuration
├── terraform.tfvars # Variable values (secrets, environment-specific configs)
├── backend.tf # Remote backend configuration (optional)
└── modules/ # Reusable modules
└── ec2/ # Example module for EC2 instances
├── main.tf
├── variables.tf
├── outputs.tf

Steps (please do check ya, as a trainer, I can make mistakes)


1. Provider Configuration-this will configure the provider, like aws, azure.. in this
case, we are using aws..

provider "aws" {
region = "ap-southeast-2"
}

2. Define Variables (variables.tf)


Reason: Input variables allow flexibility for configurations.

variable "vpc_cidr" {
description = "CIDR block for the VPC"
default = "10.0.0.0/16"
}

variable "public_subnet_cidr" {
description = "CIDR block for the public subnet"
default = "10.0.1.0/24"
}

variable "private_subnet_cidr" {
description = "CIDR block for the private subnet"
default = "10.0.2.0/24"
}

variable "instance_type" {
description = "EC2 instance type"
default = "t2.micro"
}

3. Core Infrastructure (main.tf)

Reason: We define the core infra like VPC, Subnets, EC3

# VPC
resource "aws_vpc" "main_vpc" {
cidr_block = var.vpc_cidr
enable_dns_support = true
enable_dns_hostnames = true
tags = {
Name = "TerraformVPC"
}
}

# Public Subnet
resource "aws_subnet" "public_subnet" {
vpc_id = aws_vpc.main_vpc.id
cidr_block = var.public_subnet_cidr
map_public_ip_on_launch = true
tags = {
Name = "PublicSubnet"
}
}

# Private Subnet
resource "aws_subnet" "private_subnet" {
vpc_id = aws_vpc.main_vpc.id
cidr_block = var.private_subnet_cidr
tags = {
Name = "PrivateSubnet"
}
}

# Internet Gateway
resource "aws_internet_gateway" "igw" {
vpc_id = aws_vpc.main_vpc.id
tags = {
Name = "InternetGateway"
}
}

# Route Table for Public Subnet


resource "aws_route_table" "public_route_table" {
vpc_id = aws_vpc.main_vpc.id
tags = {
Name = "PublicRouteTable"
}
}

resource "aws_route" "public_route" {


route_table_id = aws_route_table.public_route_table.id
destination_cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.igw.id
}

resource "aws_route_table_association" "public_subnet_assoc" {


subnet_id = aws_subnet.public_subnet.id
route_table_id = aws_route_table.public_route_table.id
}

# Security Group for EC2


resource "aws_security_group" "allow_ssh" {
vpc_id = aws_vpc.main_vpc.id
description = "Allow SSH inbound traffic"

ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}

egress {
from_port = 0
to_port =0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags = {
Name = "AllowSSH"
}
}

# EC2 Instance
module "ec2_instance" {
source = "./modules/ec2"
instance_type = var.instance_type
public_subnet = aws_subnet.public_subnet.id
security_group = aws_security_group.allow_ssh.id
}

4. Outputs (outputs.tf)

Reason: To show useful outputs after deployments

output "vpc_id" {
value = aws_vpc.main_vpc.id
}

output "public_subnet_id" {
value = aws_subnet.public_subnet.id
}

output "ec2_public_ip" {
value = module.ec2_instance.public_ip
}
5. Module for EC2 (modules/ec2/main.tf)
Reason: Reusable EC2 module.

resource "aws_instance" "instance" {


ami = "ami-0d6560f3176dc9ec0" # Replace with valid AMI
instance_type = var.instance_type
subnet_id = var.public_subnet
security_groups = [var.security_group]

tags = {
Name = "TerraformInstance"
}
}

output "public_ip" {
value = aws_instance.instance.public_ip
}

5. Module Variables (modules/ec2/variables.tf)

Reason: define module inputs

variable "instance_type" {
description = "Type of instance to launch"
}

variable "public_subnet" {
description = "ID of the public subnet"
}

variable "security_group" {
description = "ID of the security group"
}

7. Module Outputs (modules/ec2/outputs.tf)


Reason: Return the public IP of the EC2 instance.

output "public_ip" {
value = aws_instance.instance.public_ip
}

Next- The usual: Terraform init,validate, plan and apply!

You might also like