Use "count" and "splat syntax".
"splat syntax" is "*" in front of variable. (e.g. shared_resource.example.*.id
)
When using this syntax, terraform doesn't get an error using resource that is not created because of count:0.
I think, this is like a bug.
For example
In the first step, CI job uses development
workspace, then the shared resource is created. In the following example, a aws_api_gateway_rest_api resource named "example" is created.
Next, using production
workspace, the resource is not created but terraform read the data of "aws_api_gateway_rest_api.example".
# this resource is created when using development workspace
resource "aws_api_gateway_rest_api" "example" {
count = "${terraform.workspace == "development" ? 1 : 0}"
name = "this is example"
}
data "aws_api_gateway_rest_api" "example" {
count = "${terraform.workspace == "production" ? 1 : 0}"
name = "this is example"
}
Using the resource, you must specify the resource value as following.
resource "sample_resource" "example" {
rest_api_id = "${terraform.workspace == "production" ? join("", data.aws_api_gateway_rest_api.example.*.id) : join("", aws_api_gateway_rest_api.example.*.id)}"
}