Accessing to the current type of operation

Hello,

Terraform cloud exposes some variables, like TFC_WORKSPACE_NAME. In the same spirit, is there a way to access to the current run type? Runs - API Docs - Terraform Cloud | Terraform by HashiCorp

I would like to configure some stuff differently if it’s a speculative plan or not.

Thx

No, there is no supported way to access the current run type.

I’m curious, what is it that you want to do differently for a speculative plan? The purpose of speculative plans is to give an indication of what would happen if the changes were applied to the workspace, so configuring Terraform differently in that context would seem to reduce their usefulness.

Let me explain. Sorry, it will be a little bit long.

I want to implement some compliance using terraform.

What do I mean by compliance? Be sure that what I have in production is what I have in terraform, and nothing more.

How do I do this? With datasources and this provider Terraform Registry to compare datasources and what I’m expecting.

Why using terraform to do this? Because almost everything I need is already implemented in terraform, I do not want to have another custom script. Yes, terraform is not really designed for this.

Back to the initial question. Why do I want to access to the run type. Because I would like to launch “compliance” run during which the assert plugin above will fail or send an SNS notifications.
During standard run (not speculative), I do not want to do the compliance part.

So what I need is a way to launch a run TFE, and pass some information to the run to tune the assert plugin. But most of our workspaces are in VCS mode, so I cannot pass any variable :slight_smile: .

The only remaining solution I see is to create a branch, launch a speculative run on this branch and do the config of the assert plugin with TFC_CONFIGURATION_VERSION_GIT_BRANCH.

If my explanations are clear and if you have a better idea, I will be really happy to hear it :slight_smile:

Thx

Ah, I understand better now, thanks!

I think what you describe is the best workaround available at the moment. From Terraform’s point of view, there is nothing special about a speculative plan: it’s just a terraform plan operation either way.

While the Terraform Cloud run does have a plan-only attribute, there isn’t any way to fetch that information without making an API call. The TFE provider doesn’t have a data source for runs, so that isn’t an option.

One thing I would suggest considering for the future is the Terraform 1.2 and 1.3 support for postcondition checks. These are checked by Terraform Cloud’s continuous validation system, which would save you from having to trigger these speculative plans manually. While postcondition checks currently cannot be disabled for standard runs, that is something that we may consider changing in the future.

1 Like