2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

TerraformでAuth0環境構築

Last updated at Posted at 2025-02-17

目的・完了基準

目的

Terraformを使ってAuth0環境構築を行えるようにする。

完了基準

公式提供のサンプルWebアプリケーションを使って実際にログイン・ログアウトが行えることを確認する。

前提

環境

環境
OS Mac(M1)
Terraformバージョン 1.9.7
Auth0プロバイダーバージョン 1.12.0

Auth0とは

下記の記事がわかりやすいです。

Auth0入門 – ざっくり理解する認証/認可の基礎

まだ触ったことがないという人は、下記のハンズオンを試してみると良いでしょう。

初回登録時に22日間の無料期間が付与されるので、そこで体験しておくのがオススメです。

初めてのAuth0ハンズオン

参考記事

TerraformのAuth0プロバイダーがAuth0で完全にサポートされるようになりました #Auth0JP

Auth0 Terraform Provider: Auth0 の設定を Terraform で管理しよう

Auth0 のアプリケーションをTerraformから作る

手順

アカウント作成(未作成の場合のみ)

初めてのAuth0ハンズオンを参考にアカウントを作成しましょう。

Machine to Machine Applicationを手動で追加する

最初に、TerraformでAuth0を操作できるように設定します。

ここだけは手動で設定が必要です。

image-20250217092949459.png

image-20250217093209978.png

操作できるAPIの種類(実質的な権限設定)の選択を求められますが、今回は学習目的なのでAllを選択します。

image-20250217093404885.png

作成完了したら、Settingsタブをクリックし、Domain・ClientID・ClientSecretをメモしておきましょう。

Terraformから操作を行う際に必要になります。

image-20250217093702882.png

Auth0プロバイダー設定

ここからTerraformを記述していきます。

まずはProvider設定から。

terraform {
  required_providers {
    auth0 = {
      source  = "auth0/auth0"
      version = "~> 1.0.0"
    }
  }
}

provider "auth0" {
  domain = ${先程取得したDomainの値}
  client_id = ${先程取得したClientIdの値}
  client_secret = ${先程取得したClientSecretの値}
}

Auth0Client設定

このClient設定は、手動操作におけるApplication追加と同義です。

この後で利用するサンプルSPA用のClientを定義します。

# 後で利用するサンプルSPAで使うため、urlは"http://localhost:3000"とします
resource "auth0_client" "terraform_client" {
  name                = "terraform_client"
  app_type            = "spa" #SinglePageApplicationの略
  callbacks           = ["http://localhost:3000"]
  allowed_origins     = ["http://localhost:3000"]
  allowed_logout_urls = ["http://localhost:3000"]
  web_origins         = ["http://localhost:3000"]
  
  jwt_configuration {
    alg                 = "RS256" #HS256にすると後々の操作でエラーになります
    lifetime_in_seconds = 36000
  }
  
  oidc_conformant = true
}

resource "auth0_client_credentials" "terraform_credentials" {
	client_id = auth0_client.terraform_client.client_id
	authentication_method = "none"
}

Terraformオペレーション実行

# 初回のみ、初期化実施
terraform init

# 定義の適用
terraform apply

下記のような結果が返ってくればOKです。

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

実際にApplicationが追加されていることをAuth0のダッシュボードでも確認してみましょう。

image-20250217095737441.png

次の動作確認で利用するため、ClientIDをメモしておいてください。

サンプルSPAの起動

下記のAuth0動作確認用のサンプルSPAをgit cloneしましょう。

Auth0 React Samples

Sample-01 > auth.config.json.exampleをコピーしてauth.config.jsonを同フォルダに作成します。

その後、中身の設定値を下記のように書き換えます。

{
  "domain": ${事前にメモしたドメイン名},
  "clientId": ${Terraformで作成したClientID}
}

準備が整ったので、ローカルサーバーを立ち上げましょう。

# 初回のみ実行
npm install

# 起動
npm start

ログイン・ログアウト確認

起動したアプリケーションの画面にてログインボタンを押下し、きちんとサインアップできることを確認しましょう。

image-20250217100931390.png

image-20250217101029942.png

無事ログイン完了すれば成功です。

image-20250217101100050.png

ついでにログアウトも確認しておくと良いです。

まとめ

TerraformでAuth0の設定を管理できるのは便利そうですね。

より詳しい情報をお求めの方は公式リファレンスを参照してください。

ネット上でもちらほら記事があるので、詰まるところがあればAIに聞いたりググったりすると良いでしょう。

ひとまずここまでお疲れ様でした!

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?