0
0

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を使ってdifyをGCP上にセルフホスティングする

Last updated at Posted at 2025-05-24

1. はじめに

AIアプリがWEBUI上で簡単に作れるdifyを,terraformを使用してGoogle Cloud Platform(gcp)上でセルフホスティングする方法を記述します.

この記事でできること

  • terraformを用いたgcp上でのdifyのセルフホスティング

この記事の対象者

  • AIアプリを簡単に外部公開したい方
  • difyをローカルだけでなく,クラウドで運用したい方

動作環境

  • OS バージョン
    • MacOS Sequoia 15.5

事前準備

リポジトリのクローン

difyをgcpでホスティングする場合,クラウド上でのインフラ構築などを自動化するterraformの設定ファイルを公開してくれている以下のリポジトリがあるのでクローンします.

git clone https://github.com/DeNA/dify-google-cloud-terraform

パッケージのインストール

terraformのインストール

brew install terraform

google cloud sdkのインストール

brew install --cask google-cloud-sdk

gcpプロジェクトの作成

コンソールから任意の名前でプロジェクトを作成します.
詳細は割愛します.

cliからgcpにログイン

gcloud auth login

コマンド入力後webページが開くのでログインします.

tfvars保管用バケットの作成

terraformの状態をクラウドで管理するためのストレージを作成します
GCPコンソールからbucketsを選択します.
任意の名前でバケットを作成しておいてください

ファイルの修正

先ほどクローンしてきたリポジトリを修正して,自分用にカスタムします.

gitignoreの修正

機密情報をリモートに上げないように,gitignoreに追記します

.gitignore
terraform.tfvars
  • terraform/environments/dev
  • terraform/environments/prod
    という名前でコピーします.

provider.tfの修正

先ほど作成したバケットをterraformの状態管理に適用します

provider.tf
terraform {
  backend "gcs" {
+   bucket = "<バケットの名前>"
    prefix = "dify"
  }
}

tfvarsの修正

コピーしたprod内のterraform.tfvarsを修正します
keyについては,

openssl rand -base64 42

を実行して,作成するといいでしょう

terraform.tfvars
+ project_id = "例:dify-sample-app"
+ region = "例:asia-northeast1"
+ plugin_daemon_key = "<opensslコマンドで作成>"
+ plugin_dify_inner_api_key = "<opensslコマンドで作成>"

デプロイ

ここまできたらいよいよGCPにデプロイします

terraform初期化

cd terraform/environments/prod
terraform init

Artifact Registry リポジトリを作成

terraform apply -target=module.registry

コンテナイメージをビルド&プッシュ

cd ../../..
sh ./docker/cloudbuild.sh <project-id> <region>

terraformプランニング

cd terraform/environments/dev
terraform plan

terraform適用

terraform apply

注意

terraformの適用中にapi関連のエラーが発生することがあります,その場合はエラー文に従って,必要なapiを有効化してください.
GCPコンソールから有効化ボタン押すだけです

参考資料

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?