概要
Terraformで変数をつかってみます。
variableを使うと変数が定義できます。
きほんのかたち
インスタンスタイプをt3.microの変数を設定している。
provider "aws" {
region = "ap-northeast-1"
}
variable "instance_type" {
default = "t3.micro"
}
resource "aws_instance" "wawawa" {
ami = "ami-068a6cefc24c301d2"
instance_type = var.instance_type
}
こうなる
$ terraform plan | grep instance_type
+ instance_type = "t3.micro"
-var
オプションで上書き
-var
オプションで上書きできます。
$ terraform plan -var 'instance_type=t3.medium' | grep instance_type
+ instance_type = "t3.medium"
TF_VAR_wawawa
環境変数で上書き
環境変数で上書きできます。
TF_VAR_wawawa
という名前の環境変数を定義してあげると、それを読み込んでくれます。
$ export TF_VAR_instance_type=t3.large
$ echo ${TF_VAR_instance_type}
t3.large
$ terraform plan | grep instance_type
+ instance_type = "t3.large"
ローカル変数で上書きされないようにする
ローカル変数をつかうと、上書きできないようになります。
こんなかんじで定義してあげます。
provider "aws" {
region = "ap-northeast-1"
}
locals {
instance_type = "t3.micro"
}
resource "aws_instance" "wawawa" {
ami = "ami-068a6cefc24c301d2"
instance_type = local.instance_type
}
おわりに
いろいろありますね