リソース管理
リソース管理
Terraformにおけるリソースは、管理したいインフラストラクチャの構成要素です。このページでは、リソースの定義、作成、更新、削除の方法について説明します。
リソースとは
リソースは、Terraformが管理するインフラストラクチャのコンポーネントです。例えば、AWS EC2インスタンス、Azure仮想マシン、Google Cloud Storageバケットなどがリソースに該当します。
リソースの基本構文
resource "リソースタイプ" "リソース名" {
# リソースの設定
引数名 = 値
}
例: AWS EC2インスタンス
resource "aws_instance" "web" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
tags = {
Name = "WebServer"
}
}
構成要素の説明:
resource: リソースブロックを宣言するキーワード"aws_instance": リソースタイプ(プロバイダー名_リソース種類)"web": ローカル名(Terraform内で参照するための名前)ami,instance_type,tags: リソースの引数
リソースの作成
基本的なワークフロー
- リソースを定義:
.tfファイルにリソースを記述 - 初期化:
terraform initでプロバイダーを初期化 - 計画の確認:
terraform planで変更内容を確認 - 適用:
terraform applyでリソースを作成
実践例: S3バケットの作成
# main.tf
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}
provider "aws" {
region = "ap-northeast-1"
}
resource "aws_s3_bucket" "example" {
bucket = "my-terraform-example-bucket-12345"
tags = {
Name = "My bucket"
Environment = "Dev"
}
}
実行手順:
# 初期化
terraform init
# 実行計画の確認
terraform plan
# リソースの作成
terraform apply
terraform applyを実行すると、確認プロンプトが表示されます:
Do you want to perform these actions?
Terraform will perform the actions described above.
Only 'yes' will be accepted to approve.
Enter a value: yes
yesと入力すると、リソースが作成されます。
リソースの参照
他のリソースから別のリソースの属性を参照できます。
基本的な参照方法
リソースタイプ.リソース名.属性名
実践例: VPCとサブネット
# VPCの作成
resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16"
tags = {
Name = "main-vpc"
}
}
# サブネットの作成(VPCのIDを参照)
resource "aws_subnet" "public" {
vpc_id = aws_vpc.main.id # VPCのIDを参照
cidr_block = "10.0.1.0/24"
tags = {
Name = "public-subnet"
}
}
# セキュリティグループの作成(VPCのIDを参照)
resource "aws_security_group" "web" {
name = "web-sg"
description = "Security group for web servers"
vpc_id = aws_vpc.main.id # VPCのIDを参照
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
}
Terraformは自動的に依存関係を解析し、VPC → サブネット → セキュリティグループの順に作成します。
リソースの更新
リソースの設定を変更すると、Terraformは変更を検出し、必要な更新を行います。
インプレース更新
リソースを削除せずに更新できる場合があります。
resource "aws_instance" "web" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
tags = {
Name = "WebServer"
Environment = "Production" # タグを追加
}
}
terraform planを実行すると:
Terraform will perform the following actions:
# aws_instance.web will be updated in-place
~ resource "aws_instance" "web" {
id = "i-1234567890abcdef0"
~ tags = {
+ "Environment" = "Production"
"Name" = "WebServer"
}
}
Plan: 0 to add, 1 to change, 0 to destroy.
~マークは更新を示し、+マークは追加を示します。
再作成が必要な更新
一部の属性を変更すると、リソースの再作成が必要になります。
resource "aws_instance" "web" {
ami = "ami-new-version" # AMIを変更
instance_type = "t2.small" # インスタンスタイプを変更
tags = {
Name = "WebServer"
}
}
terraform planを実行すると:
Terraform will perform the following actions:
# aws_instance.web must be replaced
-/+ resource "aws_instance" "web" {
~ ami = "ami-0c55b159cbfafe1f0" -> "ami-new-version" # forces replacement
~ instance_type = "t2.micro" -> "t2.small" # forces replacement
id = "i-1234567890abcdef0"
}
Plan: 1 to add, 0 to change, 1 to destroy.
-/+マークは、リソースが削除されてから再作成されることを示します。
リソースの削除
設定ファイルから削除
リソースの定義を設定ファイルから削除し、terraform applyを実行すると、実際のリソースも削除されます。
# リソース定義を削除した後
terraform apply
terraform destroy コマンド
すべてのリソースを削除する場合:
terraform destroy
特定のリソースのみを削除する場合:
terraform destroy -target=aws_instance.web
terraform destroyは、Terraformが管理しているすべてのリソースを削除します。本番環境では特に注意してください。リソースのメタ引数
Terraformは、すべてのリソースで使用できる特別な引数(メタ引数)を提供しています。
depends_on
明示的に依存関係を指定します。
resource "aws_instance" "web" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
depends_on = [aws_s3_bucket.logs]
}
resource "aws_s3_bucket" "logs" {
bucket = "my-logs-bucket"
}
count
同じリソースを複数作成します。
resource "aws_instance" "server" {
count = 3
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
tags = {
Name = "Server-${count.index}"
}
}
これにより、Server-0、Server-1、Server-2という名前の3つのインスタンスが作成されます。
for_each
マップやセットを使用して複数のリソースを作成します。
resource "aws_instance" "server" {
for_each = {
web = "t2.micro"
app = "t2.small"
db = "t2.medium"
}
ami = "ami-0c55b159cbfafe1f0"
instance_type = each.value
tags = {
Name = each.key
}
}
lifecycle
リソースのライフサイクル動作をカスタマイズします。
resource "aws_instance" "web" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
lifecycle {
create_before_destroy = true # 新しいリソースを先に作成
prevent_destroy = true # 削除を防止
ignore_changes = [tags] # タグの変更を無視
}
}
lifecycle オプション:
create_before_destroy: 再作成時に新しいリソースを先に作成prevent_destroy: リソースの削除を防止ignore_changes: 特定の属性の変更を無視
リソースのインポート
既存のインフラストラクチャをTerraformの管理下に置くには、terraform importコマンドを使用します。
# 既存のEC2インスタンスをインポート
terraform import aws_instance.web i-1234567890abcdef0
手順:
- リソース定義を作成
terraform importでリソースをインポートterraform planで差分を確認- 必要に応じて設定を調整
ベストプラクティス
1. 明確なリソース名を使用
# 良い例
resource "aws_instance" "web_server" {
# ...
}
# 悪い例
resource "aws_instance" "x" {
# ...
}
2. タグを活用
resource "aws_instance" "web" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
tags = {
Name = "WebServer"
Environment = "Production"
ManagedBy = "Terraform"
Project = "MyProject"
}
}
3. 変数を使用して柔軟性を高める
variable "instance_type" {
description = "EC2 instance type"
type = string
default = "t2.micro"
}
resource "aws_instance" "web" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = var.instance_type
}
4. リソースを論理的にグループ化
.
├── network.tf # VPC、サブネット、ルートテーブル
├── compute.tf # EC2インスタンス、Auto Scaling
├── database.tf # RDS、DynamoDB
└── security.tf # セキュリティグループ、IAMロール
次のステップ
リソース管理の基本を理解したら、次は状態管理について学びましょう。