You're getting the error:
An input variable with the name "project" has not been declared. This variable can be declared with a variable "project" {} block.
This means you're trying to use var.project somewhere in your Terraform code, but you haven't declared the project variable yet.
✅ Goal: Make Variables Global and Avoid This Error
To make your naming convention (org, project, environment, region) truly global and reusable across all modules/resources, follow this structure:
📁 File Structure
terraform/
├── main.tf
├── variables.tf
├── outputs.tf (optional)
└── terraform.tfvars (optional)
1. 🔧 Define Variables in variables.tf
# variables.tf
variable "org" {
description = "Organization name"
type = string
default = "apexit"
}
variable "project" {
description = "Project name"
type = string
default = "aican"
}
variable "environment" {
description = "Environment (dev, staging, prod)"
type = string
default = "dev"
}
variable "region" {
description = "AWS Region"
type = string
default = "eu-north-1"
}
Now Terraform knows what var.org, var.project, etc., mean everywhere in your config.
2. 🛠 Use Variables in main.tf
Example of using them for resource naming and tagging:
# main.tf
resource "aws_instance" "example" {
ami = "ami-0c55b159cbfafe1f0" # Replace with your AMI ID
instance_type = "t2.micro"
tags = {
Name = "${var.org}-${var.project}-${var.environment}-instance"
Org = var.org
Project = var.project
Environment = var.environment
Region = var.region
}
}
3. 📄 Optional: Override Defaults Using terraform.tfvars
If you want different values (e.g., environment = "prod"), create:
# terraform.tfvars
org = "apexit"
project = "aican"
environment = "prod"
region = "eu-west-1"
Terraform will automatically pick up these values when you run terraform apply.
🧪 Test It
Run:
terraform init
terraform plan
No more errors about undeclared variables!
🔁 Reuse Across Modules (Bonus)
If you’re using multiple modules, pass the variables down like this:
module "my_module" {
source = "./my-module"
org = var.org
project = var.project
environment = var.environment
region = var.region
}
And inside the module, define those variables again in its own variables.tf.
✅ Summary
| Step | Action |
|---|
| ✅ 1 | Create variables.tf and declare all four variables |
| ✅ 2 | Use var.org, var.project, etc., in your resources |
| ✅ 3 | Optionally override defaults in terraform.tfvars |
| ✅ 4 | Pass variables into modules if needed |
Let me know if you want to generate a full example repo or scaffold!