Terraformの開発環境
一人Terraform Advent Calendar
この記事は一人Terraform Advent Calendarの1日目の記事です。
目的
最近Terraformをガリガリ触っていたので、自分の知識と思考の整理も兼ねてTipsを25個の記事に分けて投稿していきます。主にAmazon Web Services(AWS)でTerraformを使う方法について書きます。
1日目のテーマは開発環境についてです。
エディタ, プラグインの準備
だいたいのエディタやIDEでTerraform用のプラグインが提供されています。CLIを併用する都合上、ターミナルが組み込まれたエディタを使うのが手軽で良いです。
私は主にVisualStudioCodeを使用していますが、補完機能とLinterと定義ジャンプ機能があるだけでだいぶ楽に感じます。
CLIツールのインストール
公式サイトからダウンロードできるやつを使います。
実行可能なバイナリが落ちてくるのでPATHに突っ込んであげてください。
$ terraform Usage: terraform [-version] [-help] <command> [args] The available commands for execution are listed below. The most common, useful commands are shown first, followed by less common or more advanced commands. If you're just getting started with Terraform, stick with the common commands. For the other commands, please read the help and docs before usage. Common commands: apply Builds or changes infrastructure console Interactive console for Terraform interpolations destroy Destroy Terraform-managed infrastructure env Workspace management fmt Rewrites config files to canonical format get Download and install modules for the configuration graph Create a visual graph of Terraform resources import Import existing infrastructure into Terraform init Initialize a Terraform working directory output Read an output from a state file plan Generate and show an execution plan providers Prints a tree of the providers used in the configuration push Upload this Terraform module to Atlas to run refresh Update local state file against real resources show Inspect Terraform state or plan taint Manually mark a resource for recreation untaint Manually unmark a resource as tainted validate Validates the Terraform files version Prints the Terraform version workspace Workspace management All other commands: debug Debug output management (experimental) force-unlock Manually unlock the terraform state state Advanced state management
AWSアカウントの準備
登録してしばらくは無料で使えるのでぜひこの機会に登録してみてください。
TerraformでAWSのリソースを管理する上でアクセスキーをシークレットアクセスキーを取得する必要がありますが、AWSアカウントを作成した直後に有効になっているルートアカウントでアクセスキーとシークレットアクセスキーをTerraform用に取得して使用するのはオススメできません。理由とかはこちらの記事に書いてある内容が参考になります。
Terraformの場合はTerraform用に専用のIAMユーザーを作成するのが良いでしょう。
おわりに
環境やツール類の準備はそれほど手間ではありません。結局HCLもJSON互換のコンフィグ記述言語でしかないので、Terraformのバイナリさえあれば特別なコンパイラやランタイムも必要ないです。
AWS以外のプロバイダでTerraformを利用したい場合にはそれに応じたアカウントや環境を準備する必要があります。