<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Terraform | 伊東屋TECHブログ</title>
	<atom:link href="https://tech-itoya.com/tag/terraform/feed/" rel="self" type="application/rss+xml" />
	<link>https://tech-itoya.com</link>
	<description>千葉県・船橋市の総合園芸店が運営する技術ブログ</description>
	<lastBuildDate>Sun, 06 Oct 2024 03:18:39 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9</generator>

<image>
	<url>https://tech-itoya.com/wp-content/uploads/2024/10/cropped-itoya-logo-32x32.png</url>
	<title>Terraform | 伊東屋TECHブログ</title>
	<link>https://tech-itoya.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Terragrunt + Tfcmt + Github Actions で Plan 結果の Summary を Github 上に表示したい</title>
		<link>https://tech-itoya.com/terragrunt-tfcmt-github-actions-%e3%81%a7-plan-%e7%b5%90%e6%9e%9c%e3%81%ae-summary-%e3%82%92-github-%e4%b8%8a%e3%81%ab%e8%a1%a8%e7%a4%ba%e3%81%97%e3%81%9f%e3%81%84/</link>
					<comments>https://tech-itoya.com/terragrunt-tfcmt-github-actions-%e3%81%a7-plan-%e7%b5%90%e6%9e%9c%e3%81%ae-summary-%e3%82%92-github-%e4%b8%8a%e3%81%ab%e8%a1%a8%e7%a4%ba%e3%81%97%e3%81%9f%e3%81%84/#respond</comments>
		
		<dc:creator><![CDATA[tori-dash]]></dc:creator>
		<pubDate>Thu, 19 Sep 2024 17:09:51 +0000</pubDate>
				<category><![CDATA[TECH記事]]></category>
		<category><![CDATA[AWS]]></category>
		<category><![CDATA[Github Actions]]></category>
		<category><![CDATA[IaC]]></category>
		<category><![CDATA[Terraform]]></category>
		<guid isPermaLink="false">https://tech-itoya.com/?p=32</guid>

					<description><![CDATA[こんにちは、とりです。 皆さんは Terraform リポジトリを管理する際、CIを組んでいるでしょうか？ プルリク公開時に Terraform Plan を自動実行して、結果をGithub上で確認できると、変更箇所が伝 [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p>こんにちは、とりです。</p>



<p>皆さんは Terraform リポジトリを管理する際、CIを組んでいるでしょうか？</p>



<p>プルリク公開時に Terraform Plan を自動実行して、結果をGithub上で確認できると、変更箇所が伝わりやすくなりハッピーかと思います。</p>



<p>tfcmt はそんなTerraformのCIを組みたいときに活躍するツールで、terraform plan の結果を素敵な感じに整形して、プルリクにコメントしてくれます</p>



<figure class="wp-block-gallery has-nested-images columns-default is-cropped wp-block-gallery-1 is-layout-flex wp-block-gallery-is-layout-flex">
<figure class="wp-block-image size-large"><img fetchpriority="high" decoding="async" width="909" height="485" data-id="121" src="https://tech-itoya.com/wp-content/uploads/2024/09/tori-20240920-1.png" alt="" class="wp-image-121" srcset="https://tech-itoya.com/wp-content/uploads/2024/09/tori-20240920-1.png 909w, https://tech-itoya.com/wp-content/uploads/2024/09/tori-20240920-1-300x160.png 300w, https://tech-itoya.com/wp-content/uploads/2024/09/tori-20240920-1-768x410.png 768w" sizes="(max-width: 909px) 100vw, 909px" /></figure>
</figure>



<p>ただ、Terragrunt のような複数の Statefile を管理するリポジトリの場合、プルリク上に大量にコメントが流れてしまい、かえって見づらくなることもあるかなと思います。</p>



<p>今回触っていた IaC リポジトリでも、6環境×モジュールが6~7個あったため、最大40個強のPlan結果がプルリクコメントに並んでしまうことになり、さすがにこれを運用すると辛そうだな&#8230;という印象でした。</p>



<p>↓ は Bot が PR に大量にコメントを流している様子です</p>



<figure class="wp-block-image size-full is-resized"><img decoding="async" width="744" height="885" src="https://tech-itoya.com/wp-content/uploads/2024/09/tori-20240920-2.png" alt="bot-tfcmt" class="wp-image-122" style="width:840px;height:auto" srcset="https://tech-itoya.com/wp-content/uploads/2024/09/tori-20240920-2.png 744w, https://tech-itoya.com/wp-content/uploads/2024/09/tori-20240920-2-252x300.png 252w" sizes="(max-width: 744px) 100vw, 744px" /></figure>



<p>そこで、Github Workflow の Summary に結果を見やすくまとめることで、上記の課題を解決しつつ、快適に terraform の CI を運用する方法を紹介したいと思います。</p>



<p>Terragrunt でなく生の Terraform を使っていても、Module 等で State の分割管理を行っている場合、同様の課題感を持っていたりすると思うので、そういった方の参考にもなれば幸いです。</p>



<p>ついでに Github Enterprise 環境で tfcmt を構築している記事もあまり見かけなかったので、その解説もできたらと思っています。</p>




  <div id="toc" class="toc tnt-number toc-center tnt-number border-element"><input type="checkbox" class="toc-checkbox" id="toc-checkbox-2" checked><label class="toc-title" for="toc-checkbox-2">目次</label>
    <div class="toc-content">
    <ol class="toc-list open"><li><a href="#toc1" tabindex="0">結論</a></li><li><a href="#toc2" tabindex="0">実装</a><ol><li><a href="#toc3" tabindex="0">全体のディレクトリ構成</a></li><li><a href="#toc4" tabindex="0">tfcmt.yml の設定</a></li><li><a href="#toc5" tabindex="0">tfwrapper.sh の設定</a></li><li><a href="#toc6" tabindex="0">Github Actions のワークフロー</a></li></ol></li><li><a href="#toc7" tabindex="0">改善ポイント</a></li></ol>
    </div>
  </div>

<h2 class="wp-block-heading"><span id="toc1">結論</span></h2>



<p>いろいろ試した結果、こんな形で落ち着きました。</p>



<figure class="wp-block-gallery has-nested-images columns-default is-cropped wp-block-gallery-2 is-layout-flex wp-block-gallery-is-layout-flex">
<figure class="wp-block-image size-large"><img decoding="async" width="1024" height="489" data-id="124" src="https://tech-itoya.com/wp-content/uploads/2024/09/tori-20240920-3-1024x489.png" alt="" class="wp-image-124" srcset="https://tech-itoya.com/wp-content/uploads/2024/09/tori-20240920-3-1024x489.png 1024w, https://tech-itoya.com/wp-content/uploads/2024/09/tori-20240920-3-300x143.png 300w, https://tech-itoya.com/wp-content/uploads/2024/09/tori-20240920-3-768x366.png 768w, https://tech-itoya.com/wp-content/uploads/2024/09/tori-20240920-3-1536x733.png 1536w, https://tech-itoya.com/wp-content/uploads/2024/09/tori-20240920-3.png 1878w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</figure>



<p>環境ごとに Github Actions のワークフローを作成し、サマリー部分にモジュールごと実行した Plan 結果を見やすくまとめています。</p>



<p>逆にプルリクのラベル付けやコメントを書き込む機能などは、明示的に無効化を行いました。</p>



<h2 class="wp-block-heading"><span id="toc2">実装</span></h2>



<h3 class="wp-block-heading"><span id="toc3">全体のディレクトリ構成</span></h3>



<p>Terraform リポジトリのディレクトリ構成としてはこんな感じになっています</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>.
├── .github
│   ├── workflows
│   │   ├── develop_terraform_plan.yml
│   │   ├── staging_terraform_plan.yml
│   │   └── ... (other files)
│   └── tfcmt
│       ├── tfcmt.yml
│       └── tfwrapper.sh
├── environments
│   ├── develop
│   │   └── ap-northeast-1
│   │       └── ... (modules)
│   └── ... (other environments)
├── modules
│   ├── ecs
│   │   ├── main.tf
│   │   ├── outputs.tf
│   │   ├── variables.tf
│   │   └── ... (other files)
│   └── ... (other modules)
└── terragrunt.hcl</code></pre></div>



<h3 class="wp-block-heading"><span id="toc4">tfcmt.yml の設定</span></h3>



<p>tfcmt.yml という名前で config ファイルを書いておき、tfcmt コマンド実行時にオプションに指定してあげることで、plan 結果を整形するフォーマットをカスタマイズできます。</p>



<p>かなりカスタマイズしやすく、個人的に感動したポイントでした。</p>



<p>参考: <a href="https://suzuki-shunsuke.github.io/tfcmt/config#default-configuration">https://suzuki-shunsuke.github.io/tfcmt/config#default-configuration</a></p>



<p>今回設定した tfcmt.yml の内容になります</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>embedded_var_names: []
terraform:
  plan:
    # github enterprise の時のみ設定が必要
    # 参考: https://suzuki-shunsuke.github.io/tfcmt/github-enterprise
    ghe_base_url: https://XXX.github.com
    ghe_graphql_endpoint: https://XXX.github.com/api/graphql
    # terragrunt だと statefile 毎ラベルが上書きされて逆に管理し辛かったので無効化
    disable_label: true
    template: |
      &lt;details&gt;&lt;summary&gt;&lt;strong&gt; :package: Target Module: {{.Vars.target}} &lt;/strong&gt;&lt;/summary&gt;
        {{template &quot;result&quot; .}}
        {{template &quot;updated_resources&quot; .}}
        {{template &quot;changed_result&quot; .}}
        {{template &quot;change_outside_terraform&quot; .}}
        {{template &quot;warning&quot; .}}
        {{template &quot;error_messages&quot; .}}
      &lt;/details&gt;</code></pre></div>



<p>ポイントとしては以下になります</p>



<ul class="wp-block-list">
<li>Github Enterprise の場合は plan 実行時の 環境変数に <code>ghe_base_url</code> と <code>ghe_graphql_endpoint</code> を足してあげる必要があります。</li>



<li>tfcmt は自動で plan 結果を要約するようなラベル付けを行ってくれるのですが、複数の Statefile を管理するリポジトリの場合は、逆に見辛さが勝ってしまったので無効化しています。</li>



<li><strong>template:</strong> に続くブロックで plan 結果をどんな感じで整形するか指定できるので、お好みでいじってみても良いかと思います。</li>
</ul>



<h3 class="wp-block-heading" id="tfwrapper.sh"><span id="toc5">tfwrapper.sh の設定</span></h3>



<p>Terragrunt の場合、tfcmt が <code>terragrunt run-all plan</code> コマンドをそのまま叩けないので wrapper のスクリプトをかませてあげる必要があります。</p>



<p>Terraform をそのまま使っている場合はこの作業は不要かなと思います。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>#!/bin/bash
set -euo pipefail

# コマンドの種類を取得(例: apply, plan, fmt...)
type=$(echo &quot;$@&quot; |  awk &#39;{print $1}&#39;)

# カレントディレクトリを整形して、target の module 名を取得
base_dir=$(git rev-parse --show-toplevel)
target=${PWD#&quot;$base_dir&quot;/}
target=$(echo &quot;$target&quot; | sed &#39;s|/\.terragrunt-cache/.*||&#39;)

if [ &quot;$type&quot; == &quot;plan&quot; ]; then
    tfcmt --config &quot;$GITHUB_WORKSPACE/.github/tfcmt/tfcmt.yml&quot; \
          --output &quot;/tmp/tgplan.md&quot; \
          -var &quot;target:${target}&quot; \
          plan -patch -- terraform &quot;$@&quot;
fi</code></pre></div>



<p>ここが terragrunt run-all plan コマンドを実行した時の実体のスクリプトになるのですが、その際に　tfcmt の処理を噛ませてあげる流れとなっています</p>



<p>以下オプションの説明になります</p>



<ul class="wp-block-list">
<li><strong>&#8211;config オプション</strong>: plan 結果の整形フォーマットなどを config ファイルに書くことができます。上のステップで作成した tfcmt.yml ファイルを config として指定します</li>
</ul>



<ul class="wp-block-list">
<li><strong>&#8211;output オプション:</strong> tfcmt のメインの使い方は、プルリクのコメントに plan 結果を出力することだと思いますが、今回はそれを無効化しローカルにファイルに吐き出しています</li>
</ul>



<ul class="wp-block-list">
<li><strong>-var オプション:</strong> tfcmt に変数を渡すことができます。今回は terraform のモジュール名を渡して、tfcmt が Plan 結果を整形する際に、モジュール名も含めて出力できるようにしています</li>
</ul>



<p>細かい仕様は 公式 doc を参照ください。</p>



<p>参考: <a href="https://suzuki-shunsuke.github.io/tfcmt/terragrunt-run-all">https://suzuki-shunsuke.github.io/tfcmt/terragrunt-run-all</a></p>



<h3 class="wp-block-heading"><span id="toc6">Github Actions のワークフロー</span></h3>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>name: Execute terragrunt plan
on:
  pull_request:
    branches:
      - &#39;main&#39;
    paths:
      - &#39;environments/develop/**&#39;
      - &#39;modules/**&#39;
  workflow_dispatch:
env:
  Enviromnet: develop
  IAM_ROLE_ARN: XXX
jobs:
  plan-common:
    runs-on: XXX
    steps:
      - uses: actions/checkout@v3

     # plan 用の権限を assume する
      - name: assume ECS Role
 　　 　uses: aws-actions/configure-aws-credentials@v4
   　　　 with:
   　　　   aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
      　　　aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
      　　　aws-region: ap-northeast-1

      # tfcmt のインストール
      - name: setup tfcmt
        env:
          TFCMT_VERSION: v4.13.0
        run: |
          curl -L &quot;https://github.com/suzuki-shunsuke/tfcmt/releases/download/${TFCMT_VERSION}/tfcmt_linux_amd64.tar.gz&quot; -o /tmp/tfcmt.tar.gz
          tar xzf /tmp/tfcmt.tar.gz -C /tmp
          mv /tmp/tfcmt /usr/local/bin
          tfcmt --version

      # terragrunt plan の実行
      - name: Excute terragrunt plan
        shell: bash
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          cd ./environments/${{ env.Enviromnet }}
          terragrunt run-all init
          chmod a+x $GITHUB_WORKSPACE/.github/tfcmt/tfwrapper.sh
          terragrunt run-all plan -no-color -input=false --terragrunt-tfpath $GITHUB_WORKSPACE/.github/tfcmt/tfwrapper.sh

      # actions の workflow summary に plan 結果の summary を通知する
      - name: Create Job Summary
        if: always()
        uses: actions/github-script@v6
        with:
          github-token: ${{ secrets.GITHUB_TOKEN }}
          script: |
            const fs = require(&#39;fs&#39;);
            const filePath = &#39;/tmp/tgplan.md&#39;;
            const output = fs.readFileSync(filePath, &#39;utf8&#39;);
            await core.summary
              .addHeading(&#39;Terragrunt plan report&#39;)
              .addRaw(output)
              .write();</code></pre></div>



<p>まず terragrunt plan を実行するための IAM 権限の Assume Role と、tfcmt をローカルにインストール処理を実行後、メインとなる terragrunt run-all plan を実行しています。</p>



<p><a href="#tfwrapper.sh">ここで補足した通り</a>、tfcmt が直接 terragrunt run-all plan を実行することができないので、<code>--terragrunt-tfpath</code> オプションで wrapper スクリプトを呼び出し、そのスクリプト内で tfcmt を実行させています。</p>



<p>その後、<code>actions/github-script@v6</code> を使い、tfcmt が吐き出した md ファイルを Github の Summary 部分に書き込む処理を入れています。</p>



<p>結果、こんな感じで Summary に環境ごとの Plan 結果をまとめることができました！</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1024" height="489" src="https://tech-itoya.com/wp-content/uploads/2024/09/tori-20240920-3-1-1024x489.png" alt="" class="wp-image-125" srcset="https://tech-itoya.com/wp-content/uploads/2024/09/tori-20240920-3-1-1024x489.png 1024w, https://tech-itoya.com/wp-content/uploads/2024/09/tori-20240920-3-1-300x143.png 300w, https://tech-itoya.com/wp-content/uploads/2024/09/tori-20240920-3-1-768x366.png 768w, https://tech-itoya.com/wp-content/uploads/2024/09/tori-20240920-3-1-1536x733.png 1536w, https://tech-itoya.com/wp-content/uploads/2024/09/tori-20240920-3-1.png 1878w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<h2 class="wp-block-heading"><span id="toc7">改善ポイント</span></h2>



<p>使い勝手自体の改善は満足したので、このまま他のいろんなIaCリポジトリに横展開していきたいと考えています。</p>



<p>横展開する際、毎回 tfcmt の Config やらを設定するのは面倒なので、共通部品化して Actions モジュールを他の IaC リポジトリに配布できるといいなと考えています。機会があればチャレンジしてみようと思います。</p>



<p>tfcmt のおかげで Terraform の CI 体験が大分改善しそうで楽しみです！</p>



<p></p>
		<div class="wpulike wpulike-heart " ><div class="wp_ulike_general_class wp_ulike_is_restricted"><button type="button"
					aria-label="いいねボタン"
					data-ulike-id="32"
					data-ulike-nonce="64829ffd8a"
					data-ulike-type="post"
					data-ulike-template="wpulike-heart"
					data-ulike-display-likers=""
					data-ulike-likers-style="popover"
					class="wp_ulike_btn wp_ulike_put_image wp_post_btn_32"></button><span class="count-box wp_ulike_counter_up" data-ulike-counter-value="+1"></span>			</div></div>
	]]></content:encoded>
					
					<wfw:commentRss>https://tech-itoya.com/terragrunt-tfcmt-github-actions-%e3%81%a7-plan-%e7%b5%90%e6%9e%9c%e3%81%ae-summary-%e3%82%92-github-%e4%b8%8a%e3%81%ab%e8%a1%a8%e7%a4%ba%e3%81%97%e3%81%9f%e3%81%84/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
