Closed7

【terraform】モジュールでの条件分岐を考える

not75743not75743

題材はLambda

ディレクトリ階層

.
├── main.tf
├── modules
          ├── each-module
                    ├── lambda.tf
                    └── variables.tf

lambda.tf

### IAMロール
resource "aws_iam_role" "lambda" {
  name = "${var.function}-iam-role"
  assume_role_policy = jsonencode({
    Version = "2012-10-17"
    Statement = {
      Effect = "Allow"
      Action = "sts:AssumeRole"
      Principal = {
        Service = "lambda.amazonaws.com"
      }
    }
  })
}

resource "aws_iam_role_policy" "lambda" {
  name = "${var.function}-iam-policy"
  role = aws_iam_role.lambda.id
  policy = jsonencode({
    Version = "2012-10-17"
    Statement = [
      {
        Effect = "Allow"
        Action = [
          "logs:CreateLogGroup",
          "logs:CreateLogStream",
          "logs:PutLogEvents"
        ]
        Resource = [
          aws_cloudwatch_log_group.lambda.arn,
          "${aws_cloudwatch_log_group.lambda.arn}:*"
        ]
      }
    ]
  })
}

### cloudwatch logs
resource "aws_cloudwatch_log_group" "lambda" {
  name              = "/aws/lambda/${aws_lambda_function.lambda.function_name}"
  retention_in_days = 30
}

### lambda
resource "aws_lambda_function" "lambda" {
  function_name = var.function
  memory_size   = 128
  package_type  = "Image"
  image_uri     = var.image_uri
  role          = aws_iam_role.lambda.arn
}

main.tf

module "each-module" {
  for_each  = toset(["test1", "test2", "test3"])
  function  = each.key
  source    = "./modules/each-module"
  env       = var.env
  image_uri = var.image_uri
}
not75743not75743

デプロイすると

$ aws lambda list-functions | jq -r ".Functions[].FunctionName" | grep test | sort
test1
test2
test3

Lambda関数が3つできる

not75743not75743

test1だけメモリサイズを256にする

lambda.tf

lambda.tf
resource "aws_lambda_function" "lambda" {
  function_name = var.function
+ memory_size   = var.memory_size
  package_type  = "Image"
  image_uri     = var.image_uri
  role          = aws_iam_role.lambda.arn
}

main.tf

module "each-module" {
  for_each    = toset(["test1", "test2", "test3"])
  function    = each.key
+ memory_size = each.key == "test1" ? 256 : 128
  source      = "./modules/each-module"
  env         = var.env
  image_uri   = var.image_uri
}

すると

$ aws lambda list-functions --query 'Functions[*].[FunctionName, MemorySize]' --output table
--------------------------
|      ListFunctions     |
+----------------+-------+
|  test2         |  128  |
|  test1         |  256  |
|  test3         |  128  |
+----------------+-------+

と問題なく実現できた
三項演算子を使っている。
https://developer.hashicorp.com/terraform/language/expressions/conditionals

not75743not75743

①モジュールを再利用する

test1,2,3でそれぞれ同じモジュールを使う。シンプル

module "test1" {
  function  = "test1"
  memory_size = 256
  source    = "./modules/each-module"
  env       = var.env
  image_uri = var.image_uri
}

module "test2" {
  function  = "test2"
  memory_size = 128
  source    = "./modules/each-module"
  env       = var.env
  image_uri = var.image_uri
}

module "test3" {
  function  = "test3"
  memory_size = 512
  source    = "./modules/each-module"
  env       = var.env
  image_uri = var.image_uri
}

terraform init -upgradeなりでモジュールを読み込む必要がある
また既存のモジュールを一度削除する必要あり?tfstateいじってなんとかなる?調査が必要

not75743not75743

②lookupで環境毎にメモリ数を取得

lookup関数を使うことで複数環境に対応出来ます。

locals {
  lambda_memory = {
    test1 = {
      memory_size = 256
    }
    test2 = {
      memory_size = 128
    }
    test3 = {
      memory_size = 512
    }
  }
}

module "each-module" {
  for_each = toset(["test1", "test2", "test3"])
  function    = each.key
  memory_size = lookup(local.lambda_memory, each.key, {
    memory_size = 128
  }).memory_size
  source      = "./modules/each-module"
  env         = var.env
  image_uri   = var.image_uri
}

lookupはマップのキーを指定することでその値を利用することができる関数です
https://developer.hashicorp.com/terraform/language/functions/lookup
https://qiita.com/moko_Swallows/items/e19e8eb553fa7d565bcf#lookupmap-key-default

このスクラップは2023/12/01にクローズされました