インフラストラクチャ as コード (IaC) のための包括的な Terraform コマンドとワークフロー。リソース管理、ステート操作、マルチクラウドデプロイメントを含む。
インストールとセットアップ
| コマンド | 説明 |
|---|
terraform version | Terraformのバージョンを表示 |
terraform -help | ヘルプ情報を表示 |
terraform -help plan | 特定のコマンドのヘルプを表示 |
コアワークフロー
基本操作
| コマンド | 説明 |
|---|
terraform init | 作業ディレクトリを初期化 |
terraform plan | 実行計画を作成する |
terraform apply | 変更を適用 |
terraform destroy | インフラストラクチャーを破壊する |
terraform validate | 設定を検証 |
terraform fmt | 構成ファイルをフォーマット |
高度な計画
| コマンド | 説明 |
|---|
terraform plan -out=tfplan | ファイルに計画を保存 |
terraform apply tfplan | 保存したプランを適用 |
terraform plan -target=resource.name | 特定のリソースを計画 |
terraform plan -var="key=value" | 変数を使った計画 |
terraform plan -var-file="vars.tfvars" | 変数ファイルを使用した計画 |
ステート管理
ステート操作
| コマンド | 説明 |
|---|
terraform state list | ステート内のリソースをリスト |
terraform state show resource.name | リソースの詳細を表示 |
terraform state mv old_name new_name | ステート内のリソースを移動 |
terraform state rm resource.name | ステートからリソースを削除 |
terraform state pull | リモート状態をダウンロード |
terraform state push | リモートに状態をアップロード |
ステートのバックアップと復旧
| コマンド | 説明 |
|---|
terraform state backup | ステートのバックアップを作成 |
terraform force-unlock LOCK_ID | 強制的にロック解除状態 |
terraform refresh | リソースで状態を更新 |
ワークスペース
| コマンド | 説明 |
|---|
terraform workspace list | ワークスペースを一覧表示 |
terraform workspace new dev | 新しいワークスペースを作成 |
terraform workspace select dev | ワークスペースに切り替え |
terraform workspace delete dev | ワークスペースを削除 |
terraform workspace show | 現在のワークスペースを表示 |
インポートと出力
リソースのインポート
| コマンド | 説明 |
|---|
terraform import resource.name id | 既存のリソースをインポート |
terraform import aws_instance.example i-1234567890abcdef0 | AWS インスタンスをインポート |
出力
| コマンド | 説明 |
|---|
terraform output | すべての出力を表示 |
terraform output instance_ip | 特定の出力を表示 |
terraform output -json | JSON で出力を表示 |
設定例
基本的な AWS EC2 インスタンス
provider "aws" \\\\{
region = "us-west-2"
\\\\}
resource "aws_instance" "example" \\\\{
ami = "ami-0c55b159cbfafe1d0"
instance_type = "t2.micro"
tags = \\\\{
Name = "example-instance"
\\\\}
\\\\}
output "instance_ip" \\\\{
value = aws_instance.example.public_ip
\\\\}
変数
variable "instance_type" \\\\{
description = "EC2 instance type"
type = string
default = "t2.micro"
\\\\}
variable "environment" \\\\{
description = "Environment name"
type = string
\\\\}
データソース
data "aws_ami" "ubuntu" \\\\{
most_recent = true
owners = ["099720109477"] # Canonical
filter \\\\{
name = "name"
values = ["ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*"]
\\\\}
\\\\}
モジュール
module "vpc" \\\\{
source = "terraform-aws-modules/vpc/aws"
name = "my-vpc"
cidr = "10.0.0.0/16"
azs = ["us-west-2a", "us-west-2b"]
private_subnets = ["10.0.1.0/24", "10.0.2.0/24"]
public_subnets = ["10.0.101.0/24", "10.0.102.0/24"]
enable_nat_gateway = true
enable_vpn_gateway = true
\\\\}
リモートステート設定
S3 バックエンド
terraform \\\\{
backend "s3" \\\\{
bucket = "my-terraform-state"
key = "state/terraform.tfstate"
region = "us-west-2"
\\\\}
\\\\}
Azure バックエンド
terraform \\\\{
backend "azurerm" \\\\{
resource_group_name = "tfstate"
storage_account_name = "tfstate"
container_name = "tfstate"
key = "prod.terraform.tfstate"
\\\\}
\\\\}
Google Cloud バックエンド
terraform \\\\{
backend "gcs" \\\\{
bucket = "tf-state-bucket"
prefix = "terraform/state"
\\\\}
\\\\}
プロバイダー設定
AWS プロバイダー
provider "aws" \\\\{
region = "us-west-2"
profile = "default"
default_tags \\\\{
tags = \\\\{
Environment = "production"
Project = "my-project"
\\\\}
\\\\}
\\\\}
Azure プロバイダー
provider "azurerm" \\\\{
features \\\\{\\\\}
subscription_id = "00000000-0000-0000-0000-000000000000"
tenant_id = "00000000-0000-0000-0000-000000000000"
\\\\}
Google Cloud プロバイダー
provider "google" \\\\{
project = "my-project-id"
region = "us-central1"
zone = "us-central1-c"
\\\\}
高度な機能
条件付きリソース
Would you like me to continue with the remaining sections or provide more specific translations for the empty sections?```hcl
resource “aws_instance” “example” \\{
count = var.create_instance ? 1 : 0
ami = data.aws_ami.ubuntu.id
instance_type = var.instance_type
\\}
```hcl
resource "aws_instance" "example" \\\\{
for_each = toset(var.instance_names)
ami = data.aws_ami.ubuntu.id
instance_type = var.instance_type
tags = \\\\{
Name = each.key
\\\\}
\\\\}
```(Empty)
```hcl
resource "aws_security_group" "example" \\\\{
name = "example"
dynamic "ingress" \\\\{
for_each = var.ingress_rules
content \\\\{
from_port = ingress.value.from_port
to_port = ingress.value.to_port
protocol = ingress.value.protocol
cidr_blocks = ingress.value.cidr_blocks
\\\\}
\\\\}
\\\\}
```(Empty)
## テストと検証
### Terraform Validate
| コマンド | 説明 |
|---------|-------------|
| `terraform validate` | 構文を検証 |
| `terraform validate -json` | JSONで出力を検証する |(Empty)
### Terraform Plan Analysis
| コマンド | 説明 |
|---------|-------------|
| `terraform plan -detailed-exitcode` | 詳細な終了コードを使用した計画 |
| `terraform show` | 現在の状態を表示 |
| `terraform show -json` | JSONで状態を表示 |(Empty)
### サードパーティツール
| コマンド | 説明 |
|---------|-------------|
| `tflint` | Terraform リンター |
| `terraform-docs` | ドキュメントを生成する |
| `checkov -f main.tf` | セキュリティスキャン |
| `tfsec .` | セキュリティ分析 |(Empty)
## デバッグとトラブルシューティング
### ログ
| コマンド | 説明 |
|---------|-------------|
| `TF_LOG=DEBUG terraform plan` | デバッグログを有効にする |
| `TF_LOG=TRACE terraform apply` | トレースログを有効にする |
| `TF_LOG_PATH=terraform.log terraform plan` | ファイルにログを記録 |(Empty)
### 一般的な問題
| コマンド | 説明 |
|---------|-------------|
| `terraform refresh` | 現実と状態を同期する |
| `terraform taint resource.name` | レクリエーションのためのリソースをマーク |
| `terraform untaint resource.name` | リソースからタイントを削除する |(Empty)
## ベストプラクティス
### ファイル構成```
project/
├── main.tf # Main configuration
├── variables.tf # Variable definitions
├── outputs.tf # Output definitions
├── versions.tf # Provider versions
├── terraform.tfvars # Variable values
└── modules/
└── vpc/
├── main.tf
├── variables.tf
└── outputs.tf
```(Empty)
### バージョン制約```hcl
terraform \\\\{
required_version = ">= 1.0"
required_providers \\\\{
aws = \\\\{
source = "hashicorp/aws"
version = "~> 5.0"
\\\\}
\\\\}
\\\\}
```(Empty)
### リソース命名```hcl
resource "aws_instance" "web_server" \\\\{
# Use descriptive names
ami = data.aws_ami.ubuntu.id
instance_type = var.instance_type
tags = \\\\{
Name = "$\\\\{var.project_name\\\\}-web-$\\\\{var.environment\\\\}"
Environment = var.environment
Project = var.project_name
\\\\}
\\\\}
```(Empty)
### セキュリティベストプラクティス`-parallelism`**ステートセキュリティ**: 暗号化されたリモートステートを使用する`-target`**シークレット管理**: 外部シークレットストアを使用する