Home > Software design >  Terraform module - error No changes. Your infrastructure matches the configuration
Terraform module - error No changes. Your infrastructure matches the configuration

Time:06-05

I'm trying to experiment with the terraform modules and created a very simple one. Structure as follows.

├───dev
│       dev.tfvars
│       main.tf
│       output.tf
│       variables.tf
│
└───modules
    └───instances
            main.tf
            output.tf
            variables.tf
            versions.tf

terraform init
PS C:\Users\61421\github\IAC-TF\dev> terraform init
Initializing modules...
- ec2_create in ..\modules\instances

Initializing the backend...

Initializing provider plugins...
- Finding hashicorp/aws versions matching "~> 4.17.1"...
- Installing hashicorp/aws v4.17.1...
- Installed hashicorp/aws v4.17.1 (signed by HashiCorp)

terraform plan
PS C:\Users\61421\github\IAC-TF\dev> terraform plan

No changes. Your infrastructure matches the configuration.

I'm executing those commands from my DEV(i'm planning to build prod in a different dir).

dev.tfvars
instance_type    = "t2.micro"
instance_keypair = "user001"
no_of_instances  = 1
ami_id           = "ami-0c6120f461d6b39e9"


main.tf
provider "aws" {
  region = "ap-southeast-2"
}
module "ec2_create" {
  source        = "../modules/instances"
  ami           = var.ami_id
  instance_type = var.instance_type
  count         = var.no_of_instances

}

variables.tf
variable "no_of_instances" {
  description = "Number of Instances"
  type        = number
  default     = 0
}

variable "ami_id" {
  description = "ami_id"
  type        = string
  default     = ""
}

variable "instance_type" {
  description = "instance_type"
  type        = string
  default     = ""
}

my sample module as follows.

main.tf
resource "aws_instance" "web" {
  ami = var.ami
  instance_type = var.instance_type
  count = var.no_of_instances
}

variables.tf
variable "ami" {
  description = "ID of AMI to use for the instance"
  type        = string
  default     = ""
}

variable "instance_type" {
  description = "instance_type of AMI to use for the instance"
  type        = string
  default     = ""
}

variable "no_of_instances" {
  description = "Number of Instances"
  type        = number
  default     = 1
}

versions.tf
terraform {
  required_version = "~> 1.1.7" # which means any version equal & above 4.17.1 like 4.17.2
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 4.17.1"
    }
  }

looks like nothing created, i tried the same resource aws_instance with a one file without module and it created. Looks like i'm missing something when call the module. appreciate your help on this. I'm new to terraform.

CodePudding user response:

This happens most likely because your no_of_instances is 0 by default. Thus there is no resources to be created.

To use your dev.tfvars you have to explicitly pass this file:

terraform plan -var-file=dev.tfvars
  • Related