Quick overview
This workflow runs every 6 hours to detect AWS Cost Explorer spend spikes, correlates the spike to the latest Terraform change in GitHub, uses OpenAI to draft a root-cause analysis and updated Terraform file, opens a GitHub pull request with the fix, and posts the results to Slack.
How it works
- Runs on a schedule (or via manual trigger) and sets repository, Terraform path, thresholds, and date ranges for analysis.
- Queries AWS Cost Explorer for daily unblended cost by AWS service over the last 8 days and calculates today’s spend against a rolling baseline average.
- If the percent increase exceeds the configured threshold, identifies the top contributing AWS service and continues; otherwise the run stops.
- Uses the GitHub API to list recent commits that touched the target Terraform file and selects the most recent commit as the likely source of the change.
- Fetches the current Terraform file from GitHub and sends the cost spike metrics, suspect commit details, and file content to OpenAI to generate a root-cause summary, fix description, and a complete corrected file.
- Creates a new GitHub branch from the base branch, commits the updated Terraform file to that branch, opens a pull request, and posts a Slack message with the cost impact, analysis, and PR link.
Setup
- Add an AWS credential with Cost Explorer permissions (ce:GetCostAndUsage) and ensure Cost Explorer calls are made in us-east-1.
- Add a GitHub API credential (personal access token with repo scope) and ensure it can create branches, commit to the target file path, and open pull requests.
- Add an OpenAI credential and select a compatible chat model (for example, gpt-4.1-mini) for the agent step.
- Add a Slack credential with chat:write access and ensure the target channel exists.
- Update the configuration values for repoOwner, repoName, terraformPath, baseBranch, costSpikeThresholdPercent, and slackChannel to match your environment.