Terraform-根模块中尚未声明托管资源

时间:2020-03-19 17:22:55

标签: terraform terraform-provider-aws

我正在尝试使用terraform创建ec2实例并设置负载均衡器,但是我正面临以下错误。如何在单个main.tf文件中创建实例并配置负载平衡器?

错误:对未声明资源的引用

“ aws_lb_target_group”“前端”:27:vpc_id =“ $ {aws_vpc.terrafom-elb.id}” 根中尚未声明托管资源“ aws_vpc”“ terrafom-elb” module.source`

代码:

  region = "us-east-1"
  access_key = "*********************"
  secret_key = "**********************"
}

resource "aws_instance" "terraform" {
  ami           = "ami-07ebfd5b3428b6f4d"
  instance_type = "t2.micro"
  security_groups    = ["nodejs","default"]
  tags = {
    Name = "terrafom-elb"
  }
}

resource "aws_lb" "front-end"{
  name = "front-end-lb"
  internal = false
  security_groups    = ["nodejs"]

}

resource "aws_lb_target_group" "front-end" {
  name     = "front-end"
  port     = 8989
  protocol = "HTTP"
  vpc_id   = "${aws_vpc.terrafom-elb.id}"
  depends_on = [aws_instance.terraform]
}

2 个答案:

答案 0 :(得分:1)

您分配 vpc_id 的位置有误:

vpc_id   = "${aws_vpc.terrafom-elb.id}"

应该是:

vpc_id   = "${aws_vpc.terraform-elb.id}"

注意单词“terraform”中缺少的“r”

答案 1 :(得分:0)

您可以在顶部添加data结构,并将VPC ID作为变量传递:

data "aws_vpc" "selected" {
  id = var.vpc_id
}

并将其引用为vpc_id = data.aws_vpc.selected.id