Skip to content
Latchkey

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.

terraform output
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.

main.tf
resource "aws_security_group" "web" {
  name        = "web"
  description = "sg for ${var.app_name}"   # not its own id
}

Reference own attributes only where allowed

  1. Use self.<attr> only inside provisioner/connection blocks, where it is valid.
  2. For values needed elsewhere, compute them from inputs/locals, not from the resource itself.
  3. Split into a second resource or a data source 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 self for provisioner and connection blocks.
  • Run terraform validate to catch self-references before apply.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →