Terraform "Self-referential block" / Resource Cannot Reference Itself in CI
A resource references its own not-yet-known attributes inside its own block. Terraform cannot compute a value that depends on itself, so it rejects the self-reference.
What this error means
plan fails with "Self-referential block", pointing at an argument that reads self-derived or own-resource attributes within the same resource. The value would have to exist before it is created, which is impossible.
Error: Self-referential block
on main.tf line 9, in resource "aws_security_group" "web":
9: description = "sg ${aws_security_group.web.id}"
Configuration for aws_security_group.web may not refer to itself.Common causes
Resource references its own computed attribute
Reading aws_x.this.id (or another computed attribute) inside the same aws_x.this block creates an impossible dependency - the value is unknown until the resource exists.
Misused self in the wrong context
The self object is only valid in provisioner/connection blocks. Referencing a resource’s own attributes elsewhere in its definition is a self-reference.
How to fix it
Use static inputs instead of own attributes
Derive the value from a variable, local, or name rather than the resource’s own computed attribute.
resource "aws_security_group" "web" {
name = "web"
description = "sg for ${var.app_name}" # not its own id
}Reference own attributes only where allowed
- Use
self.<attr>only insideprovisioner/connectionblocks, where it is valid. - For values needed elsewhere, compute them from inputs/locals, not from the resource itself.
- Split into a second resource or a
datasource if you truly need the created value.
How to prevent it
- Build resource arguments from variables/locals, not the resource’s own computed attributes.
- Reserve
selffor provisioner and connection blocks. - Run
terraform validateto catch self-references before apply.