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

时间:2021-07-11 09:27:40

标签: amazon-web-services amazon-ec2 terraform terraform-provider-aws

我是 terraform 的新手,正在尝试通过下图制作 AWS (t2.nano) 实例。 这是我的 tf 文件:

provider "aws" {
  profile = "default"
  region  = "us-west-2"
}

resource "aws_s3_bucket" "prod_tf_course" {
  bucket = "tf-course-20210607"
  acl    = "private"
}

resource "aws_default_vpc" "default" {}

resource "aws_security_group" "group_web"{
  name = "prod_web"
  description = "allow standard http and https ports inbound and everithing outbound"

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

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

  egress{
    from_port = 0
    to_port = 0
    protocol = "-1"
    cidr_blocks = ["0.0.0.0/0"]
  
  }
  tags = {
    "Terraform" : "true"
  }

}

resource "aws_instance" "prod_web"{
  ami = "ami-05105e44227712eb6"
  instance_type ="t2.nano"

  vpc_security_group_ids = [
    aws_security_group.prod_web.id
  ]

  tags = {
    "Terraform" : "true"
  }
}

当我运行命令 terraform plan 时,它产生以下错误:

$ terraform plan
╷
│ Error: Reference to undeclared resource
│
│   on prod.tf line 50, in resource "aws_instance" "prod_web":
│   50:     aws_security_group.prod_web.id
│
│ A managed resource "aws_security_group" "prod_web" has not been declared in
│ the root module.
╵

如果有人能帮我解决它,我会很高兴。

1 个答案:

答案 0 :(得分:4)

应该是:

  vpc_security_group_ids = [
    aws_security_group.group_web.id
  ]

因为您的 aws_security_group 被称为 group_web,而不是 prod_web

相关问题