使用 bitbucket 作为源时,Terraform 无法从模块中获取参数
Terraform not able to fetch arguments from modules when using bitbucket as source
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.
当我从本地使用同一个模块时,它工作正常。当我尝试使用上传到 bitbucket 的模块并将 URL 作为 main.tf 中的源时,问题就出现了。 Terraform init 工作正常,它将 repo 克隆到 .terraform/modules/ec2
但在 Terraform 上应用它给出了上述错误。
下面提到的是 ec2 的模块和来自 Bitbucket private-repo 的相应 var 文件
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" {}
从中访问模块的 main.tf
module "ec2_test" {
source = "git@bitbucket.org: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"
}
几个 Terraform's supported module sources 必须区分源代码将来自的存储库或包以及该存储库或包中的路径。 Git 源就是一个例子,因为 Git 协议要求首先检索整个存储库(使用 git clone
),然后才从本地工作树访问子目录。
模块源文档讨论了 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.
应用您的示例,您必须使用 //
标记将存储库路径与子目录路径分开:
source = "git@bitbucket.org:private-repo/terraform.git//modules_terraform/ec2"
以上应该会导致 Terraform 在 //
:
之前首先克隆存储库部分
git clone git@bitbucket.org:private-repo/terraform.git
...然后从生成的 git 工作树的 modules_terraform/ec2
子目录加载模块。
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.
当我从本地使用同一个模块时,它工作正常。当我尝试使用上传到 bitbucket 的模块并将 URL 作为 main.tf 中的源时,问题就出现了。 Terraform init 工作正常,它将 repo 克隆到 .terraform/modules/ec2
但在 Terraform 上应用它给出了上述错误。
下面提到的是 ec2 的模块和来自 Bitbucket private-repo 的相应 var 文件 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" {}
从中访问模块的 main.tf
module "ec2_test" {
source = "git@bitbucket.org: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"
}
几个 Terraform's supported module sources 必须区分源代码将来自的存储库或包以及该存储库或包中的路径。 Git 源就是一个例子,因为 Git 协议要求首先检索整个存储库(使用 git clone
),然后才从本地工作树访问子目录。
模块源文档讨论了 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.
应用您的示例,您必须使用 //
标记将存储库路径与子目录路径分开:
source = "git@bitbucket.org:private-repo/terraform.git//modules_terraform/ec2"
以上应该会导致 Terraform 在 //
:
git clone git@bitbucket.org:private-repo/terraform.git
...然后从生成的 git 工作树的 modules_terraform/ec2
子目录加载模块。