Terraform
インストール
Install Terraform | Terraform - HashiCorp Learn
apt
$ sudo apt-get update && sudo apt-get install -y gnupg software-properties-common curl
$ curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -
$ sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"
$ sudo apt-get update && sudo apt-get install terraform
CLI
フォーマッタ
standard styleへ変換する。
terraform fmt -recursive
-recursive
無しの場合はカレントディレクトリのみが対象
providerバージョン確認
terraform providers -version
といわれてるけどproviders
が無くても出力は同じ。
providerバージョンアップグレード
tfファイルのprovicersの定義で指定バージョンを変更して、以下コマンド実行。
terraform init -upgrade
リソース一覧
terraform state list
workspace
現在のworkspace
$ terraform workspace show
default
一覧
$ terraform workspace list
default
* 1
2
変更
workspace 2
に変更
$ terraform workspace select 2
Switched to workspace "2".
設定値は.terraform/environment
に記録される。
variable
引数
terraform plan -var='enable_wan=false'
backend設定
S3
backendの定義を追加
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 3.0"
}
}
backend "s3" {
bucket = "bucket-name"
key = "terraform.tfstate"
region = "ap-northeast-1"
}
}
provider "aws" {
region = "ap-northeast-1"
}
もともとローカルでtfstate作っていた状態からS3へ移行する場合は、
- S3バケット作成
- tfstateをS3アップロード
- ソースに
backend
定義追加 (バケット名・キー(ファイル)名・リージョン指定)
ここまではセットで。
で、
- ローカルのtfstateを退避
terraform init
実行 (S3設定が反映される)terraform plan
で差分がないことを確認
でいける。