AWS
Terraform

Terraformを使ってAWS環境を構築するやり方(Workspace機能)

前回の続きになります。
環境ごとにリソース名の分岐するための機能です。

コマンド説明
Usage: terraform workspace

  Create, change and delete Terraform workspaces.

Subcommands:
    delete    Delete a workspace
    list      List Workspaces
    new       Create a new workspace
    select    Select a workspace
    show      Show the name of the current workspace
  • 開発環境・本番環境の2つを作成します。

$ sh bin/terraform_aws.sh workspace new dev

コマンドログ
Created and switched to workspace "dev"!

You're now on a new, empty workspace. Workspaces isolate their state,
so if you run "terraform plan" Terraform will not see any existing state
for this configuration.`

$ sh bin/terraform_aws.sh workspace new prod

コマンドログ
Created and switched to workspace "prod"!

You're now on a new, empty workspace. Workspaces isolate their state,
so if you run "terraform plan" Terraform will not see any existing state
for this configuration.`

  • 環境の切り替え方法について

$ sh bin/terraform_aws.sh workspace select dev

コマンドログ
Switched to workspace "dev".

環境変数を使って、リソース名を切り替えます。

${terraform.workspace}

本家サイトドキュメント
https://www.terraform.io/docs/state/workspaces.html