│ Error: Reference to undeclared resource

This is my code anyone can help to resolve this error
provider “aws” {
region = “us-east-1”
access_key = “"
secret_key = "
************”
}
resource “aws_instance” “terraform” {
ami = “ami-0866a3c8686eaeeba”
instance_type = “t2.micro”
name = “terraform”
}

resource “aws_eip” “terraform_eip” {
vpc = true
}
resource “aws_eip_association” “eip_assoc” {
instance_id = aws_instance.terraform.id
allocation_id = aws_eip.terraform_eip.id
}
resource “aws_security_group” “allow_ip” {
name = “sg_terraform”

ingress{
    from_port = 443
    to_port = 443
    protocol = "tcp"
    cidr_blocks = ["${aws_eip.terraform_eip.public_ip/}"]
}

}

ubuntu@SW-VM-302:~/tfdemo$ terraform plan

│ Warning: Argument is deprecated

│ with aws_eip.terraform_eip,
│ on ec2witheip.tf line 12, in resource “aws_eip” “terraform_eip”:
│ 12: vpc = true

│ use domain attribute instead


│ Error: Reference to undeclared resource

│ on ec2witheip.tf line 15, in resource “aws_eip_association” “eip_assoc”:
│ 15: instance_id = aws_instance.terraform.id

│ A managed resource “aws_instance” “terraform” has not been declared in the root module.

Hi @karthickselvam2,

It would help others if you formatted the configuration such that we could read it without the forum displaying it as markdown. There may be other things hidden in the lost formatting, but I can at least see that this expression in invalid:

["${aws_eip.terraform_eip.public_ip/}"]

If you were trying to format that as the ip with a slash, then the / character should be outside of the brackets and part of the surrounding string:

["${aws_eip.terraform_eip.public_ip}/"]

(Also make sure you’ve rotated that key and secret if they were real, removing it from display does not remove it from the history of being posted on a public forum.)

1 Like