0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

terraform tips

Posted at

今更なものも多いが、改めて必要な場面で知ると便利なのでメモしておく

countとfor_eachの併用

  • 通常countとfor_each併用できない。
  • ただし、環境差異などで差分が発生する場合にcountでリソース作成要否を判定してから、for_eachで繰り返しリソースを作成したい場合がある。
    • NG例
    resource "aws_kinesis_firehose_delivery_stream" "aws_to_datadog" {
      count var.environment.inputs.env_name == "prod" ? 1 : 0
      for_each    = local.kinesis_streams
      destination = "http_endpoint"
      name        = each.value.kinesis_streams_name
      ///略///
    
    • 以下ならOK
    resource "aws_kinesis_firehose_delivery_stream" "aws_to_datadog" {
      for_each    =  var.environment.inputs.env_name == "prod" ? local.kinesis_streams : {}
      destination = "http_endpoint"
      name        = each.value.kinesis_streams_name
      ///略///
    

dynamic_blockによる複数リソースの構築

  • 一つのリソースブロック中のパラメータブロックで環境ごとの差分を表現したい。
  • dynamicを利用することで必要な数だけループもしくは、環境ごとに作成要否を判定できる。
resource "aws_lb" "alb" {
  ///略///
   dynamic "access_logs" {
    for_each = var.environment.inputs.env_name == "prod" ? [1] : []
    content {
      bucket  = data.aws_s3_bucket.alb_log[0].bucket
      prefix  = "logs"
      enabled = true
    }
  ///略///
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?