skip to Main Content

I can write bit terraform and ignored modules at first,now i am trying to learn them..but its not going well..

Here is my module code

resource "aws_instance" "ec2_instance" {

  ami           = var.ami
  instance_type = var.instance_type
  tags = {
    "Name" = var.instance_name
  }

}

here is my variables.tf file


variable "instance_name" {
  description = "Name of the EC2 instance"
}

variable "instance_type" {
  description = "Type of the EC2 instance"
}

variable "ami" {
  description = "amiid"
}

I am calling this like below

module "prod" {
  source        = "./modules/ec2-instance"
  ami           = "ami-08a52ddb321b32a8c"
  instance_type = "t2.micro"
  instance_name = "hellll"

  tags = {
    Name = "helloo"
  }

}

I get below error…

Error: Unsupported argument

on ec2module.tf line 7, in module "prod":
7: tags = {

An argument named "tags" is not expected here.

I am not able to understand why i am getting this error…The same tags works in just AWS_instance resource


resource "aws_instance" "name" {
  
  ami = "ami-08a52ddb321b32a8c"
  instance_type = "t2.micro"

  tags = {
    "Name" = "dev"
  }
}

But why is this not working in a module, am i missing some thing..Based on this answer,i have even exposed this tags in module..https://stackoverflow.com/questions/61934062/why-cant-add-tags-into-terraform-modules

Any help would be appreciated

2

Answers


  1. In your variables.tf for the module, you are missing tags variable. You have to add:

    variable "tags" {
      description = "Tags for the instance"
    }
    

    Otherwise you can’t pass it them into the module, the way you are trying to do now.

    Login or Signup to reply.
  2. There’s two issues here.

    The first issue is as Marcin mentioned, you forgot to declare the variable tags. However the way you’ve written your module, you’re statically defining tags as name = var.instance_name

    Therefore, your two options are:

    1. Remove the tags argument from module.prod as it’s not actually being used
    2. Change your ec2_instance resource to accept something like tags = var.tags instead of name = var.instance_name
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search