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 typeAWS::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.Workflow YAML instructions
Workflow YAML instructions
To upload the Workflow example YAML into your workspace:
- Save the Workflow example YAML file below:
- 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>
User input
User input
This block prompts the user to confirm that they want to destroy the resource.
- Click + in the center of the page. In the block library modal, choose User input.
- In the block configuration side panel, enter a name and unique slug for this block.
- In this example, we use the name
Are you sureand the slugare-you-sure.
- In this example, we use the name
- 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
Falseto prevent accidental deletion. - Click Add input.
- Save the block.
Branch
Branch
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.
- Click + in the center of the page. In the block library modal, choose Branch.
- In the block configuration side panel, enter a name and unique slug for this block.
- In this example, we use the name
Branchand the slugbranch.
- In this example, we use the name
- 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- Click + under the new path. In the block library modal, choose Data transformation.
- In the block configuration side panel, enter a name and unique slug for this block.
- In this example, we use the name
Get descriptorand the slugget-descriptor.
- In this example, we use the name
- Enter a jq expression:
- Save the block.
- Click + under the path. In the block library modal, choose Data transformation.
-
In the block configuration side panel, enter a name and unique slug for this block. In this example, we use the name
Get workspace IDand the slugget-workspace-id. -
Enter a jq expression:
- Save the block.
- Click + under the new path. In the block library modal, choose HTTP request.
- In the block configuration side panel, enter a name and unique slug for this block.
- In this example, we use the name
Terraform destroyand the slugterraform-destroy.
- In this example, we use the name
- 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+jsonAuthorization:Bearer {{ context.secrets.tf_token }}
- Payload: In our example, we enter the following:
- Save the block.
- Click + under the new path. In the block library modal, choose Cortex > Delete entity.
- In the block configuration side panel, enter a name and unique slug for this block.
- In this example, we use the name
Delete entityand the slugdelete-entity.
- In this example, we use the name
- 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.