Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Terraform言語の静的解析 / static analysis of Terraform...

Sponsored · SiteGround - Reliable hosting with speed, security, and support you can count on.

Terraform言語の静的解析 / static analysis of Terraform language

Lint Night #4 の発表資料です
https://lintnight.connpass.com/event/385142/

Avatar for Kazuma Watanabe

Kazuma Watanabe

April 24, 2026

More Decks by Kazuma Watanabe

Other Decks in Programming

Transcript

  1. Terraform aws_instance.example: Creating... │ Error: creating EC2 Instance: operation error

    EC2: RunInstances, ... │ │ with aws_instance.example, │ on main.tf line 17, in resource "aws_instance" "example": │ 17: resource "aws_instance" "example" { │
  2. TFLint ― A Pluggable Terraform Linter $ tflint 1 issue(s)

    found: Error: "t1.2xlarge" is an invalid value as instance_type (aws_instance_invalid_type) on main.tf line 19: 19: instance_type = "t1.2xlarge" # invalid type!
  3. Terraform言語とHCL • HCL ≠ Terraform言語 • Terraform言語はHCLの上に追加の意味論を持つ言語 • 例えば... ◦

    resource blockがEC2インスタンスなどのリソースと対応付けられることは Terraform言語の仕様 ◦ resourceのようにブロックに名前とラベルを設定できるのはHCLの仕様 ◦ var.amiがvariable blockに定義された変数に解決されるのはTerraform言語 の仕様 ◦ var.amiのようにオブジェクトの属性を評価できるのはHCLの仕様
  4. HCL Syntax-Agnostic Information Model • HCLには構文に依存しない情報モデルが定義されており、複数の具象構文が実装 できるように抽象型と意味論が定義されている ◦ https://github.com/hashicorp/hcl/blob/v2.24.0/spec.md •

    よく知られるHCLと呼ばれる構文は正確にはHCL native syntaxと呼ばれる具象構 文のひとつ • TerraformがJSONでも書けるのはHCL JSON syntaxがHCLの具象構文のひとつ であるため
  5. HCL JSON構文 JSON構文にはブロック定義とオブジェクト属性の曖昧性がある # block foo { bar = "baz"

    } # object attribute foo = { bar = "baz" } # block or object attribute? { "foo": { "bar": "baz" } }
  6. TerraformにおけるUnknown Value • applyするまでわからない値はunknownとして解決される • terraform plan時に出てくる(known after apply)の正体はunknown value

    Terraform will perform the following actions: # aws_instance.example will be created + resource "aws_instance" "example" { + ami = "ami-0e467ee8344baec9e" + arn = (known after apply)
  7. Value Refinements • go-ctyでは未知の値に追加の制約を与えることができる ◦ cty.UnknownVal(cty.Number).NumberUpperBound(2) • 自明なケースで未知の値に解決されてしまうのを防ぐ ◦ 例えば

    <unknown expr> ? 1 : 2 のような式は明らかに2より大きくならないが、 unknownが伝染して制約が失われてしまう • v1.18の時点ではNotNullやStringPrefix, CollectionLengthUpperBoundなどの Refinementsがサポートされている
  8. 式の評価 • go-ctyのunknown valueの仕組みに乗っかり、簡単に解決できない値を未知の値 として扱っている ◦ デフォルト値の無い必須変数 ◦ データソースの属性 ◦

    モジュールの出力 ◦ プロバイダ定義関数の返り値 • Terraform言語の型システムの上で実現できているため、保守性が高い
  9. 参考文献 • https://developer.hashicorp.com/terraform/language/syntax • https://github.com/hashicorp/hcl/blob/v2.24.0/spec.md • https://github.com/hashicorp/hcl/blob/v2.24.0/hclsyntax/spec.md • https://github.com/hashicorp/hcl/blob/v2.24.0/json/spec.md •

    https://log.martinatkins.me/2021/06/14/terraform-plan-unknown-values/ • https://github.com/zclconf/go-cty/blob/v1.18.0/docs/concepts.md • https://github.com/zclconf/go-cty/blob/v1.18.0/docs/marks.md • https://github.com/zclconf/go-cty/blob/v1.18.0/docs/refinements.md • https://github.com/hashicorp/hcl/pull/590 • https://github.com/hashicorp/terraform/pull/33234