LoginSignup
3
1

More than 1 year has passed since last update.

TerraformのLifecycleブロックを使用して管理対象をカスタマイズする

Posted at

概要

TerraformのLifecycleの引数であるignore_changesを使用することで管理外のブロックを明示的に指定できます。
要するに積極的にコード管理しない機能です。
コンテナ定義をアプリ開発者側でupdateするといったケースでは、リビジョンのインクリメント毎に差分が発生してしまうので、リソースブロック内で差分が出ないことを目的に使用しています。
図では緑矢印のcodepipelineとecsserviceの箇所です。
スクリーンショット 2023-02-08 15.38.16.png

対象

下記ではtask定義とtaskの起動数およびロードバランサーを対象にしています。
(B/Gデプロイメント戦略ではtarget_groupをignoreにしたいが、できない?(検証できてない)のでロードバランサー自体をignoreにしています。)

resource "aws_ecs_service" "example" {
  # ...

  lifecycle {
    ignore_changes = [
      desired_count,   # Updated possibly by auto scaling
      task_definition, # Updated by deployments
      load_balancer,
    ]
  }

下記のケースでは開発段階ではターゲットとなるブランチが頻繁に変更されるため、
サービスブロック内でネストされた引数であるブランチ名をignoreにしています。

resource "aws_codepipeline" "example" {
  # ...

  stage {
    name = "Source"

    action {
      category = "Source"
      configuration = {
        "BranchName"           = "feature/xxxxxx"
        "ConnectionArn"        = xxxxxx
        "FullRepositoryId"     = "xxxxxx/xxxxxx"
        "OutputArtifactFormat" = "CODE_ZIP"
      }
    }
  }

  lifecycle {
    ignore_changes = [
      stage.0.action.0.configuration.BranchName,
    ]
  }
3
1
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
3
1