Terraform クイックリファレンス
よく使用される terraform コマンドとハウツーのクイック リファレンス。
よく使われるTerraformコマンド
terraform init
Terraform 構成ファイルを含む作業ディレクトリを初期化します。このコマンドは、必要なプロバイダー プラグインをダウンロードしてインストールし、状態ファイルを保存するためのバックエンドを設定します。
terraform plan
実行プランを生成し、構成ファイルで定義されているインフラストラクチャの目的の状態を実現するために Terraform が実行するアクションを示します。これはドライ ランであり、実際のリソースは変更されません。
terraform apply
構成の目的の状態に到達するために必要な変更を適用します。このコマンドは、terraform plan によって生成されたプランを実行し、インフラストラクチャに必要な変更を加えます。
terraform destroy
Terraform によって管理されるインフラストラクチャを破棄します。このコマンドは、構成ファイルで定義されているすべてのリソースを削除するために使用されます。
terraform show
現在の状態または保存されたプランを表示します。このコマンドは、インフラストラクチャの現在の状態または以前に生成されたプランを検査するのに役立ちます。
terraform output
構成で定義された出力値を表示します。これは、デプロイされたインフラストラクチャに関する情報を取得するのに役立ちます。
基本的なTerraform構成を定義する方法
基本的な Terraform 構成ファイルは通常、プロバイダー構成、リソース定義、オプションで変数と出力など、いくつかの主要コンポーネントで構成されます。以下は、AWS EC2 インスタンスをプロビジョニングする Terraform 構成ファイルの簡単な例です。
# Define the provider
provider "aws" {
region = "ap-northest-1a"
}
# Define a variable for the AMI ID
variable "ami_id" {
description = "The AMI ID to use for the instance"
default = "ami-xxx"
}
# Define a variable for the instance type
variable "instance_type" {
description = "The type of instance to use"
default = "t2.micro"
}
# Define the EC2 instance resource
resource "aws_instance" "app_server" {
ami = var.ami_id
instance_type = var.instance_type
tags = {
Name = "ExampleAppServerInstance"
}
}
# Output the public IP of the instance
output "instance_ip" {
description = "The public IP address of the instance"
value = aws_instance.app_server.public_ip
}
モジュールを使用して Terraform 構成を定義する方法
- structure
.
├── main.tf
└── modules
└── ec2_instance
├── main.tf
├── variables.tf
└── outputs.tf
- main.tf
# Define the provider
provider "aws" {
region = "ap-northeast-1a"
}
# Define a variable for the AMI ID
variable "ami_id" {
description = "The AMI ID to use for the instance"
default = "ami-xxx"
}
# Define a variable for the instance type
variable "instance_type" {
description = "The type of instance to use"
default = "t2.micro"
}
# Call the local module
# The label immediately after the module keyword is a local name,
# which the calling module can use to refer to this instance of the module.
module "app_server" {
source = "./modules/ec2_instance"
instance_type = var.instance_type
ami_id = var.ami_id
}
# Output the public IP of the instance
output "instance_ip" {
description = "The public IP address of the instance"
value = module.app_server.instance_ip
}
- modules/ec2_instance/main.tf
# Define the EC2 instance resource
resource "aws_instance" "this" {
ami = var.ami_id
instance_type = var.instance_type
tags = {
Name = "ExampleAppServerInstance"
}
}
- modules/ec2_instance/variables.tf
# Define a variable for the AMI ID
variable "ami_id" {
description = "The AMI ID to use for the instance"
type = string
}
# Define a variable for the instance type
variable "instance_type" {
description = "The type of instance to use"
type = string
}
- modules/ec2_instance/outputs.tf
# Output the public IP of the instance
output "instance_ip" {
description = "The public IP address of the instance"
value = aws_instance.this.public_ip
}
Terraform ブロックの役目は何ですか
Terraform 構成ファイル内の terraform ブロックは、Terraform 自体の動作を構成する設定を定義するために使用されます。このブロックには、必要な Terraform バージョンの指定、状態ストレージのバックエンドの構成、必要なプロバイダーの定義、実験的な機能の有効化など、さまざまな設定を含めることができます。
これらの設定をいくつか組み合わせた例を次に示します。
terraform {
required_version = ">= 1.1.0"
backend "s3" {
bucket = "my-terraform-state"
key = "terraform.tfstate"
region = "ap-northest-1a"
}
required_providers {
aws = {
source = "hashicorp/aws"
version = ">= 3.71.0"
}
}
experiments = ["example"]
provider_meta "aws" {
example_key = "example_value"
}
}
参考文献