Terraformの式を途中で改行する方法

Terraformにおける式の改行方法を備忘録的に記載していきます。


```.bash:使用バージョンとプロバイダ
$ terraform version
Terraform v0.14.2
+ provider registry.terraform.io/hashicorp/aws v3.21.0
```

# 条件式
**改行したい時は"()"で囲む**
例）S3バケット名が変数として指定されていればその名前を、未指定なら特定の命名規則に従ったバケット名を作成したい時

```terraform:改行無し
variable bucket_name {
  default     = ""
}
locals {
  bucket_name = var.bucket_name == "" ? "test-bucket-${formatdate("YYYYMMDDhhmmss", timestamp())}" : var.bucket_name
}
output "assigned_bucket_name" {
  value = local.bucket_name
}
```

```terraform:改行有り
variable "bucket_name" {
  default = ""
}
locals {
  bucket_name = (
    var.bucket_name == "" ?
    "test-bucket-${formatdate("YYYYMMDDhhmmss", timestamp())}" :
    var.bucket_name
  )
}
output "assigned_bucket_name" {
  value = local.bucket_name
}

```

```terraform:(参考)エラーになる例
variable bucket_name {
  default     = ""
}
locals {
  bucket_name = var.bucket_name == "" ? \
                "test-bucket-${formatdate("YYYYMMDDhhmmss", timestamp())}" : \
                var.bucket_name

}
output "assigned_bucket_name" {
  value = local.bucket_name
}

```

# for文
**特に何も必要無し**
例）S3バケットに保存されているオブジェクトのうち、特定の文字列パターンに合致するオブジェクト名を大文字に変換したリストを作成したい時（なんで？）

```.tf:改行無し
data "aws_s3_bucket_objects" "bucket" {
  bucket = "some-bucket"
}
locals {
  objects = [for obj in data.aws_s3_bucket_objects.bucket.keys : upper(obj) if length(regexall("SOME_PATTERN", obj)) != 0]

}
output "objects" {
  value = local.objects
}
```

```.tf:改行有り
data "aws_s3_bucket_objects" "bucket" {
  bucket = "some-bucket"
}
locals {
  objects = [
    for obj in data.aws_s3_bucket_objects.bucket.keys :
    upper(obj)
    if length(regexall("SOME_PATTERN", obj)) != 0
  ]

}
output "objects" {
  value = local.objects
}
```


# 文字列
**"${}"の中でなら改行可能**
**また、文字列全体を"()"で囲めば改行可能**
例）複数の変数やリソース名を組み合わせた長い文字列を作成したい場合

```.tf:改行無し
data "aws_s3_bucket" "bucket" {
  bucket = "some-bucket"
}
variable some_variable {
  default     = "SOME_VARIABLE"
}
locals {
  long_string = "This is an example of a long string composed of some variable like ${var.some_variable} or some data source like ${data.aws_s3_bucket.bucket.region}"

}
output "objects" {
  value = local.long_string
}
```

```.tf:改行有り（１）
data "aws_s3_bucket" "bucket" {
  bucket = "some-bucket"
}
variable some_variable {
  default = "SOME_VARIABLE"
}
locals {
  long_string = "This is an example of a long string composed of some variable like ${
    var.some_variable
    } or some data source like ${
    data.aws_s3_bucket.bucket.region
  }"

}
output "long_string" {
  value = local.long_string
}
```

```.tf:改行有り（２）
data "aws_s3_bucket" "bucket" {
  bucket = "some-bucket"
}
variable "some_variable" {
  default = "SOME_VARIABLE"
}
locals {
  long_string = (
    "This is an example of a long string composed of some variable like ${
      var.some_variable
      } or some data source like ${
      data.aws_s3_bucket.bucket.region
      }, by the way ${
      "bla bla bla bla bla bla ..."
    }"
  )
}
output "long_string" {
  value = local.long_string
}
```

```.tf:（参考）主題とはずれるけどあんまり長い時はテンプレートファイル化が吉
data "aws_s3_bucket" "bucket" {
  bucket = "some-bucket"
}
variable some_variable {
  default = "SOME_VARIABLE"
}
data "template_file" "long_string" {
  template = "./template.txt"
  vars = {
    SOME_VAR  = var.some_variable
    SOME_DATA = data.aws_s3_bucket.bucket.region
  }
}
locals {
  long_string = data.template_file.long_string.rendered
}
output "long_string" {
  value = local.long_string
}
```

```.bash:テンプレートファイルの中身
$ cat template.txt 
This is an example of a long string composed of some variable like ${SOME_VAR} or some data source like ${SOME_DATA}
```