IBM Developer

Tutorial

Automate Terraform updates from Turbonomic actions with AI

Build an AI-driven workflow that converts Turbonomic optimization actions into Terraform code updates and GitHub pull requests, keeping your infrastructure and IaC aligned

By Anita Kumari, Simon Ravenscroft, Joseph Mate

In modern cloud environments, teams often use multiple automation tools to manage infrastructure. Some tools optimize resource usage at runtime, while other tools manage infrastructure through infrastructure as code. Turbonomic and Terraform work together, but some optimization actions may still require a manual task: updating the Terraform code that defines the resource.

Imagine that Turbonomic recommends changing a Terraform-managed virtual machine to a more cost-efficient instance type. Approving the recommendation is only the first step. If Terraform code defines the virtual machine, the Terraform code must also be updated. Updating the Terraform code ensures that future Terraform deployments continue to use the new instance type.

This tutorial shows how AI can automate Terraform code updates in response to a Turbonomic optimization action. The process turns a manual update into a pull request that developers can review and manage through existing development workflows.

Use case: Keeping Terraform code aligned with Turbonomic actions

Turbonomic can identify opportunities to assure performance while improving efficiency and reducing cloud costs. For example, Turbonomic might recommend changing a production virtual machine from g1-small to e2-small while continuing to meet workload requirements.

Turbonomic savings action

Approving the optimization action updates the running infrastructure. However, if the virtual machine is managed by Terraform, the Terraform code must also be updated. Otherwise, a future Terraform deployment could restore the previous configuration and remove the optimization benefit.

Updating the Terraform code is not always straightforward. In a small Terraform project, the required change may be easy to find. In a larger codebase, configuration values can be spread across variables, modules, and reusable components.

For example, a small Terraform project might contain the instance type directly in the resource definition. Updating the instance type requires a simple change to the resource configuration.

In a larger Terraform codebase, the same value may be defined in a variables file and passed through multiple modules before reaching the resource. In that case, teams must trace the configuration through several files to find the correct location for the update.

Small Terraform Codebase Large Modular Terraform Codebase
resource "google_compute_instance" "app_vm" {
name = "prod-app-vm"
machine_type = "g1-small"
zone = "us-central1-a"
}

In this case, the update is obvious:

machine_type = "e2-small"
envs/prod/terraform.tfvars

app_vm_config = {
machine_type = "g1-small"
}

envs/prod/main.tf

module "application" {
source = "../../modules/application"
app_vm_config = var.app_vm_config
}

modules/application/main.tf

module "compute" {
source = "../compute"
machine_type = var.app_vm_config.machine_type
}

modules/compute/main.tf

resource "google_compute_instance" "app_vm" {
name = "prod-app-vm"
machine_type = var.machine_type
}

In this case, the update requires tracing through nested modules to reach terraform.tfvars.

machine_type = "e2-small"

Table. Comparison of a small Terraform codebase and a large modular Terraform codebase

As Terraform codebases grow, locating the correct configuration file becomes a manual and time-consuming task. Even a simple instance type change can require developers to trace values through several layers of Terraform modules.

Using AI to automate Terraform code updates

IBM Turbonomic already supports Terraform-managed environments. The Turbonomic Provider for Continuous Optimization helps infrastructure resources move toward an optimized state. The Terraform integration provides Terraform code context that helps identify where infrastructure resources are defined.

For many teams, these capabilities are sufficient. However, teams that manage infrastructure through Git-based Terraform workflows often need one additional step. After a Turbonomic action is approved, the corresponding Terraform code may need to be updated and submitted through the standard review and approval process.

To get the full benefit of infrastructure optimization, teams must update both the running infrastructure and the Terraform code that defines it.

A better approach is to automatically create a pull request whenever Turbonomic generates an optimization action. The pull request updates the Terraform code and keeps the infrastructure definition aligned with the deployed resource.

This approach helps teams:

  • Preserve cost savings from optimization actions
  • Keep Terraform code aligned with deployed infrastructure
  • Maintain a complete audit trail in Git
  • Follow existing code review and approval processes

This tutorial demonstrates one way to automate that step. By combining Turbonomic action details with Terraform code context, an AI assistant can identify the required Terraform code update, make the change in the repository, and create a pull request for developer review.

The result is a workflow that helps keep deployed infrastructure and Terraform code aligned while preserving existing Git-based development practices.

Architecture for AI-driven Terraform updates

To keep Terraform code aligned with infrastructure changes, this tutorial uses a lightweight workflow that connects Turbonomic optimization actions to Terraform code updates.

Architecture diagram showing Turbonomic webhook triggering AI-assisted Terraform pull request workflow

The workflow begins when Turbonomic executes an optimization action. A webhook configured in Turbonomic sends the action details to an HTTP middleware service. The payload contains the information required to process the change, including the resource identifier, the recommended target configuration, and the Terraform code context.

The middleware service acts as the orchestration layer. It processes the webhook payload and creates a structured prompt for IBM Bob Shell, the AI assistant used in this implementation. The prompt includes the required infrastructure change and the repository information needed to locate the relevant Terraform code.

IBM Bob Shell then performs the Terraform code update. It clones the Terraform repository, analyzes the Terraform module structure, identifies where the instance type is defined, and updates the correct configuration. In many codebases, the configuration value is defined several levels deep in nested modules.

After updating the Terraform code, IBM Bob Shell completes the Git workflow by:

  • Creating a branch
  • Committing the change with a descriptive commit message
  • Pushing the branch to the remote repository
  • Creating a pull request

For more information about IBM Bob Shell configuration and usage, see the IBM Bob Shell documentation.

Prerequisites

Before you begin, make sure that you have the required infrastructure, Turbonomic configuration, and tools.

Infrastructure

  • A Turbonomic instance (version 8.x or later) with webhook support.
    • Sign up for a free 30-day trial.
    • Log in to your Turbonomic instance and verify that you have access to the Settings panel. You will use the Settings panel to configure the webhook, virtual machine group, and automation policy described in this tutorial.
  • A Terraform repository hosted in GitHub or GitHub Enterprise.
  • A Linux server to host the HTTP service and AI assistant.

Turbonomic configuration

Tools and access

  • A Linux HTTP server
  • Git and GitHub CLI installed
  • An AI coding assistant, such as IBM Bob Shell
  • SSH keys configured for GitHub repository access
  • A GitHub personal access token with permission to create pull requests
  • IBM Bob Shell installed and configured. For installation instructions and configuration details, see the IBM Bob Shell documentation.
    • Configure Git access so IBM Bob Shell can clone repositories, create branches, commit changes, push changes to remote repositories, create pull requests.

Step 1. Configure the HTTP service and AI assistant

Before connecting Turbonomic to your automation workflow, prepare the server that will receive webhook events and perform Terraform code updates. This server hosts both the HTTP service and the AI assistant used to process optimization actions.

  1. Install an AI coding assistant on the server. This implementation uses IBM Bob Shell.

  2. Install Git on the server.

  3. Install a version control system (VCS) command-line tool, such as GitHub CLI, to interact with repositories and pull requests.

  4. Configure the required SSH keys and API tokens so the AI assistant can:

    • Clone repositories
    • Create branches
    • Commit changes
    • Create pull requests
  5. Create a lightweight HTTP service to receive webhook events from Turbonomic. You can use Node.js, Python, or another automation platform. This implementation uses Node.js Express.

    The HTTP service should:

    • Receive webhook payloads from Turbonomic
    • Extract optimization action details
    • Build prompts for the AI assistant
    • Invoke the AI assistant to perform the Terraform code update

    You can also use orchestration platforms such as IBM Concert Workflows, Node-RED, or similar tools instead of a custom HTTP service.

    IBM Bob Shell prompt used for Terraform code updates

    Figure. IBM Bob Shell prompt used for Terraform code updates

Step 2. Configure a Turbonomic webhook

The next step is to configure a webhook in Turbonomic. The webhook sends optimization action details to the HTTP service, which then triggers the AI-assisted Terraform update workflow.

  1. Log in to the Turbonomic user interface.
  2. Navigate to Settings → Workflows.
  3. On the Workflows page, click Create Workflow.
  4. In the Select Connection Type panel, select Webhook, and then click Next.

    Workflow connection type

  5. Configure the webhook details:

    • Display Name: Enter a descriptive name, such as Terraform PR Automation.
    • Description: Optional.
    • Address: Enter the URL of your HTTP service. For example, http://<http-server-ip>:<port>
    • HTTP Method: Select POST.
    • Validate Server Certificate: Keep this option enabled to verify the server identity and ensure secure communication. Disable it only when necessary and only in trusted environments.

      Webhook details

  6. Configure authentication:

    • Select None if your HTTP service does not require authentication.
    • Select Basic Authentication if your HTTP service requires credentials.
  7. Add a request header.

    • Content-Type: application/json

      Webhook content type

  8. Specify the request body.

    • Payload template: Use the payload template to define which fields from the ActionApiDTO and ActionDetailsApiDTO objects are included in the webhook payload.

    For example, the following field returns the time when the action was created:

    $action.createTime
    

    For a complete list of available fields, see the API Reference.

    • To include all action fields in the webhook payload, use the following template:
    converter.toJson($action)
    

    Webhook action payload details

  9. Save the workflow configuration.

Step 3. Create a virtual machine group

Before creating a workflow policy, create a virtual machine group that contains the virtual machines you want Turbonomic to manage. The group allows you to target specific virtual machines when defining automation policies and optimization actions.

  1. Verify that the target virtual machines are in an active state.
  2. In the Turbonomic user interface, navigate to Settings → Groups.
  3. Click New Group.
  4. Under Group Type, select Virtual Machine.

    Virtual machine group type

  5. Configure the group:

    • Name: Enter a name for the virtual machine group, for example, GCP VM Group.
    • Type: Select Static.
    • Members: Select the virtual machines that you want to include in the group.

    Virtual machine selection

  6. Click Create to create the virtual machine group.

Step 4. Create an automation policy for the webhook workflow

The automation policy determines which Turbonomic actions trigger the webhook workflow. In this example, the policy links optimization actions for a specific virtual machine group to the webhook that starts the Terraform code update process.

For detailed policy configuration instructions, see the Automation Workflow Policies Documentation.

  1. Navigate to Settings → Policies → Automation.

  2. Click New Automation Policy.

  3. Configure the policy:

    • Name: Enter a descriptive name, such as Terraform Sync Policy.
    • Scope: Select the virtual machine group that you created in Step 3.
    • Locate Automation Workflow and click Add Action.
  4. Configure the action settings:

    • Action Type: Select the action types that should trigger the workflow, such as Cloud Scale All.
    • Action Generation: Select Manual.

      • Manual: Turbonomic generates actions, but does not execute them automatically.
      • Automated: Turbonomic automatically executes actions.
  5. Configure the workflow action:

    • Under On Execution → Critical Workflow, select the webhook workflow that you created in Step 2 (Terraform PR Automation).

    Webhook workflow selection

  6. Configure scaling constraints:

    • Exclude specific machine types, including the current machine type of the virtual machines, or
    • Include only the machine types that you want Turbonomic to consider for scaling actions.
  7. Save the policy.

Step 5. Validate the workflow

After you complete the configuration, test the workflow to confirm that all components work together correctly. This test verifies that a Turbonomic optimization action triggers the webhook, starts the AI workflow, and creates a Terraform pull request.

  1. Start the HTTP service.

  2. Execute a Turbonomic action.

    In the Turbonomic user interface:

    • Navigate to the target virtual machine.
    • Locate a scale action.
    • Click Execute.

    Executing a Turbonomic action

  3. Monitor the HTTP service.

    • Review the service logs.
    • Verify that the webhook request was received.
    • Verify that the AI assistant started processing the action.
  4. Verify the pull request. After the workflow runs successfully, review the pull request created by the AI assistant. The pull request should contain the Terraform code update along with a clear explanation of the proposed change.

    • Git diff: Review the Git diff to verify that the Terraform code change matches the Turbonomic optimization action.

      Git diff showing Terraform instance type change from g1-small to e2-small in pull request

    • Pull request description: Review the pull request description generated by the AI assistant. The description should explain the infrastructure change, the Terraform code update, and the reason for the change.

      Pull request description generated by IBM Bob Shell

Summary

When Turbonomic optimizes a Terraform-managed resource, the Terraform code should also be updated. Updating the Terraform code ensures that the change moves through the normal review, approval, and deployment process.

IBM Turbonomic already provides strong support for Terraform-managed environments through the Provider for Continuous Optimization and the Terraform integration. Turbonomic identifies the optimization opportunity, while the Terraform integration provides the Terraform code context needed to locate the correct configuration. Together, these capabilities help reduce manual effort and support Git-based infrastructure workflows.

This tutorial demonstrated one way to extend that foundation. By combining Turbonomic webhooks, Terraform code context, and AI, teams can convert an optimization action into a pull request that developers can review and approve.

This approach helps teams:

  • Reduce the manual effort required to keep infrastructure and Terraform code aligned
  • Preserve optimization results by updating Terraform code after approved infrastructure changes
  • Maintain visibility and governance through pull request reviews
  • Integrate with AI coding assistants such as IBM Bob Shell and other code-aware AI tools

By automating Terraform code updates, teams can move optimization decisions from running infrastructure into source code more efficiently while continuing to use their existing Git-based development workflow.