Terraformでクリスマスツリーを作る

2020.12.24

Terraformでクリスマスツリーを作ってみました。

どうゆうこと?

そういうTerraform Providerがあるのです。

やってみた

折角なので Providerの使い方を学んでいってください。

Providerの定義

先程のProviderのページ右上に「Use Provider」のボタンがあるのでクリックして、コードの雛形をゲットします。 use-provider use-provider2 providerブロックは、今回はやることがないので省略します。

また、TerraformのVersion指定もしておきます。この書き方は Version0.13以降の書き方なので。

terraform {
  required_version = "~> 0.14.3"
  required_providers {
    christmas-tree = {
      source = "cappyzawa/christmas-tree"
      version = "0.2.1"
    }
  }
}

初期化

$ terraform init

Initializing the backend...

Initializing provider plugins...
- Finding cappyzawa/christmas-tree versions matching "0.2.1"...
- Installing cappyzawa/christmas-tree v0.2.1...
- Installed cappyzawa/christmas-tree v0.2.1 (self-signed, key ID 507E4B79DB2F5588)

Partner and community providers are signed by their developers.
If you'd like to know more about provider signing, you can read about it here:
https://www.terraform.io/docs/plugins/signing.html

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.

Providerがダウンロードされました。

Resource定義

早速クリスマスツリーを作りましょう。

resource "christmas-tree" "example" {
  path        = "/tmp/example"
  ball_color  = "red"
  star_color  = "yellow"
  light_color = "white"
}
$ terraform apply --auto-approve
christmas-tree.example: Creating...
christmas-tree.example: Creation complete after 0s [id=/tmp/example]

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

確認

pathで指定した場所にクリスマスツリーができています。 tree

Data Sourceもあります

data "christmas-tree" "example" {
  path = "/tmp/example"
}
output tree {
  value = data.christmas-tree.example.tree
}

data-source

メリークリスマス!🎄