Cloud Cost Optimization with HashiCorp Terraform: A Simple Guide
Sarwan Jassi
Multi-Cloud Enterprise Solutions Architect | Data Center & Hosting Specialist | DevOps/DevSecOps Consultant | FinOps Advisor | Site Reliability Engineer |Agile Methodologies Advocate | Leader in Digital Transformation
Cloud computing has changed how companies manage their IT infrastructure, and it's now more important than ever to keep an eye on costs. Traditionally, finance teams managed budgets, but with the rise of on-demand cloud services, engineers are stepping into the role of cloud financial controllers. So, how do you manage this shift effectively?
This guide will help you understand how to use HashiCorp Terraform to optimize your cloud costs. By the end, you'll know how to define roles, automate processes, and implement a strategy for cloud cost optimization.
What You'll Learn
A Survey of Cloud Waste
With the continuous shift to consumption-based cost models for infrastructure and operations, i.e., Cloud Service Providers (CSPs), you pay for what you use but you also pay for what you provision and don’t use. If you do not have a process for continuous governance and optimization, then there is a huge potential for waste.
A recent cloud spending survey found that:
First, let’s unpack why there is an opportunity and then get to the execution.
The Role of Cloud Governance Teams
When moving to the cloud, many organizations establish governance models where a team, often called the Cloud Center of Excellence, oversees strategy, architecture, operations, and costs. These teams typically include IT management, cloud technical specialists, and finance professionals. Finance is responsible for cost planning, migration financial forecasting, and optimization.
However, financial teams often say, “We need to get a handle on costs, savings, forecasting, etc.” but lack direct control over these costs. It’s now engineers who manage both infrastructure and costs directly.
The Financial Paradigm Shift
The shift to cloud services brings a new financial paradigm:
Finance lacks control in two primary areas:
Planning, Optimization, and Governance
Now the next question: How can engineers use Terraform at each level of the cloud cost management process to deliver value and minimize additional work? To get started, see how the visualization illustrates Terraform’s place in the cloud cost management lifecycle. (Start at the top with the “Planning” phase)
To summarize the steps:
领英推荐
Planning — Pre-Migration and Ongoing Cost Forecasting
Cloud migrations require a multi-point assessment to determine if it makes sense to move an application/workload to the cloud. Primary factors for the assessment are:
Since engineers are now taking on some of these responsibilities, it makes sense to use engineering tools to handle them. Terraform helps engineers take on these new responsibilities.
Using Terraform configuration files as a standard definition of how an application/workload’s cost is estimated, you can now use HCP Terraform & Enterprise APIs to automatically supply finance with estimated cloud financial data or use Terraform’s user interface to provide finance direct access to review costs. By doing this, you can help eliminate many slower oversight processes.
Planning Recommendations:
Basic Patterns for Consuming Optimization Recommendations
To establish a mechanism for Terraform to access optimization recommendations, we see several common patterns:
Optimization as Code: Terraform Code Update Examples
To optimize resources effectively, it's important to maintain key pieces of resource data as variables. Optimization tools provide recommendations for resources such as compute, database, and storage. Here's how you can set up your Terraform configuration to use these recommendations.
At a minimum, you should have three variables: new_recommendations, current_fallback, and resource_unique_id.
For example, using Densify, you can find the Densify Terraform module via the Terraform Registry and the Densify-dev GitHub repo.
Step 1: Define Variables
variable "densify_recommendations" {
description = "Map of maps generated from the Densify Terraform Forwarder. Contains all of the systems with the settings needed to provide details for tagging as Self-Aware and Self-Optimization"
type = "map"
}
variable "densify_unique_id" {
description = "Unique ID that both Terraform and Densify can use to track the systems."
}
variable "densify_fallback" {
description = "Fallback map of settings that are used for new infrastructure or systems that are missing sizing details from Densify."
type = "map"
}
Step 2: Update Terraform Code with Variables and Logic
Use the lookup function to check for optimization recommendations in the local file densify.auto.tfvars.
locals {
temp_map = "${merge(map(var.densify_unique_id, var.densify_fallback), var.densify_recommendations)}"
densify_spec = "${local.temp_map[var.densify_unique_id]}"
cur_type = "${lookup(local.densify_spec, "currentType", "na")}"
rec_type = "${lookup(local.densify_spec, "recommendedType", "na")}"
savings = "${lookup(local.dens