🐈

Terraform workspaceを使って、個人ごとのリソースを管理してみる

2023/04/01に公開

概要

Terraform には、workspaceという機能があります。

https://developer.hashicorp.com/terraform/language/state/workspaces

個人ごとに定義ファイルを分けていくのは、1名・2名の場合は問題ないのですが、10名とかになると、管理が大変になってきます。

そこで、workspaceを使って、個人ごとのリソースを管理してみました。

事前準備

AWS IAMの設定

公式ドキュメントを参考に、IAMユーザーを作成してください。

https://docs.aws.amazon.com/ja_jp/IAM/latest/UserGuide/id_users_create.html

AWS Cliのインストール

$ sudo easy_install pip
$ sudo pip install awscli

# or

$ brew install awscli

AWS CLIのprofileの設定

$ aws configure --profile <profile名>

AWS Access Key ID [None]: アクセスキー ID
AWS Secret Access Key [None]: シークレットアクセスキー
Default region name [None]: ap-northeast-1
Default output format [None]: json

Terraformのインストール

tfenvを使って、Terraformのバージョンを管理します。

https://github.com/tfutils/tfenv

$ brew install tfenv
$ tfenv install # 任意のバージョンを指定してください ex 1.0.4
$ tfenv use # インストールしたバージョンを指定してください ex 1.0.4

workspaceの作成

https://developer.hashicorp.com/terraform/cli/commands/workspace

$ terraform workspace new <workspace名>

# 例
$ terraform workspace new test
Created and switched to workspace "test"!

$ terraform workspace list       
  default
* test

プロバイダーの設定

AWSのプロバイダーを設定します。

作業ディレクトリを作成して、以下のファイルを作成してください。

$ mkdir terraform
$ cd terraform
$ touch main.tf
main.tf
terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 4.0"
    }
  }
}

provider "aws" {
  region = "ap-northeast-1"
  profile = "default"
}

リソースの作成

今回は、VPC と 個人ごとのALB target groupを作成してみます。

先ほどの作業ディレクトリに、以下のファイルを作成してください。

$ touch vpc.tf
$ touch alb.tf

VPCの作成

vpc.tf
resource "aws_vpc" "test" {
  cidr_block = "10.0.0.0/16"

  tags = {
    Name = "test"
  }
}

ALB target groupの作成

variable で実行時に個人名を指定してもらうようにしています。

ALBの名前は32文字以内である必要がありますので、注意してください。

alb.tf
variable "personal_name_prefix" {
  type        = string
  description = <<-EOF
    個人環境ごとのPrefixを指定してください。
    指定方法:[firstname]-[lastname]
  EOF
}

resource "aws_alb_target_group" "test" {
  name     = var.personal_name_prefix
  port     = 80
  protocol = "HTTP"
  vpc_id   = aws_vpc.test.id
}

実行計画の確認

今回は新規でリソースを作成するので、terraform plan で実行計画を確認してみます。

$ terraform plan -var personal_name_prefix=test

terraform plan -var personal_name_prefix=test

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # aws_alb_target_group.test will be created
  + resource "aws_alb_target_group" "test" {
      + arn                                = (known after apply)
      + arn_suffix                         = (known after apply)
      
      # 以下割愛
    }

  # aws_vpc.test will be created
  + resource "aws_vpc" "test" {
      + arn                                  = (known after apply)
      + cidr_block                           = "10.0.0.0/16"
      
      # 以下割愛
    }

Plan: 2 to add, 0 to change, 0 to destroy.

リソースの作成

terraform apply でリソースを作成してみます。

$ terraform apply -var personal_name_prefix=test

~~~ 省略 ~~~
Apply complete! Resources: 2 added, 0 changed, 0 destroyed.

結果

リソースがAWS上に作成されていると思います。

また、ワークスペースによって、個人ごとのリソースが作成されていることが確認できます。

$ terraform workspace list
  default
* test
 
# terraform.tfstate.d にワークスペースごとのファイルが作成されていることが確認できます。
$ ls terraform.tfstate.d/
  test

お掃除

使わないリソースは、terraform destroy で削除してください。

$ terraform destroy -var personal_name_prefix=test

Destroy complete! Resources: 1 destroyed.

まとめ

workspaceを使うことで、個人ごとのリソースを管理することができました。

今回は、VPCとALB target groupを作成しましたが、他にも、EC2やRDSなど、個人ごとに作成するリソースがあれば、workspaceを使って管理すると良いと思います。

GitHubで編集を提案

Discussion