惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

The GitHub Blog
The GitHub Blog
博客园 - 三生石上(FineUI控件)
V
V2EX
博客园 - 司徒正美
小众软件
小众软件
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
T
Tailwind CSS Blog
Last Week in AI
Last Week in AI
雷峰网
雷峰网
月光博客
月光博客
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Apple Machine Learning Research
Apple Machine Learning Research
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
S
SegmentFault 最新的问题
美团技术团队
Hugging Face - Blog
Hugging Face - Blog
WordPress大学
WordPress大学
宝玉的分享
宝玉的分享
爱范儿
爱范儿
博客园 - 聂微东
量子位
J
Java Code Geeks
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Vercel News
Vercel News

Luca Cavallin

AI Engineering for Developers | Blog AI Engineering for Developers Platform Engineering End-to-End | Blog Google Cloud Networking 101: The Comprehensive TLDR | Blog Google Cloud Networking 101: The Comprehensive TLDR Containers Are Not Automatically Secure | Blog Containers Are Not Automatically Secure Watery Stone Beacon | Photography Blue Iceman Suture | Photography Hidden Emerald Pool | Photography Autumn Chapel Pinnacles | Photography A Tour of eBPF in the Linux Kernel: Observability, Security and Networking | Blog A Tour of eBPF in the Linux Kernel: Observability, Security and Networking Shared Violet Pulse | Photography Kubernetes Networking from Packets to Pods | Blog An Overview of Network Protocols | Blog An Overview of Network Protocols A Quick Journey Into the Linux Kernel | Blog A Quick Journey Into the Linux Kernel OpenTelemetry: A Guide to Observability with Go | Blog I'm on the Cillers Podcast Talking About Tech and Hackathons | Blog Yet Another List of Random Opinions on Writing Readable Code and Other Rants | Blog My post about Istio is now on the Istio blog too! | Blog Tropical Jungle Escape | Photography The Istio Service Mesh for People Who Have Stuff to Do | Blog Dreamy Cartoonscape Windmill | Photography Twilight Windmill Reflections | Photography Notes I took while reading "Applied Machine Learning and AI for Engineers" and "Introducing MLOps" | Blog Things I've Learned About Terraform That I Keep Telling People About | Blog Analyzing Unsplash Photo Performance with Python | Blog
Things I've Learned About Terraform That I Keep Telling P...
Luca Cavallin · 2024-07-10 · via Luca Cavallin

I've been working with Terraform for a while now, and I've noticed that there are a few things that people keep asking me about. I thought it would be helpful to write a blog post about some of the most common questions I get asked and share some of the things I've learned along the way. This is not an exhaustive list, and, if you have any feedback or suggestions, please let me know!

File Conventions

Terraform offers great flexibility in its configuration language, making it easy to write code and organize directories according to your preferences. This adaptability ensures that your code remains readable, scalable, and maintainable. A well-organized codebase makes it easier to manage and scale your infrastructure. Here's a standard file structure I recommend to get started:

  • main.tf: The core file where you define your resources, data sources, and modules.
  • data.tf: I like to put data sources here.
  • variables.tf: Where you declare all the variables your configuration will use.
  • outputs.tf: Defines the outputs from your resources, making data available to other parts of your configuration.
  • provider.tf: Providers are initialized in this file.
  • versions.tf: Specifies the required versions of Terraform and its providers.
  • terraform.tfvars: Contains variable values that override the default values set in variables.tf. This file is often environment-specific and should not be checked into version control.

Naming Conventions

Consistency in naming makes your Terraform code easier to read and maintain. Here are some key guidelines:

  • Use underscores (_) instead of dashes (-) in resource names, data source names, variable names, and outputs.
  • Stick to lowercase letters and numbers.
  • Avoid repeating the resource type in names. For example, use resource "aws_vpc" "main" {} or resource "aws_vpc" "this" {} instead of resource "aws_vpc" "main_vpc" {}.
  • Use singular nouns for resource names.
  • For arguments values that will be exposed to humans (like DNS names), use dashes.

Using Data Sources

Data sources allow you to query existing resources in your infrastructure. This can be incredibly powerful for dynamically retrieving information and avoiding hardcoding values in your configuration.

Example of using a data source:

hcl

data "aws_ami" "latest" {
  most_recent = true
  owners      = ["self"]
 
  filter {
    name   = "name"
    values = ["my-custom-ami-*"]
  }
}
 
resource "aws_instance" "example" {
  ami           = data.aws_ami.latest.id
  instance_type = "t2.micro"
 
  tags = {
    Name = "example-instance"
  }
}

Configuring and Handling State

Managing state is a critical aspect of using Terraform effectively. The state file keeps track of the resources Terraform manages, so it's important to store it securely and make it accessible to your team. Here are some best practices:

  • Remote State Storage: Use a remote backend like AWS S3, Azure Storage, or Terraform Cloud to store your state files. This ensures that your state is not lost and can be accessed by your team members.
  • State Locking: Enable state locking to prevent multiple users from making concurrent changes. AWS S3 with DynamoDB, for example, supports state locking.
  • Do not forget to review resources periodically, restrict state file access, and carefully remove unnecessary resources.
  • Never store sensitive information in state files or in your Terraform configuration (use environment variables or data sources and a secrets management tool). I wrote a whole post about How to Safely Store Secrets in Terraform Using Cloud KMS - check it out!
  • Do not edit state files manually. If you need to make changes, use Terraform commands like terraform state mv, terraform state rm, or terraform import (but if you can avoid it, do so).

Using Terraform Workspaces

Terraform workspaces are incredibly useful for managing and isolating different state files within a single project, especially when dealing with multiple environments. They allow you to deploy multiple resources based on different inputs. With workspaces, you can use the same configuration for various environments like development, staging, and production. Each workspace maintains its own state file, making it easier to handle multiple environments efficiently.

Terraform Workspaces are an excellent way to manage multiple environments (e.g., dev, staging, production) within the same configuration. Workspaces allow you to use a single Terraform configuration for different environments, with separate state files for each workspace. After creating a new workspace with terraform workspace new, you can switch between workspaces using terraform workspace select. You can then reference the current workspace in your Terraform code to differentiate configurations or resource names, like so:

hcl

resource "aws_instance" "example" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"
  tags = {
    Name = "${terraform.workspace}-example-instance"
  }
}

I wrote another blog post about How to Use Terraform Workspaces to Manage Environment-based Configuration - check this one out too!

Terraform provides pretty much everything you need to manage your infrastructure out-of-the-box. However, there are some additional tools that can help you work more efficiently and maintain code quality.

Using Formatters and Linters

Formatters are tools that automatically format your code according to a specific style guide. Terraform provides the terraform fmt command to format your code based on the HashiCorp style guide. Linters help maintain code quality by enforcing style guidelines and catching potential errors. To maintain code quality and consistency, you can use TFLint as a linter. It helps identify errors and best practice violations in your Terraform configurations.

If you are using GitHub Actions as your CI/CD pipeline (do it!), you can use the terraform-linters/setup-tflint action from the marketplace to set up and run TFLint in your workflow.

Terratest and Terragrunt

Terratest and Terragrunt are two tools for testing and managing your Terraform configurations. They are built by Gruntwork, a company that specializes in DevOps and infrastructure as code.

Terratest is a Go library that provides patterns and helper functions for testing your infrastructure code. It allows you to write automated tests for your Terraform configurations to ensure they work as expected, just like you would test your application code.

Example of a simple test in Go with Terratest:

go

package test
 
import (
  "testing"
  "github.com/gruntwork-io/terratest/modules/terraform"
)
 
func TestTerraformExample(t *testing.T) {
  opts := &terraform.Options{
    TerraformDir: "../examples/terraform-aws-example",
  }
  defer terraform.Destroy(t, opts)
  terraform.InitAndApply(t, opts)
}

Terragrunt instead is a thin wrapper for Terraform that provides extra tools for keeping your configurations DRY (Don't Repeat Yourself). It is especially useful for managing multiple environments and handling dependencies between modules.

Conclusion

I worked with Terraform for a while now, and I've learned a few things along the way. I find myself mentioning these best practices and tools to people who are new to Terraform or looking to improve their existing workflows. I hope this post helps you get started with Terraform and provides some useful tips for working with it effectively!

By following these best practices for file conventions, naming conventions, using data sources, configuring and handling state and workspaces, leveraging linters and other tools like Terratest and Terragrunt you'll ensure your infrastructure-as-code configuration is robust, maintainable, and scalable.

Happy Terraforming!