Search code examples
amazon-web-servicesterraformterraform-provider-aws

Terraform AWS ..An argument named "tags" is not expected here


I can write bit terraform and ignored modules at first. Now I am trying to learn them, but it's 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 this error:

Error: Unsupported argument │ on ec2module.tf line 7, in module "prod": 7: tags = { │ An argument named "tags" is not expected here.

I don't 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 tag in module: Why can't add tags into terraform modules

Any help would be appreciated.


Solution

  • 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.