以下の内容はhttps://kiririmode.hatenablog.jp/entry/20200510/1589087089より取得しました。


module に対する terraform validate が "The argument "region" is required, but was not set." エラーで失敗する

GitLab CI/CD を使い、自作の AWS 用 terraform モジュールに対して terraform validate をかけようとすると、以下のエラーメッセージが出て失敗しました。

Error: Missing required argument
The argument "region" is required, but was not set.

原因

これはどうも terraform validate が AWS Provider の宣言があることを前提に validation を行おうとしているためのようです。 正確にいえば、Provider のスキーマを前提にチェックをかけます。そして、Provider のスキーマ上は region の設定が必須になります。

これがルートモジュールであれば region を設定すれば良いのですが、子モジュールだとそうはいきません。 公式ドキュメントにおいて、子モジュールで provider block を定義することが非推奨であると記載されているためです。

While in principle provider blocks can appear in any module, it is recommended that they be placed only in the root module of a configuration, since this approach allows users to configure providers just once and re-use them across all descendent modules.

Modules Landing Page - Configuration Language | Terraform by HashiCorp

対応策

現在 (v0.12.24) の terraform では如何ともしがたいのですが、要求される region を環境変数で宣言するようにして回避可能です。 ぼくは、 .gitlab-ci.yml にグローバルな変数として突っ込むようにしています。

variables:
  AWS_DEFAULT_REGION: ap-northeast-1

参考




以上の内容はhttps://kiririmode.hatenablog.jp/entry/20200510/1589087089より取得しました。
このページはhttp://font.textar.tv/のウェブフォントを使用してます

不具合報告/要望等はこちらへお願いします。
モバイルやる夫Viewer Ver0.14