Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

│ Error: Reference to undeclared resource

I am new to terraform and trying to make an instance of AWS (t2.nano) by the image below. this is my tf file:

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"
  }
}

When I run the command terraform plan, its produces the following error:

$ 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.
╵

if someone can help me fix it , i will be so happy.

like image 402
Orly Orly Avatar asked Jul 11 '21 09:07

Orly Orly


1 Answers

It should be:

  vpc_security_group_ids = [
    aws_security_group.group_web.id
  ]

as your aws_security_group is called group_web, not prod_web.

like image 163
Marcin Avatar answered Oct 22 '22 06:10

Marcin