gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 23:08:25 24 4
gpt4 key购买 nike

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

Error: Reference to undeclared resource

"aws_lb_target_group" "front-end":27: vpc_id = "${aws_vpc.terrafom-elb.id}"A managed resource "aws_vpc" "terrafom-elb" has not been declared in the rootmodule.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]
}

最佳答案

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

data "aws_vpc" "selected" {
id = var.vpc_id
}
并将其引用为 vpc_id = data.aws_vpc.selected.id

关于Terraform - 未在根模块中声明托管资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60762152/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com