I initialized terraform after config the terraform cloud in main.tf
:
terraform {
cloud {
organization = "my-org"
workspaces {
name = "learn-terraform-aws"
}
}
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.16"
}
}
required_version = ">= 1.2.0"
}
provider "aws" {
region = "ap-northeast-1"
}
resource "aws_instance" "app_server" {
ami = "ami-06cb72d48e1d7b1ee"
instance_type = "t2.micro"
tags = {
Name = var.instance_name
}
}
And set variables in variables.tf
:
variable "instance_name" {
description = "Value of the Name tag for the EC2 instance"
type = string
default = "ExampleAppServerInstance"
}
Run init:
terraform init
But when run terraform plan
or terraform apply
, got this error:
Running apply in Terraform Cloud. Output will stream here. Pressing Ctrl-C
will cancel the remote apply if it's still pending. If the apply started it
will stop streaming the logs, but will not stop the apply running remotely.
Preparing the remote apply...
To view this run in a browser, visit:
https://app.terraform.io/app/my-org/learn-terraform-aws/runs/run-Tidmx8BS2hjDTqm9
Waiting for the plan to start...
Terraform v1.5.7
on linux_amd64
Initializing plugins and modules...
╷
│ Error: Reference to undeclared input variable
│
│ on main.tf line 28, in resource "aws_instance" "app_server":
│ 28: Name = var.instance_name
│
│ An input variable with the name "instance_name" has not been declared. This
│ variable can be declared with a variable "instance_name" {} block.
╵
Operation failed: failed running terraform plan (exit 1)
Why can’t use variables values in the same local directory in Terraform cloud?
By the way, even I set the instance_name
value in Terraform cloud’s workspace variables as Terraform variable
, still got the same error.