DevOps 面试题大全(六·上):Terraform 基础设施即代码 25 题详解

前言

Terraform 是基础设施即代码(IaC)工具,使用 HCL 语言声明式管理云资源。本文整理 50 道 Terraform 面试题。

一、基础概念题(1-15 题)

1. 什么是 Terraform?

HashiCorp 开发的 IaC 工具,支持多云 Provider,声明式配置,状态管理,依赖自动推导。

2. Terraform 核心概念

  • Provider - 云服务插件(AWS/Azure/GCP)
  • Resource - 基础设施资源
  • State - 状态文件
  • Module - 可复用代码单元
  • Variable/Output - 输入输出

3. Terraform 工作流程

  1. Write - 编写 .tf 配置
  2. Init - 初始化(下载 Provider)
  3. Plan - 预览变更
  4. Apply - 应用变更
  5. Destroy - 销毁资源

4. 基本配置示例

# provider.tf
provider "aws" {
  region = "us-east-1"
}

# resource.tf
resource "aws_instance" "web" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"
  
  tags = {
    Name = "web-server"
  }
}

# output.tf
output "instance_ip" {
  value = aws_instance.web.public_ip
}

5. State 文件作用

  • 记录资源映射关系
  • 追踪资源元数据
  • 支持依赖分析
  • 实现增量更新

6. State 后端类型

  • local - 本地文件(默认)
  • s3 - AWS S3(推荐生产)
  • azurerm - Azure Blob
  • gcs - Google Cloud Storage
  • consul - Consul KV

7. 远程后端配置

terraform {
  backend "s3" {
    bucket = "my-terraform-state"
    key    = "prod/terraform.tfstate"
    region = "us-east-1"
    
    encrypt        = true
    dynamodb_table = "terraform-locks"
  }
}

8. 变量类型

  • string - 字符串
  • number - 数字
  • bool - 布尔值
  • list - 列表
  • map - 映射
  • object - 对象
  • tuple - 元组

9. 变量定义方式

# variables.tf
variable "instance_type" {
  description = "EC2 实例类型"
  type        = string
  default     = "t2.micro"
}

# terraform.tfvars
instance_type = "t2.small"

# 命令行
terraform apply -var "instance_type=t2.medium"

10. Output 输出

output "lb_dns" {
  description = "负载均衡器 DNS"
  value       = aws_lb.main.dns_name
}

output "instance_ids" {
  value = aws_instance.web[*].id
}

11-15. 更多基础题

题号 题目 要点
11 terraform init 作用? 初始化,下载 Provider
12 terraform plan 作用? 预览变更,不执行
13 什么是 Provider? 云服务 API 插件
14 资源依赖如何处理? 自动推导,显式引用
15 terraform.tfvars 作用? 变量值文件

二、进阶实战题(16-25 题)

16. Meta-Arguments

# count - 数量
resource "aws_instance" "web" {
  count         = 3
  ami           = "ami-123456"
  instance_type = "t2.micro"
}

# for_each - 遍历
resource "aws_s3_bucket" "buckets" {
  for_each = toset(["dev", "staging", "prod"])
  bucket   = "my-bucket-${each.key}"
}

# depends_on - 显式依赖
resource "aws_instance" "app" {
  depends_on = [aws_s3_bucket.data]
}

17. 条件表达式

resource "aws_instance" "web" {
  instance_type = var.env == "prod" ? "t2.large" : "t2.micro"
  
  tags = {
    Environment = var.env
    Managed     = "true"
  }
}

18. 动态块

resource "aws_security_group" "web" {
  dynamic "ingress" {
    for_each = var.ports
    content {
      from_port = ingress.value
      to_port   = ingress.value
      protocol  = "tcp"
      cidr_blocks = ["0.0.0.0/0"]
    }
  }
}

19. 本地值(locals)

locals {
  common_tags = {
    Project     = "web-app"
    Environment = var.env
    ManagedBy   = "terraform"
  }
}

resource "aws_instance" "web" {
  tags = local.common_tags
}

20. 数据源查询

data "aws_ami" "ubuntu" {
  most_recent = true
  owners      = ["099720109477"]
  
  filter {
    name   = "name"
    values = ["ubuntu/images/hvm-ssd/ubuntu-focal-*"]
  }
}

resource "aws_instance" "web" {
  ami = data.aws_ami.ubuntu.id
}

21. Module 调用

module "vpc" {
  source = "terraform-aws-modules/vpc/aws"
  version = "3.14.0"
  
  cidr = "10.0.0.0/16"
  azs  = ["us-east-1a", "us-east-1b"]
  
  tags = {
    Environment = "prod"
  }
}

22. Module 开发

  • inputs.tf - 输入变量
  • outputs.tf - 输出值
  • main.tf - 主要资源
  • versions.tf - Provider 版本
  • README.md - 使用文档

23. Workspace 多环境

# 创建 workspace
terraform workspace new dev
terraform workspace new prod

# 切换
terraform workspace select dev

# 在配置中使用
resource "aws_instance" "web" {
  instance_type = terraform.workspace == "prod" ? "t2.large" : "t2.micro"
}

24. Import 导入资源

# 1. 编写资源定义
resource "aws_instance" "imported" {
  # ...
}

# 2. 导入
terraform import aws_instance.imported i-12345678

# 3. 调整配置匹配现有资源

25. 状态操作命令

  • terraform state list - 列出资源
  • terraform state show - 显示资源
  • terraform state mv - 移动资源
  • terraform state rm - 移除资源
  • terraform state pull/push - 拉取/推送

下篇预告:26-50 题,涵盖高级特性、故障排查、最佳实践等

发表回复

后才能评论