Noor Ul Ain Butt
Noor Ul Ain Butt

Reputation: 129

Reference to undeclared resource in Terraform

I am trying to run a test server on aws using terraform. When i run terraform apply it throws an error saying Reference to undeclared resource. Below is my test server file inside terraform.

test-server.tf

module "test-server" {
  source = "./node-server"
  ami-id = "Here ive given my ami_id"
  key-pair = aws_key_pair.microservices-demo-key.key_name
  name = "Test Server"
}

Below is my key pair file code.

key-pairs

resource "aws_key_pair" "microservcies-demo-key" {
  key_name = "microservices-demo-key"
  public_key = file("./microservices_demo.pem")
}

Error detail thrown by terraform:

Error: Reference to undeclared resource

on test-server.tf line 4, in module "test-server":

4: key-pair = aws_key_pair.microservices-demo-key.key_name

A managed resource "aws_key_pair" "microservices-demo-key" has not been declared in the root module.

Although ive declard the variables. Its still throwing the error. This is the image of the directory.

enter image description here

Upvotes: 2

Views: 2689

Answers (2)

Be_developer
Be_developer

Reputation: 1

I had the same problem, and that how I landed here but unfortunately i didnt get any response. So I tried resolving this problem on my own and instead accessing the key with Argument I tried to access this with value and I would suggest you the same. You can try key_name = "microservices-demo-key" rather key-pair = aws_key_pair.microservices-demo-key.key_name

Upvotes: 0

jordanm
jordanm

Reputation: 34934

You have a typo here:

resource "aws_key_pair" "microservcies-demo-key" {

Fix this name to be microservices-demo-key so that it matches the name you reference in test-server.tf.

Upvotes: 1

Related Questions