Configure a load balancer
You can configure a load balancer to distribute traffic across your VM (Virtual Machine) using Terraform.
Before you begin
To perform the actions described below, you must have:
- A Numspot account
- Terraform ↗ installed
- A browser or an HTTP client
warning
The load balancer health checks must succeed in reaching your services so that the load balancer can direct traffic to the correct instances.
Create and deploy Numspot resources using Terraform
To get started, first create the configuration file ↗.
You must then create the following resources:
- VPC (Virtual Private Cloud);
- subnet;
- 2 VM;
- internet gateway;
- route table;
- security group;
- load balancer.
Add the following blocks:
resource "numspot_vpc" "vpc" {
ip_range = "10.0.0.0/16"
}
resource "numspot_subnet" "subnet" {
vpc_id = numspot_vpc.vpc.id
ip_range = "10.0.0.0/24"
}
resource "numspot_vm" "vm-1" {
image_id = "<VM IMAGE>"
subnet_id = numspot_subnet.subnet.id
type = "<VM TYPE>"
security_group_ids = [numspot_security_group.security-group.id]
}
resource "numspot_vm" "vm-2" {
image_id = "<VM IMAGE>"
subnet_id = numspot_subnet.subnet.id
type = "<VM TYPE>"
security_group_ids = [numspot_security_group.security-group.id]
}
resource "numspot_internet_gateway" "internet-gateway" {
vpc_id = numspot_vpc.vpc.id
}
resource "numspot_route_table" "route-table" {
vpc_id = numspot_vpc.vpc.id
subnet_id = numspot_subnet.subnet.id
routes = [{
destination_ip_range = "0.0.0.0/0"
gateway_id = numspot_internet_gateway.internet-gateway.id
}]
}
resource "numspot_security_group" "security-group" {
vpc_id = numspot_vpc.vpc.id
name = "security group name"
description = "security group description"
inbound_rules = [{
from_port_range = 80
to_port_range = 80
ip_protocol = "tcp"
ip_ranges = ["0.0.0.0/0"]
}]
}
resource "numspot_load_balancer" "load-balancer" {
name = "nlb"
listeners = [{
load_balancer_port = 80
backend_port = 80
load_balancer_protocol = "TCP"
}]
subnets = [numspot_subnet.subnet.id]
security_groups = [numspot_security_group.security-group.id]
backend_vm_ids = [numspot_vm.vm-1.id, numspot_vm.vm-2.id]
type = "internet-facing"
health_check = {
healthy_threshold = 10
check_interval = 30
path = "/"
port = 80
protocol = "HTTP"
timeout = 5
unhealthy_threshold = 5
}
}
Your configuration is now ready to be deployed on the Numspot infrastructure ↗.
Permissions
info
This action requires the following IAM (Identity and Access Management) permissions:
- compute.vpc.create
- compute.subnet.create
- compute.vm.create
- compute.internetGateway.create
- compute.routeTable.create
- compute.securityGroup.create
- compute.loadBalancer.create