Lab4: Terraform Resources - OT-TRAINING/TerraformAWS GitHub Wiki
Terraform Providers, Resources, and Commands
Must To DO:
- Create One file with name main.tf.
- Configure AWS as a provider and download the Terraform plugin for AWS.
- Add resource code to create one Subnet.
- Add resource code to create one VPC.
- Add resource code to create one SG.
- Add resource code to create one EC2. NOTE- Make Sure You are validating your code after adding all resources. Try to use optional arguments as well as much you can. Good To DO:
- Create one resource for ALB.
Let's Begin Now
provider "aws" {
region = "us-east-2"
}
# create VPC
resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16"
instance_tenancy = "default"
tags = {
Name = "test-vpc"
}
}
variable "az_map" {
type = list
default = [
{
name = "us-east-2a"
az = "us-east-2a"
cidr = "10.0.1.0/24"
},
{
name = "us-east-2b"
az = "us-east-2b"
cidr = "10.0.2.0/24"
},
{
name = "us-east-2c"
az = "us-east-2c"
cidr = "10.0.3.0/24"
}
]
}
# create public subnet
resource "aws_subnet" "main" {
count = length(var.az_map)
vpc_id = aws_vpc.main.id
cidr_block = lookup(var.az_map[count.index], "cidr")
availability_zone = lookup(var.az_map[count.index], "az")
map_public_ip_on_launch = true
tags = {
Name = lookup(var.az_map[count.index], "name")
}
}
# create IGW
resource "aws_internet_gateway" "main" {
vpc_id = aws_vpc.main.id
tags = {
Name = "test_igw"
}
}
# Public Route Table
resource "aws_route_table" "main" {
vpc_id = aws_vpc.main.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.main.id
}
}
# Associate the Route Table with the public Subnet
resource "aws_route_table_association" "rahul" {
subnet_id = aws_subnet.main[0].id
route_table_id = aws_route_table.main.id
}
# create ec-2
resource "aws_instance" "main" {
count = 2
ami = "ami-08962a4068733a2b6"
instance_type = "t2.micro"
subnet_id = aws_subnet.main[0].id
vpc_security_group_ids = [aws_security_group.main.id]
tags = {
Name = "test-instance"
}
}
# Create the Security Group
resource "aws_security_group" "main" {
vpc_id = aws_vpc.main.id
name = "main Security Group"
# allow ingress of port 22
ingress {
cidr_blocks = ["0.0.0.0/0"]
from_port = 22
to_port = 22
protocol = "tcp"
}
ingress {
cidr_blocks = ["0.0.0.0/0"]
from_port = 80
to_port = 80
protocol = "tcp"
}
# allow egress of all ports
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags = {
Name = "test-SG"
}
}
resource "aws_lb" "test" {
name = "test-lb-tf"
internal = false
load_balancer_type = "application"
security_groups = [aws_security_group.main.id]
subnets = aws_subnet.main.*.id
tags = {
Environment = "production"
}
}
resource "aws_lb_target_group" "test" {
name = "tf-test-lb-tg"
port = 80
protocol = "HTTP"
vpc_id = aws_vpc.main.id
}
resource "aws_lb_listener" "redirect" {
load_balancer_arn = aws_lb.test.arn
port = "80"
protocol = "HTTP"
default_action {
type = "redirect"
redirect {
port = "443"
protocol = "HTTPS"
status_code = "HTTP_301"
}
}
}
resource "aws_lb_target_group_attachment" "test" {
count = length(aws_instance.main)
target_group_arn = aws_lb_target_group.test.arn
target_id = aws_instance.main[count.index].id
port = 80
}