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

推荐订阅源

F
Full Disclosure
WordPress大学
WordPress大学
小众软件
小众软件
Cloudbric
Cloudbric
AWS News Blog
AWS News Blog
腾讯CDC
量子位
人人都是产品经理
人人都是产品经理
大猫的无限游戏
大猫的无限游戏
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
V
Vulnerabilities – Threatpost
Scott Helme
Scott Helme
Hugging Face - Blog
Hugging Face - Blog
博客园_首页
C
CXSECURITY Database RSS Feed - CXSecurity.com
The Hacker News
The Hacker News
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
IT之家
IT之家
Jina AI
Jina AI
Attack and Defense Labs
Attack and Defense Labs
S
SegmentFault 最新的问题
Simon Willison's Weblog
Simon Willison's Weblog
The Cloudflare Blog
阮一峰的网络日志
阮一峰的网络日志
T
Tailwind CSS Blog
Last Week in AI
Last Week in AI
博客园 - 【当耐特】
Google Online Security Blog
Google Online Security Blog
美团技术团队
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
Visual Studio Blog
罗磊的独立博客
L
LINUX DO - 最新话题
博客园 - Franky
博客园 - 叶小钗
Apple Machine Learning Research
Apple Machine Learning Research
The Last Watchdog
The Last Watchdog
J
Java Code Geeks
AI
AI
C
Cisco Blogs
酷 壳 – CoolShell
酷 壳 – CoolShell
C
Cyber Attacks, Cyber Crime and Cyber Security
Cisco Talos Blog
Cisco Talos Blog
博客园 - 三生石上(FineUI控件)
雷峰网
雷峰网
Help Net Security
Help Net Security
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
云风的 BLOG
云风的 BLOG
I
Intezer
S
Securelist

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 Analyzing Unsplash Photo Performance with Python I am a Top Mentor on MentorCruise! 🎉 | Blog CI/CD Observability on GitHub Actions and the Role of OpenTelemetry | Blog CI/CD Observability on GitHub Actions and the Role of OpenTelemetry Silent Water Sentinel | Photography Three Early Crosses | Photography Fiery Twilight Trails | Photography Forested Folds Flowing | Photography Majestic Snowbound Spire | Photography Shrouded Winter Peaks | Photography Space Cat Pillar | Photography I am a CNCF (Cloud Native Computing Foundation) Ambassador! | Blog Curved Valley Mist | Photography Highly Independent Tree | Photography Misty Morning Plateau | Photography Sick Shadows Fading | Photography Half Moon Blossom | Photography Serene Pedestal Swinging | Photography Sunset Clouds Reeling | Photography Aerial Nose Parking | Photography How to Structure C Projects: These Best Practices Worked for Me | Blog How to Structure C Projects: These Best Practices Worked for Me I'm on the KubeFM Podcast Talking About "Linux Containers From Scratch" | Blog I am (again) a Google Developers Expert! | Blog How to Configure OIDC with Terraform for GitHub Enterprise Server | Blog How to Configure OIDC with Terraform for GitHub Enterprise Server | Blog Modern Frontend Development: A Tooling Overview for Engineers Revisiting the Field | Blog Meet verto.sh: Your Gateway to Open-Source Collaboration. | Blog Crafting a Clean, Maintainable, and Understandable Makefile for a C Project. | Blog Crafting a Clean, Maintainable, and Understandable Makefile for a C Project. | Blog barco: Linux Containers From Scratch in C. | Blog barco: Linux Containers From Scratch in C. | Blog How to Create a Release With Multiple Artifacts From a GitHub Actions Workflow Using the Matrix Strategy | Blog How to Create a Release With Multiple Artifacts From a GitHub Actions Workflow Using the Matrix Strategy | Blog How Databases Store and Retrieve Data with B-Trees | Blog How Databases Store and Retrieve Data with B-Trees | Blog Concurrency in Go: Goroutines, Channels, Mutexes, and More | Blog Concurrency in Go: Goroutines, Channels, Mutexes, and More | Blog Club Cloud 2021: Cloud Engineering Panel Discussion | Blog Club Cloud 2021: Cloud Engineering Panel Discussion | Blog How to Prepare for the Google Cloud Engineer Associate Certification Exam | Blog How to Prepare for the Google Cloud Engineer Associate Certification Exam | Blog What is Google Cloud Deploy? | Blog What is GitOps? | Blog Club Cloud Stories #2 - News from Around the Cloud | Blog Club Cloud Stories #2 - News from Around the Cloud | Blog Club Cloud Stories #1 - The First Episode with Antoni Tzavelas & Mark van Holsteijn | Blog Club Cloud Stories #1 - The First Episode with Antoni Tzavelas & Mark van Holsteijn | Blog Quiet Oak Shining | Photography How to Read Firestore Events with Cloud Functions and Golang | Blog How to Read Firestore Events with Cloud Functions and Golang | Blog Google Cloud Pub/Sub vs NATS: An Easy-to-Understand Comparison | Blog Google Cloud Pub/Sub vs NATS: An Easy-to-Understand Comparison | Blog How to Deploy a Multi-cluster Service Mesh on GKE with Anthos | Blog How to Deploy a Multi-cluster Service Mesh on GKE with Anthos | Blog How to Safely Store Secrets in Terraform Using Cloud KMS | Blog How to Safely Store Secrets in Terraform Using Cloud KMS | Blog Designing Serverless Applications on AWS - Jacco Kulman and Luca Cavallin @ End2End LIVE | Blog Designing Serverless Applications on AWS - Jacco Kulman and Luca Cavallin @ End2End LIVE | Blog How to Use Terraform Workspaces to Manage Environment-based Configuration | Blog Puffy Steel Spreading | Photography How to Deploy ElasticSearch on GKE using Terraform and Helm | Blog How to Deploy ElasticSearch on GKE using Terraform and Helm | Blog Summer Windmills Spinning | Photography How to Optimize PHP Performance on Google Cloud Run | Blog How to Optimize PHP Performance on Google Cloud Run | Blog Foggy Boats Rusting | Photography How I Prepared for the Google Cloud Associate Cloud Engineer Exam | Blog How I Prepared for the Google Cloud Associate Cloud Engineer Exam | Blog Winter Kids Chasing | Photography
Things I've Learned About Terraform That I Keep Telling People About
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!