Skip to main content
Using a Workflow, you can streamline the process of dismantling infrastructure that was previously provisioned and managed by a Terraform configuration. This Workflow will also remove the resource entity from your Cortex workspace.

How to use a Workflow in Cortex to destroy a resource with Terraform

Prerequisite

Before getting started, you should have already followed the guide to Provision an EC2 instance with Terraform.

Considerations

  • This Workflow makes HTTP calls to Terraform Cloud, but it is possible to call other Terraform orchestrators, such as Spacelift.
  • The Terraform orchestrator you use must have an API call to perform the destroy.

Step 1: Start creating the Workflow

Follow the steps in the documentation to create a Workflow and configure its basic settings. In the Workflow’s basic settings, set its scope to apply to the entity type AWS::EC2::Instance and the group terraform. The prerequisite Workflow Provision an EC2 instance with Terraform automatically adds the group to the entities it creates.

Step 2: Add blocks to the Workflow

The instructions on this page describe how to create this Workflow in the Cortex UI, but it is also possible to copy the Workflow YAML and add it to your workspace via the Cortex CLI. This allows you to quickly set up the example configuration then iterate on it for your own use case. Expand the tile below to learn more.
To upload the Workflow example YAML into your workspace:
  1. Save the Workflow example YAML file below:
  1. Use the Cortex CLI to run this command, using the path to your Workflow YAML file:
    cortex workflows create -f <path-to-your-workflow.yaml>
Expand the tiles below to learn about each block in this Workflow and how to configure them in the Cortex UI:
This block prompts the user to confirm that they want to destroy the resource.
  1. Click + in the center of the page. In the block library modal, choose User input.
  2. In the block configuration side panel, enter a name and unique slug for this block.
    • In this example, we use the name Are you sure and the slug are-you-sure.
  3. Click +Add user input. Add the following:
    • Name: Are you sure? This will also delete it from the catalog
    • Key: are-you-sure
    • Type: Toggle
    • Default value: This is set to False to prevent accidental deletion.
    • Click Add input.
  4. Save the block.
During the previous User input step, if the user selects “Yes” when prompted, the Workflow will branch into a set of blocks to destroy the resource. If the user does not select yes, then the Workflow will not continue.
  1. Click + in the center of the page. In the block library modal, choose Branch.
  2. In the block configuration side panel, enter a name and unique slug for this block.
    • In this example, we use the name Branch and the slug branch.
  3. Configure the conditional path:
    • Name: Yes
    • Slug: yes
    • Path expression: actions["are-you-sure"].outputs["are-you-sure"] == true

Add blocks to the branched path

The blocks in the path will get the entity descriptor and workspace ID, send an HTTP request to Terraform to destroy the resource, and delete the entity in your Cortex workspace.Data transformations
  1. Click + under the new path. In the block library modal, choose Data transformation.
  2. In the block configuration side panel, enter a name and unique slug for this block.
    • In this example, we use the name Get descriptor and the slug get-descriptor.
  3. Enter a jq expression:
  1. Save the block.
  2. Click + under the path. In the block library modal, choose Data transformation.
  3. In the block configuration side panel, enter a name and unique slug for this block. In this example, we use the name Get workspace ID and the slug get-workspace-id.
  4. Enter a jq expression:
  5. Save the block.
HTTP request
  1. Click + under the new path. In the block library modal, choose HTTP request.
  2. In the block configuration side panel, enter a name and unique slug for this block.
    • In this example, we use the name Terraform destroy and the slug terraform-destroy.
  3. Configure the block:
    • HTTP method: POST
    • URL: In our example, we set this to:
      https://app.terraform.io/api/v2/runs
    • Headers: Set the following headers:
      • Content-Type: application/vnd.api+json
      • Authorization: Bearer {{ context.secrets.tf_token }}
    • Payload: In our example, we enter the following:
  1. Save the block.
Delete entity
  1. Click + under the new path. In the block library modal, choose Cortex > Delete entity.
  2. In the block configuration side panel, enter a name and unique slug for this block.
    1. In this example, we use the name Delete entity and the slug delete-entity.
  3. Under Entity ID or tag, enter {{context.entity.tag}}.

Step 3: Run the Workflow

When you run the Workflow, the following events happen:
  • The User input step prompts the user to confirm that they want to delete a resource.
  • If the user confirms during the first block, then the branch block runs.
  • The Workflow obtains the entity descriptor, then uses that information to obtain the related workspace ID.
  • The HTTP request triggers a destroy in Terraform.
  • The “Delete entity” step uses the entity descriptor obtained in the previous steps to determine which entity to delete.
Last modified on September 28, 2026