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
In your
variables.tf
for the module, you are missingtags
variable. You have to add:Otherwise you can’t pass it them into the module, the way you are trying to do now.
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:
tags
argument frommodule.prod
as it’s not actually being usedec2_instance
resource to accept something liketags = var.tags
instead ofname = var.instance_name