Open5

Terraform使用感メモ

SaitomTechSaitomTech

準備

Terraformのバージョン管理用ソフト

-> https://github.com/tfutils/tfenv

インストール方法

-> $ brew install tfenv でOK

使い方

  • $ tfenv list-remote
    • インストール可能なTerraformの一覧を取得
  • $ tfenv install <version>
    • 指定したversionのTerraformをインストール(uninstallもある)
  • $ tfenv list
  • $ tfenv use <version>
    • 上でインストールしたTerraformを使用

参考

試してみた結果

  • $ terraform --version でちゃんと切り替わるのを確認

  • $ terraform

Main commands:
  init          Prepare your working directory for other commands
  validate      Check whether the configuration is valid
  plan          Show changes required by the current configuration
  apply         Create or update infrastructure
  destroy       Destroy previously-created infrastructure

拡張機能

名前: HashiCorp Terraform
ID: hashicorp.terraform
説明: Syntax highlighting and autocompletion for Terraform
バージョン: 2.25.2
パブリッシャー: HashiCorp
VS Marketplace リンク: https://marketplace.visualstudio.com/items?itemName=HashiCorp.terraform
SaitomTechSaitomTech

試してみる

AMIはマネジメントコンソールの「インスタンスを起動」から適当に確認

作業メモ

コード

main.tf
provider "aws" {
    profile = "default"
    region = "us-east-1"
}

resource "aws_instance" "sample" {
    ami = "ami-0beaa649c482330f7"
    instance_type = "t2.micro"
}

作業

$ terraform init でAWS関連のPluginがインストールされる( .terraform/ 配下)

Initializing the backend...

Initializing provider plugins...
- Finding latest version of hashicorp/aws...
- Installing hashicorp/aws v4.47.0...
- Installed hashicorp/aws v4.47.0 (signed by HashiCorp)

Terraform has created a lock file .terraform.lock.hcl to record the provider
selections it made above. Include this file in your version control repository
so that Terraform can guarantee to make the same selections by default when
you run "terraform init" in the future.

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.
  • terraform.lock.hclについて(【Terraform】.terraform.lock.hclについて理解する)

https://rurukblog.com/post/terraform-lock-hcl/

  • .gitignore に含めるかどうか
    • .terraform/: 含めない
    • .lock.hcl: 含める

https://stackoverflow.com/questions/59289037/should-i-version-the-terraform-folder
https://stackoverflow.com/questions/67963719/should-terraform-lock-hcl-be-included-in-the-gitignore-file
https://qiita.com/nyamada43/items/b8becb672ad572897c25

参考

https://reboooot.net/post/what-is-terraform/

SaitomTechSaitomTech

試してみる(続き)

  • $ terraform apply

指定してるところだけ明記される感じ

  # aws_instance.sample will be created
  + resource "aws_instance" "sample" {
      + ami                                  = "ami-0b5eea76982371e91"
      + arn                                  = (known after apply)
      + associate_public_ip_address          = (known after apply)
...

Created!

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

その他

  • 同時に terraform.tfstate というファイルが生成されている(.backupというのも)
  • コンソール上で確かに作成されているのを確認できた
  • .tfstate はgit管理しないらしい(以下リンク参照)

https://zenn.dev/sway/articles/terraform_staple_sharestate
https://stackoverflow.com/questions/38486335/should-i-commit-tfstate-files-to-git

SaitomTechSaitomTech

HCL2

  • # でコメント
  • ヒアドキュメント可
  • {} でブロック
    • ブロックタイプ
    • ラベル

ブロックタイプ

locals, variable, terraform, provider, data, resource, output, ...

変数

locals

  • 以下のように定義して ${local.aaa} のように参照("abc")
locals {
    aaa = "abc"
}

variable

  • 以下のように定義して ${var.aiueo} のように参照("abc")
  • 外部変更可能な変数
variable "aiueo" {
    type = string
    default = "abc"  ← 変数のデフォルト値
}
  • type には以下を指定可能
    • プリミティブ: string, number, bool,
    • 構造体: object (キーバリュー), tuple
    • コレクション: list, map, set
object
variable "aiueo" {
    type = object({
        aaa = string
        bbb = number
    })
    default = {
        aaa = "abc"
        bbb = 12345
    }
}
tuple
variable "aiueo" {
    type = tuple([string, number])
    default = ["abc", 12345]
}