Terraform:使用局部变量

Use*_*ice 3 terraform

我试图了解: - 如何使用此处定义的本地人。

所以,我有一个这样的目录结构:my-example/modules/test/security-groups/main.tf vpc/main.tf

代码my-examples/modules/test/vpc/main.tf

variable "vpc_name" {
  default = "Test"
}

resource "aws_vpc" "test_vpc" {
  cidr_block            = "172.31.0.0/16"
  enable_dns_support    = true
  enable_dns_hostnames  = true

  tags {
    Name = "${var.vpc_name}:VPC"
    Environment = "${var.vpc_name}"
  }
}

locals {
  id_vpc = "${aws_vpc.test_vpc.id}"
}

module "security_groups" {
  source = "../security-groups"
  id_vpc = "${local.id_vpc}"
}
Run Code Online (Sandbox Code Playgroud)

我们的想法是能够使用id_vpcmy-examples/modules/security-group/main.tf像这样:

resource "aws_security_group" "bastion_sg" {
  vpc_id = id_vpc
  name = "Bastion-SG"

  ingress {
    from_port = 22
    to_port = 22
    protocol = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }

}
Run Code Online (Sandbox Code Playgroud)

然而,我不断收到此错误:错误:

  * 1 error(s) occurred:

  * module root: module security_groups: id_vpc is not a valid parameter
Run Code Online (Sandbox Code Playgroud)

有人可以向我解释为什么不评估局部变量吗?将不胜感激。谢谢你。

BMW*_*BMW 5

模块中的名称security_groups看起来不错。但是您确实对模块中的代码有问题security_groups

请更换

resource "aws_security_group" "bastion_sg" {
  vpc_id = id_vpc
  name = "Bastion-SG"
  ...
}
Run Code Online (Sandbox Code Playgroud)

resource "aws_security_group" "bastion_sg" {
   vpc_id = ${var.id_vpc}
  name = "Bastion-SG"
  ...
}
Run Code Online (Sandbox Code Playgroud)

id_vpc在模块中定义变量。