【问题标题】:│ Error: Reference to undeclared resource│ 错误:引用未声明的资源
【发布时间】:2021-09-20 21:16:01
【问题描述】:

我是 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.
╵

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

【问题讨论】:

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


    【解决方案1】:

    应该是:

      vpc_security_group_ids = [
        aws_security_group.group_web.id
      ]
    

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

    【讨论】:

      猜你喜欢
      • 2021-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-20
      • 2021-12-23
      • 2018-11-28
      • 2019-08-04
      • 1970-01-01
      相关资源
      最近更新 更多