我正在尝试使用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]
}
在分配vpc_id:时有一个拼写错误
vpc_id = "${aws_vpc.terrafom-elb.id}"
应该是:
vpc_id = "${aws_vpc.terraform-elb.id}"
注意单词"地形"中缺少的"r">
您可以在顶部添加一个data
结构,并将VPC ID作为变量传递:
data "aws_vpc" "selected" {
id = var.vpc_id
}
并将其称为vpc_id = data.aws_vpc.selected.id