Build a scalable web app on Compute Instances
20 min tutorial
What you'll build
Two Compute Instances sharing a label, and an L4 Load Balancer that fronts both of them behind a single public IP — the pattern for scaling a web app horizontally instead of resizing a single instance.
Prerequisites
- A SonicInfra account with billing enabled.
- An SSH key on your account (handy for troubleshooting, not required by the steps below).
- Comfort with the basics from Deploy your first compute instance — this tutorial moves faster over instance creation and spends its time on the new part: labels and load balancer selectors.
Open Compute Instances
Click Compute Instances in the sidebar.
Create the first instance
Click + Create Compute Instance.
Fill out the instance details
Fill in the fields:
- Instance Type — pick
ne.2for this tutorial. - Image — pick
ubuntu-24-04. - Labels — this is the important one. Add a label so a Load Balancer can find this instance later without naming it directly:
tier: web. Any instance carrying this label will be treated as part of the same pool.
Before you create it
Compute instances are a paid resource — your organization needs billing set up before you can create one.
Haven't set up billing yet? Here's how to add a payment method first.
Create the first instance
- Web
- CLI
- Terraform
Click Create Compute Instance.
instanceType—ne.2— 2 cores, 8Gi RAM. Plenty for a small web app; bump the core count for more headroom.labels—tier: webis what the Load Balancer's selector will match on later — not the instance name — so it can find both instances without listing them individually.
sonic instances create web-01 \
--instanceType ne.2 \
--image dWJ1bnR1LTI0LTA0 \
--labels.tier web
# Generated by SonicInfra
# Review before applying — some fields may need adjustment
# (e.g. cloud_init requires a YAML string in the Terraform provider)
resource "sonicinfra_compute_instance" "web_01" {
name = "web-01"
instance_type = "ne.2"
image = "ubuntu-24-04"
labels = { "tier" = "web" }
}
Create a second instance
Click + Create Compute Instance again — one instance isn't a pool. This second one is what makes the Load Balancer's selector actually matter.
Create the second instance
- Web
- CLI
- Terraform
Same Instance Type and Image as before. Give it a different Name — web-02 — but the same label: tier: web. Click Create Compute Instance.
sonic instances create web-02 \
--instanceType ne.2 \
--image dWJ1bnR1LTI0LTA0 \
--labels.tier web
# Generated by SonicInfra
# Review before applying — some fields may need adjustment
# (e.g. cloud_init requires a YAML string in the Terraform provider)
resource "sonicinfra_compute_instance" "web_02" {
name = "web-02"
instance_type = "ne.2"
image = "ubuntu-24-04"
labels = { "tier" = "web" }
}
Open L4 Load Balancers
Compute instances don't get a public IP on their own. An L4 Load Balancer allocates one and forwards traffic to a raw TCP/UDP port on whatever instances match its selector. Click L4 Load Balancers in the sidebar.
Create a load balancer
Click + Create L4 Load Balancer.
Target both instances by label
Fill in the fields:
- Target — by default this field picks one instance by name. Click Advanced: match by any label, then set Label key to
tierand Label value toweb. That matches bothweb-01andweb-02— and any instance you add later with the same label. - Ports — add one mapping: Port
80, Target Port3000(whatever port your app listens on inside each instance).
Create the load balancer
- Web
- CLI
- Terraform
Click Create L4 Load Balancer.
sonic networking-load-balancers create web-lb \
--selector.key tier \
--selector.value web \
--ports '{"port":80,"targetPort":3000}'
# Generated by SonicInfra
# Review before applying — some fields may need adjustment
# (e.g. cloud_init requires a YAML string in the Terraform provider)
resource "sonicinfra_load_balancer" "web_lb" {
name = "web-lb"
selector = { "key" = "tier", "value" = "web" }
ports {
port = 80
target_port = 3000
}
}
Open the load balancer
Its row is highlighted below — click it to open the details panel.
Find the address
Find the assigned address under External IP. Traffic sent to <external-ip>:80 now gets spread across every instance matching tier: web — currently web-01 and web-02. Confirm it from your terminal:
curl http://<external-ip>
Run it a few times (or with -v and a couple of retries) and you'll see responses coming from either instance, not always the same one.
Scale out further
To add capacity, you don't touch the load balancer at all — create another Compute Instance with the same tier: web label (a web-03, using the same Instance Type, Image, and label as before) and it joins the pool automatically. Remove an instance and the selector stops sending it traffic just as automatically. The load balancer's selector, not a fixed list of instances, is what defines the pool.