CodexBloom - Programming Q&A Platform

OCI Resource Manager scenarios to Create Infrastructure with Invalid Variable Reference

👀 Views: 1311 đŸ’Ŧ Answers: 1 📅 Created: 2025-06-10
terraform oci infrastructure-as-code hcl

I've been researching this but I'm deploying to production and I'm having a hard time understanding I'm working on a project and hit a roadblock. I'm trying to set up an infrastructure using OCI Resource Manager with Terraform, but I'm running into an scenario where the plan fails due to an invalid variable reference. Specifically, I have a variable defined in my `variables.tf` file as follows: ```hcl variable "instance_shape" { description = "Shape of the compute instance" type = string default = "VM.Standard2.1" } ``` In my `main.tf`, I attempt to use this variable when defining a compute instance: ```hcl resource "oci_core_instance" "example_instance" { availability_domain = "us-ashburn-1-ad-1" compartment_id = var.compartment_id display_name = "example-instance" shape = var.instance_shape source_details { source_type = "image" source_id = var.image_id } metadata = { ssh_authorized_keys = file("~/.ssh/id_rsa.pub") } } ``` However, when I run `terraform apply`, I receive the following behavior: ``` behavior: Invalid variable reference on main.tf line 6: 6: shape = var.instance_shape Invalid reference to variable "instance_shape": variable doesn't exist in this context. ``` I've double-checked that the variable is indeed defined in `variables.tf`, and I need to figure out why this behavior is occurring. I've also tried running `terraform init` and `terraform validate` to see if there are any issues, but everything seems fine. My Terraform version is 1.0.0, and I'm using the OCI provider version 4.0.0. I've also confirmed that my `terraform` block is properly configured for the provider: ```hcl terraform { required_providers { oci = { source = "oracle/oci" version = "4.0.0" } } } ``` Can anyone shed some light on why I'm getting this behavior? Is there something I might be missing in terms of variable scoping or module usage? I would greatly appreciate any insights or suggestions for troubleshooting this scenario. For context: I'm using Hcl on Ubuntu. What am I doing wrong? I'm on Linux using the latest version of Hcl. Any help would be greatly appreciated! This is my first time working with Hcl 3.9. Any help would be greatly appreciated! For reference, this is a production service.