ホーム

【Terraform入門】メリットを理解してmacにインストールする

作成日: 2023-12-28

更新日: 2024-1-7

【Terraform入門】メリットを理解してmacにインストールする

この記事を読んだ後に理解できること

  • Terraformの概要
  • Terraformのメリット・デメリット
  • macへのTerraformのインストール方法

Terraformとは

概要

Terraformとは、AWS等のインフラリソースをコードによって管理・反映ができます。

Terraformのようにインフラリソースを管理するツールをInfrastructure as Code、略してIaCと呼びます。

なお、複数のインフラリソースを管理することができるツールは、Terraform以外にもPulumiなどもあります。

Terraformを使うメリット

インフラリソースをコード管理することで得られる最大のメリットは、

  • PR等のコードレビューを通して、リソースの変更ができる
  • 操作ミスや設定ミスといったヒューマンエラーを減らすことができる
  • コードを通して、既存のインフラリソースを把握できる
  • Terraformだけで複数のインフラリソース(AWS、GCP、Azure等)管理が可能

になるかと思います。

Terraformのデメリット

Terraformのデメリットは、

  • 学習コストが高い

ことになります。

Terraformはインフラリソースをコードで管理することができます。

当然、コードの書き方等を理解する必要があるので、導入時における学習コストは高いと言えるでしょう。

macにTerraformをインストールする

Homebrewでリポジトリを追加する

Terraformをmacにインストールさせる方法はいくつかあります。

今回は、Homebrewを使ったインストール方法について解説します。

まずは、HomebrewにTerraformの開発元であるHashiCorpのリポジトリを追加する必要があります。

$ brew tap hashicorp/tap

$ brew tap コマンドを使うことでHomebrewからデフォルトでインストールできない外部リポジトリをインストールすることができます。

brewコマンドでTerraformをインストールする

その後、インストールしたらリポジトリからTerraformをインストールすることができます。

$ brew install hashicorp/tap/terraform

Terraformがローカルにインストールされているか確認します。

$ terraform -help

Usage: terraform [global options] <subcommand> [args]

The available commands for execution are listed below.
The primary workflow commands are given first, followed by
less common or more advanced commands.

Main commands:
  init          Prepare your working directory for other commands
  validate      Check whether the configuration is valid
  plan          Show changes required by the current configuration
  apply         Create or update infrastructure
  destroy       Destroy previously-created infrastructure

All other commands:
  console       Try Terraform expressions at an interactive command prompt
  fmt           Reformat your configuration in the standard style
  force-unlock  Release a stuck lock on the current workspace
  get           Install or upgrade remote Terraform modules
  graph         Generate a Graphviz graph of the steps in an operation
  import        Associate existing infrastructure with a Terraform resource
  login         Obtain and save credentials for a remote host
  logout        Remove locally-stored credentials for a remote host
  metadata      Metadata related commands
  output        Show output values from your root module
  providers     Show the providers required for this configuration
  refresh       Update the state to match remote systems
  show          Show the current state or a saved plan
  state         Advanced state management
  taint         Mark a resource instance as not fully functional
  test          Execute integration tests for Terraform modules
  untaint       Remove the 'tainted' state from a resource instance
  version       Show the current Terraform version
  workspace     Workspace management

Global options (use these before the subcommand, if any):
  -chdir=DIR    Switch to a different working directory before executing the
                given subcommand.
  -help         Show this help output, or the help for a specified subcommand.
  -version      An alias for the "version" subcommand.

インストールは完了です。

Terraformコマンドがローカル上で実行可能になります。

Terraformのコードは拡張子が .tfファイルに書くことができます。

最後に

Terraformを使うことでインフラリソースの安全な取り扱い、管理が可能になります。

学習コストが高く、導入へのハードルは高いですが、相応の恩恵は存在するかと思います。