1. Introduction#
This article describes how to self-host dify, which allows you to easily create AI applications on WEBUI, on Google Cloud Platform (GCP) using terraform.
What you can do with this article#
- Self-hosting dify on gcp using terraform
Who is this article for?#
- Those who want to easily publish AI applications externally
- Those who want to operate dify not only locally but also in the cloud
Operating environment#
-OS version - MacOS Sequoia 15.5
Advance preparation#
Clone the repository#
When hosting dify with gcp, there is a repository below that publishes configuration files for terraform, which automates infrastructure construction on the cloud, so clone it.
git clone https://github.com/DeNA/dify-google-cloud-terraformInstalling packages#
Installing terraform#
brew install terraformInstalling google cloud sdk#
brew install --cask google-cloud-sdkCreate gcp project#
Create a project with any name from the console. I will omit the details.
Login to gcp from cli#
gcloud auth loginAfter entering the command, a web page will open and you can log in.
Create a bucket for storing tfvars#
Create storage for managing terraform state in the cloud Select buckets from the GCP console. Create a bucket with any name
Modify the file#
Modify the repository you cloned earlier and customize it for yourself.
Fix gitignore#
Add to gitignore to prevent confidential information from being uploaded remotely
terraform.tfvars*terraform/environments/dev The *terraform/environments/prod Copy it with the name.
Modification of provider.tf#
Apply the bucket you created earlier to terraform state management
terraform {
backend "gcs" {
+ bucket = "<バケットの名前>"
prefix = "dify"
}
}Fixed tfvars#
Modify terraform.tfvars in the copied prod Regarding the key,
openssl rand -base64 42It would be a good idea to create it by running
+ project_id = "例:dify-sample-app"
+ region = "例:asia-northeast1"
+ plugin_daemon_key = "<opensslコマンドで作成>"
+ plugin_dify_inner_api_key = "<opensslコマンドで作成>"Deploy#
Once you’ve reached this point, it’s time to deploy to GCP.
terraform initialization#
cd terraform/environments/prod
terraform initCreate Artifact Registry repository#
terraform apply -target=module.registryBuild and push container image#
cd ../../..
sh ./docker/cloudbuild.sh <project-id> <region>terraform planning#
cd terraform/environments/dev
terraform planApply terraform#
terraform applyAttention#
API-related errors may occur while applying terraform. In that case, follow the error message and enable the necessary APIs. Just press the enable button from the GCP console
Reference materials#
https://github.com/DeNa/dify-google-cloud-terraform