PixelaのグラフをTerraformで作成する
2020/11/25
Pixelaのグラフの作成がTerraformでできるようなので試してみた。
#pixela を宣言的に管理するぞ!ということで初めてTerraform Providerを作ってみた!
— Yoichiro Shimizu (@budougumi0617) November 24, 2020
v0.0.4ではグラフをTerraformで定義できます!https://t.co/LdD99leEjO
手順
TerraformはDockerで。
必要なファイルを作成
- docker-compose.yml
- .env
- main.tf
docker-compose.yml
version: '3'
services:
terraform:
image: hashicorp/terraform:light
volumes:
- ./:/terraform
working_dir: /terraform
env_file:
- .env
.env
PIXELA_TOKEN=hogehoge
main.tf
terraform {
required_providers {
pixela = {
versions = ">= 0.0.4"
source = "budougumi0617/pixela"
}
}
}
provider pixela {
username = "takagi"
}
resource "pixela_graph" "sample" {
graph_id = "sample"
name = "sample from terraform"
unit = "page"
type = "int"
color = "ajisai"
timezone = "Asia/Tokyo"
self_sufficient = "none"
is_secret = true
publish_optional_data = false
}
terraform init
$ docker-compose run --rm terraform init
Creating terraform-pixela_terraform_run ... done
Initializing the backend...
Initializing provider plugins...
- Finding latest version of budougumi0617/pixela...
- Installing budougumi0617/pixela v0.0.4...
- Installed budougumi0617/pixela v0.0.4 (self-signed, key ID 4CE3A37F58A6A092)
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
The following providers do not have any version constraints in configuration,
so the latest version was installed.
To prevent automatic upgrades to new major versions that may contain breaking
changes, we recommend adding version constraints in a required_providers block
in your configuration, with the constraint strings suggested below.
* budougumi0617/pixela: version = "~> 0.0.4"
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 plan
$ docker-compose run --rm terraform plan
Creating terraform-pixela_terraform_run ... done
Refreshing Terraform state in-memory prior to plan...
The refreshed state will be used to calculate this plan, but will not be
persisted to local or remote state storage.
------------------------------------------------------------------------
An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
+ create
Terraform will perform the following actions:
# pixela_graph.sample will be created
+ resource "pixela_graph" "sample" {
+ color = "ajisai"
+ graph_id = "sample"
+ id = (known after apply)
+ is_secret = true
+ last_updated = (known after apply)
+ name = "sample from terraform"
+ publish_optional_data = false
+ self_sufficient = "none"
+ timezone = "Asia/Tokyo"
+ type = "int"
+ unit = "page"
}
Plan: 1 to add, 0 to change, 0 to destroy.
------------------------------------------------------------------------
Note: You didn't specify an "-out" parameter to save this plan, so Terraform
can't guarantee that exactly these actions will be performed if
"terraform apply" is subsequently run.
terraform apply
$ docker-compose run --rm terraform apply
Creating terraform-pixela_terraform_run ... done
An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
+ create
Terraform will perform the following actions:
# pixela_graph.sample will be created
+ resource "pixela_graph" "sample" {
+ color = "ajisai"
+ graph_id = "sample"
+ id = (known after apply)
+ is_secret = true
+ last_updated = (known after apply)
+ name = "sample from terraform"
+ publish_optional_data = false
+ self_sufficient = "none"
+ timezone = "Asia/Tokyo"
+ type = "int"
+ unit = "page"
}
Plan: 1 to add, 0 to change, 0 to destroy.
Do you want to perform these actions?
Terraform will perform the actions described above.
Only 'yes' will be accepted to approve.
Enter a value: yes
pixela_graph.sample: Creating...
pixela_graph.sample: Creation complete after 0s [id=sample]
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
確認
グラフがちゃんと作られてる! takagi/sample from terraform | Pixela
terraform destroy
$ docker-compose run --rm terraform destroy
Creating terraform-pixela_terraform_run ... done
pixela_graph.sample: Refreshing state... [id=sample]
An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
- destroy
Terraform will perform the following actions:
# pixela_graph.sample will be destroyed
- resource "pixela_graph" "sample" {
- color = "ajisai" -> null
- graph_id = "sample" -> null
- id = "sample" -> null
- is_secret = true -> null
- name = "sample from terraform" -> null
- publish_optional_data = false -> null
- self_sufficient = "none" -> null
- timezone = "Asia/Tokyo" -> null
- type = "int" -> null
- unit = "page" -> null
}
Plan: 0 to add, 0 to change, 1 to destroy.
Do you really want to destroy all resources?
Terraform will destroy all your managed infrastructure, as shown above.
There is no undo. Only 'yes' will be accepted to confirm.
Enter a value: yes
pixela_graph.sample: Destroying... [id=sample]
pixela_graph.sample: Destruction complete after 0s
Destroy complete! Resources: 1 destroyed.
おわり
完璧だ。
Pixela as Code(PaC)ができる。