What if your terraform plan failed the moment a change left the Always Free tier, and told you which limit you hit?
You get exactly that with this open-source Terraform module on Oracle Cloud: two Ampere A1 VMs with no public IP behind a load balancer, SSH only through OCI Bastion, and plan-time guardrails that reject any configuration leaving Always Free unless you switch to paid mode. Part 1 - Architecture Decisions helps you decide whether this architecture fits your project and which trade-offs you accept. Here you see the code, how it is tested, and how you deploy it.
- Terraform Registry: devsahamerlin/alwaysfree-and-flexible-ha-compute-service/oci
- Ready-to-run example: examples/example-1
- GitHub: devsahamerlin/terraform-oci-alwaysfree-and-flexible-ha-compute-service
The constraint, and the Terraform IaC design goals
The Always Free limits your tenancy has to respect: Ampere A1 at 1,500 OCPU hours and 9,000 GB hours per month, which means 2 OCPUs and 12 GB in total (halved from 4 and 24); 1 flexible load balancer at 10 Mbps; 200 GB of boot and block volumes; 5 volume backups; everything in your home region. Cross one of them and your tenancy starts billing.
| Goal | Success criterion |
|---|---|
| $0 by default | Your terraform plan refuses any configuration that leaves Always Free |
| Private by default | No public IP on your VMs, SSH only through OCI Bastion, only 80/443 exposed |
| High availability | At least 2 VMs behind a health-checked load balancer |
| Scale without a rewrite |
free_tier_mode = false switches to paid shapes, same architecture |
| Opt-in features | Database, storage, DNS and container registry behind enable_* flags |
| Verifiable | fmt, validate, TFLint, Checkov and a mocked terraform test on every pull request; Terratest on a real tenancy |
Non-goals: multi-region, Kubernetes, and deploying your application. The module provisions your infrastructure; your app arrives through cloud-init or your own pipeline.
Your VMs live in a private subnet with assign_public_ip = false, and are spread round robin across your region's availability domains (count.index % length(ads)); in single-AD regions, OCI picks the fault domains for you. They reach the internet for updates through a NAT gateway, and OCI services (Object Storage, Autonomous Database, Container Registry) through a service gateway. The public subnet only holds your load balancer and the Bastion endpoint, and its security list only opens 80 and 443.
Your load balancer is pinned to the free shape, distributes traffic round robin and health-checks each VM over HTTP, so a dead VM leaves the pool:
shape = "flexible"
shape_details {
minimum_bandwidth_in_mbps = 10
maximum_bandwidth_in_mbps = 10
}
Your SSH access goes through the OCI Bastion service, with managed SSH sessions. The module creates one session per VM and prints a ready-to-use command for you:
ssh -i ~/.ssh/id_rsa \
-o ProxyCommand="ssh -i ~/.ssh/id_rsa -W 10.0.2.15:22 -p 22 <session_ocid>@host.bastion.<region>.oci.oraclecloud.com" \
-p 22 ubuntu@10.0.2.15
You set the Bastion's client allow list with allowed_ssh_cidrs. If you leave it at 0.0.0.0/0, Terraform warns you through a check block.
Guardrails as plan-time preconditions
Each Always Free limit is a precondition with its own error message, so you know what to change:
resource "null_resource" "free_tier_guard" {
lifecycle {
precondition {
condition = !var.free_tier_mode || local.total_requested_ocpus <= local.max_free_ocpus
error_message = "Always Free validation failed: ${local.total_requested_ocpus} OCPUs requested (vm_count x vm_ocpus), the Always Free limit is ${local.max_free_ocpus} OCPUs. Set free_tier_mode = false to scale beyond it."
}
precondition {
condition = !var.free_tier_mode || local.total_requested_load_balancers <= local.max_free_load_balancers
error_message = "Always Free validation failed: ${local.total_requested_load_balancers} load balancers requested, Always Free includes ${local.max_free_load_balancers}. ..."
}
# + shape, memory, 200 GB of block storage, no scheduled backups
}
}
Your plan fails before any resource is created, which is when you want to hear about cost. The rules sit on a null_resource rather than in variable validations because they combine several variables (vm_count × vm_ocpus, both load balancer flags, total storage), and validations can only reference other variables from Terraform 1.9, while the module supports 1.5 and later. The limits themselves are four values in locals.tf, so when Oracle changes the allowance, you update one line per limit.
Going paid is one flag for you, with the same subnets, Bastion and load balancer logic. In paid mode your Autonomous Database can also use a private endpoint in a dedicated subnet:
free_tier_mode = false
vm_count = 3
vm_shape = "VM.Standard.E4.Flex"
vm_ocpus = 2
vm_memory_gb = 16
enable_backend_lb = true
enable_backups = true
Testing: a mocked provider first, then a real cloud
The module you deploy goes through three layers of tests:
-
terraform fmt -check,terraform validate, TFLint and Checkov on every pull request. -
terraform testwith a mocked OCI provider on every pull request: 17 runs that need no cloud account and no credentials. They check that each Always Free guardrail rejects its case, what everyenable_*flag creates, and the outputs, including oneapplyof the full paid stack against the mocks. - Terratest on a real tenancy: a smoke test (network and one VM), destroyed right after. The full-stack test sends an HTTP request through the load balancer and expects a 200 from the app.
You can test your own module without a cloud in a few lines: a mock_provider block, fake values for the attributes your code indexes (here, availability domains, the service gateway's services and the load balancer IP), and expect_failures on the guard resource:
mock_provider "oci" {
source = "./tests/mocks"
}
run "rejects_more_than_2_ocpus" {
command = plan
variables {
vm_ocpus = 2 # 2 VMs x 2 OCPUs = 4
}
expect_failures = [null_resource.free_tier_guard]
}
Technical trade-offs you accept
Each one is written down in the README and in .checkov.yaml, with when you should revisit it:
-
Stateful security lists (Checkov
CKV_OCI_17). Return traffic is handled for you, so your rules stay simple and hard to get wrong. Stateless rules only pay off at very high throughput, and you have a flag for that. -
Oracle-managed encryption keys (Checkov
CKV_OCI_9/CKV_OCI_3). AES-256 at rest with nothing for you to operate. Switch to customer-managed keys in OCI Vault if you store PCI, PHI or PII data. - Always Free ATP on a public endpoint. Always Free Autonomous Databases can't use private endpoints, so your access relies on the mTLS wallet and the admin password. Paid mode gives you a private endpoint; an access list is on the roadmap.
-
Terraform-managed Bastion sessions. You get a working SSH command right after
apply, but sessions expire after 3 hours, so your next plan recreates them. Making them optional is on the roadmap. - 10 Mbps load balancer. The free shape. Raise it in paid mode, or put a CDN in front of your static assets.
Results: what you get from the Terraform module
- $0/month with the defaults: 2 Ampere A1 VMs (1 OCPU / 6 GB each), a 10 Mbps load balancer, Bastion with one session per VM, a private versioned bucket and one 50 GB block volume per VM. That's 30 resources in your plan, all inside Always Free.
-
Guardrails that work: 6 scenarios checked with
terraform planagainst a real tenancy. The free defaults (30 resources) and a paid E4.Flex setup (33 resources) pass; 4 OCPUs / 24 GB, two load balancers, and storage with backups are rejected with a message that tells you which limit you crossed. -
Tests without a cloud account: 17
terraform testruns against a mocked OCI provider, which you can run yourself without credentials. - Security posture: no public IP on your VMs, only 80/443 exposed, IMDSv1 disabled, in-transit encryption for boot volumes. Checkov: 10 checks passed, 0 failed, with each exception documented. TFLint: 0 issues.
- Terraform first, OpenTofu-compatible: built and tested with Terraform (validated with 1.10), and it also validates with OpenTofu 1.11 if your team needs it.
| Item | Cost |
|---|---|
| 2 × A1 VMs, 200 GB of volumes, 1 load balancer, Bastion, bucket (≤ 20 GB) | $0 (Always Free) |
| Autonomous Database (Always Free, optional) | $0 |
| Second load balancer, scheduled backups, non-A1 shapes | Paid (blocked while free_tier_mode = true) |
| DNS zones, Container Registry (optional, off by default) | Not listed in Always Free, check OCI pricing |
What's next: explicit fault-domain placement for single-AD regions, an access list for the free ATP, optional Bastion sessions, identity-domain support for the registry IAM resources, frontend HTTPS with your own certificate, and a clearer error when atp_admin_password is missing: today, if you enable the database without it, you see the provider's Missing required argument before the module's own message.
Try the Terraform module
Add it to your project from the Terraform Registry:
provider "oci" {
region = "eu-marseille-1" # your tenancy home region
# Authentication: ~/.oci/config, environment variables, or user_ocid/fingerprint/private_key_path
}
module "ha_stack" {
source = "devsahamerlin/alwaysfree-and-flexible-ha-compute-service/oci"
version = "~> 1.0"
tenancy_ocid = var.tenancy_ocid
region = "eu-marseille-1" # your home region
ssh_public_key = file("~/.ssh/id_rsa.pub")
allowed_ssh_cidrs = ["203.0.113.10/32"] # curl ifconfig.me
cloud_init_config = file("${path.module}/cloud-init.yaml") # your app must listen on :8080 for the LB health check
}
Or clone the repository and run the bundled example, which ships a small Nginx page showing which VM answered:
git clone https://github.com/devsahamerlin/terraform-oci-alwaysfree-and-flexible-ha-compute-service.git
cd terraform-oci-alwaysfree-and-flexible-ha-compute-service/examples/example-1
cp terraform.tfvars.example terraform.tfvars # OCIDs, key paths, home region
terraform init && terraform apply
Your first apply takes about 15 minutes, most of it waiting for the Bastion plugin to start on your VMs. Then check the load balancing:
LB=$(terraform output -raw frontend_load_balancer_public_ip)
for i in 1 2 3 4; do curl -s "http://$LB/" | grep -o 'vm-[0-9]*'; done
Hosting your side project shouldn't need a budget line, and "free" shouldn't mean "insecure". If this module saves you an afternoon of VCN plumbing, star the repo, open an issue with what you'd like next, or send a pull request, and share what you build with it.
- 👉 Terraform Registry: https://registry.terraform.io/modules/devsahamerlin/alwaysfree-and-flexible-ha-compute-service/oci
- 👉 GitHub: https://github.com/devsahamerlin/terraform-oci-alwaysfree-and-flexible-ha-compute-service
In Part 1 - Trade Offs , you will see the trade offs of this system



Top comments (0)