use data when a resource exists, otherwise create a new one

Viewed 1720

terraform has resource and data. resource creates a new thing, while data refers to an existing thing.

Is it possible to do the following? 1) if a resource does not exit, create a new one by 'resource' 2) if it exists, refer to it by 'data'

For example,

# if not exists
   resource "a" "b" { name = ...}
# else 
   data "a" "b" {name = ...}

resource "c" "d" {
   field = "${element(concat(a.b.*.name, data.a.b.*.name), 0)}"
}
1 Answers

This blog post describes a way to accomplish what you're trying to do by using a variable of type object with the minimum required (shared) attributes and passing that in.

Related