本文主要是介绍Terraform模块元参数,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
在Terraform 0.13之前,模块在使用上存在一些限制。
例如通过模块来创建EC2主机,可以这样:
module "ec2_instance" {source = "terraform-aws-modules/ec2-instance/aws"version = "~> 3.0"name = "single-instance"ami = "ami-ebd02392"instance_type = "t2.micro"key_name = "user1"monitoring = truevpc_security_group_ids = ["sg-12345678"]subnet_id = "subnet-eddcdzz4"tags = {Terraform = "true"Environment = "dev"}
}
如果要创建两台这样的主机怎么办?在Terraform 0.13之前的版本中,由于Module不支持元参数,所以只能手动拷贝模块代码:
module "ec2_instance_0" {source = "terraform-aws-modules/ec2-instance/aws"version = "~> 3.0"name = "single-instance-0"ami = "ami-ebd02392"instance_type = "t2.micro"key_name = "user1"monitoring = truevpc_security_group_ids = ["sg-12345678"]subnet_id = "subnet-eddcdzz4"tags = {Terraform = "true"Environment = "dev"}
}module "ec2_instance_1" {source = "terraform-aws-modules/ec2-instance/aws"version = "~> 3.0"name = "single-instance-1"ami = "ami-ebd02392"instance_type = "t2.micro"key_name = "user1"monitoring = truevpc_security_group_ids = ["sg-12345678"]subnet_id = "subnet-eddcdzz4"tags = {Terraform = "true"Environment = "dev"}
}
自从Terraform 0.13开始,模块也像资源一样,支持count
、for_each
、depends_on
三种元参数。
可以这样:
module "ec2_instance" {count = 2source = "terraform-aws-modules/ec2-instance/aws"version = "~> 3.0"name = "single-instance-${count.index}"ami = "ami-ebd02392"instance_type = "t2.micro"key_name = "user1"monitoring = truevpc_security_group_ids = ["sg-12345678"]subnet_id = "subnet-eddcdzz4"tags = {Terraform = "true"Environment = "dev"}
}
要注意的是Terraform 0.13之后在模块上声明depends_on
,列表中也可以传入另一个模块,声明depends_on
的模块中的所有资源的创建都会发生在被依赖的模块中所有资源创建完成之后。
这篇关于Terraform模块元参数的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!