Skip to content

Terraform チートシート

インフラの構築・変更・バージョン管理を行う Infrastructure as Code ツール。

01

はじめに

Provider

Provider でクラウドプラットフォームを定義します。

terraform
provider "aws" {
  region = "us-east-1"
}

Resource

Resource でインフラのオブジェクトを定義します。

terraform
resource "aws_instance" "web" {
  ami           = "ami-123456"
  instance_type = "t2.micro"
  tags = { Name = "WebServer" }
}

コマンド

基本のワークフローは init、plan、apply、destroy です。

terraform
# terraform init
# terraform plan
# terraform apply
# terraform destroy
02

機能

変数

変数で構成をパラメータ化できます。

terraform
variable "region" {
  type    = string
  default = "us-east-1"
}

resource "aws_instance" "web" {
  region = var.region
}

Outputs

Outputs で apply 後の値を取り出せます。

terraform
output "instance_ip" {
  value = aws_instance.web.public_ip
}

Data Sources

Data Source で既存のリソースを参照できます。

terraform
data "aws_ami" "ubuntu" {
  most_recent = true
  filter { name = "name"; values = ["ubuntu-*"] }
}

Was this helpful?