0
votes
Error: Unsupported argument

  on main.tf line 3, in module "ec2_test":
   3:   ec2_count = 2

An argument named "ec2_count" is not expected here.


Error: Unsupported argument

  on main.tf line 4, in module "ec2_test":
   4:   ami_id = "ami-123"

An argument named "ami_id" is not expected here.


Error: Unsupported argument

  on main.tf line 5, in module "ec2_test":
   5:   instance_type = "t2.micro"

An argument named "instance_type" is not expected here.

The same module is working fine when I use it from local. The problem arises when I try to use the modules uploaded to bitbucket and give the URL as source in main.tf. Terraform init works fine and it cloned the repo to .terraform/modules/ec2 but on Terraform apply it gives the above error.

Below mentioned are the module for ec2 and respective var file from the Bitbucket private-repo instance.tf:

provider "aws"{
  region = var.region
}

resource "aws_instance" "ec2" {
  count = var.ec2_count
  ami           = var.ami_id
  instance_type = var.instance_type
  subnet_id = var.subnet_id
  vpc_security_group_ids = var.security_grp

  tags = {
    Name = "Terraform_module_test"
  }
}

var.tf

variable "ec2_count" {
 default = "1"
}

variable "ami_id" {}

variable "instance_type" {
 default = "t2.micro"
}

variable "subnet_id" {}

variable "security_grp" {}

variable "region" {}

The main.tf from which the modules are accessed

module "ec2_test" {
  source = "[email protected]:private-repo/terraform.git/modules_terraform/ec2"
  ec2_count = 2
  ami_id = "ami-123"
  instance_type = "t2.micro"
  subnet_id = "subnet-123"
  security_grp = ["sg-123"]
  region = "ap-southeast-1"
}
1
Show us the code for the module and how you're calling/instantiating it. It's difficult to provide help without a minimal reproducible example.pijemcolu
@pijemcolu can you help now?coder here

1 Answers

1
votes

Several of Terraform's supported module sources have to make a distinction between the repository or package the source code will come from and the path within that repository or package. The Git source is an example of that, because the Git protocol requires first retrieving the entire repository (using git clone) and only then accessing the subdirectory from the local work tree.

The module sources documentation discusses the syntax for that in Modules in Package Sub-directories:

When the source of a module is a version control repository or archive file (generically, a "package"), the module itself may be in a sub-directory relative to the root of the package.

A special double-slash syntax is interpreted by Terraform to indicate that the remaining path after that point is a sub-directory within the package.

Applying that your example, you must separate the repository path from the subdirectory path using the // marker:

  source = "[email protected]:private-repo/terraform.git//modules_terraform/ec2"

The above should cause Terraform to first clone the repository part, before the //:

git clone [email protected]:private-repo/terraform.git

...and then load the module from the modules_terraform/ec2 subdirectory of the resulting git work tree.