前回はローカルでDifyを動かしてみましたが、今回はAWSのEC2上でDifyを動かしてみます。
目次
実施作業
準備
Difyのインストールと実行方法については前回同様にDocker Composeを利用したデプロイを実施します。
今回は以下の手順で実行します。
- ユーザーデータ設定内容整理
- EC2インスタンス構築(Difyインストールはユーザーデータにて実施)
前提
手順を開始する時点の前提条件は以下です。
- VPC/Subnetは構築済み
- EC2はPublic Subnet上に構築
- Difyはユーザーデータを利用してインストール・デプロイする
- Docker Composeのインストールもユーザーデータを利用する
- EC2に設定するSecurity Groupは作成済み(インバウンドは自宅IPからのHTTP接続/SSH接続のみ許可)
ユーザーデータ設定内容整理
最小限のユーザーデータの内容をAIで出力し、念のためDocker ComposeのGithubから手順が問題なさそうかを確認します。
Docker Composeのバージョンは2025/9/8時点の最新であるv2.39.2を利用します。
#!/bin/bash
# システムアップデート
dnf update -y
# Dockerのインストール
dnf install -y docker
systemctl start docker
systemctl enable docker
# ec2-userをdockerグループに追加
usermod -a -G docker ec2-user
# Gitのインストール(Amazon Linux 2023には通常含まれているが念のため)
dnf install -y git
# Docker Compose(V2)のインストール
DOCKER_COMPOSE_VERSION="v2.39.2"
mkdir -p /usr/local/lib/docker/cli-plugins
curl -SL "https://github.com/docker/compose/releases/download/${DOCKER_COMPOSE_VERSION}/docker-compose-linux-x86_64" -o /usr/local/lib/docker/cli-plugins/docker-compose
chmod +x /usr/local/lib/docker/cli-plugins/docker-compose
# Difyのクローンとセットアップ
cd /home/ec2-user
git clone https://github.com/langgenius/dify.git --branch 1.8.1
cd dify/docker
cp .env.example .env
# ファイルの所有権をec2-userに変更
chown -R ec2-user:ec2-user /home/ec2-user/dify
# Docker Composeでサービス起動
sudo -u ec2-user docker compose up -d
# ログファイルに完了メッセージを出力
echo "Dify installation completed at $(date)" >> /var/log/cloud-init-output.log
EC2インスタンス構築
ユーザーデータが整理できたらマネジメントコンソールからEC2インスタンスを構築していきます。 以下のようなパラメータを設定して「Launch instance」ボタンを押下します。
- Number of instances:1
- Name:Dify-Server
- AMI:Amazon Linux 2023 kernel-6.1 AMI(ami-07faa35bbd2230d90)
- Architecture:x86_64
- Instance Type:t3.mefium
- Key pair:既存のキーペア名を指定
- Network settings:既存のVPC、Public Subnetを指定
- Auto-assign public IP:Enable
- Security Group:既存のSGを指定
- Storage:20GiB、gp3
- Advanced details
- User data:先ほど作成したユーザーデータを入力
- その他:デフォルト



無事EC2インスタンスの作成が開始されました。
Difyのインストールと起動に時間がかかるので少し待ちます。

Dify起動確認
EC2にはSSHでもログインできるようにしているので、WSLなどからSSHでログインします。
ユーザーデータの実行ログを以下コマンドで確認してみます。
cat /var/log/cloud-init-output.log
ログを見るとDockerのインストールは成功してそうです。
Installed: ~~ docker-25.0.8-1.amzn2023.0.5.x86_64 ~~
gitのインストールも成功しています。
Installed: git-2.50.1-1.amzn2023.0.1.x86_64 git-core-2.50.1-1.amzn2023.0.1.x86_64 git-core-doc-2.50.1-1.amzn2023.0.1.noarch ~~
Difyのクローンも成功してそうです。
Cloning into 'dify'... Note: switching to 'c7700ac1762a4feccf60211d3dca3e39ec65a83c'. You are in 'detached HEAD' state. You can look around, make experimental changes and commit them, and you can discard any commits you make in this state without impacting any branches by switching back to a branch. If you want to create a new branch to retain commits you create, you may do so (now or later) by using -c with the switch command. Example: git switch -c <new-branch-name> Or undo this operation with: git switch -
ログの最後を確認するとユーザーデータの最後で出力するメッセージが出力されていました。
~~ Container docker-api-1 Starting Container docker-db-1 Healthy Container docker-plugin_daemon-1 Starting Container docker-worker-1 Started Container docker-worker_beat-1 Started Container docker-api-1 Started Container docker-nginx-1 Starting Container docker-plugin_daemon-1 Started Container docker-nginx-1 Started Dify installation completed at Mon Sep 8 11:02:53 UTC 2025 Cloud-init v. 22.2.2 finished at Mon, 08 Sep 2025 11:02:54 +0000. Datasource DataSourceEc2. Up 184.73 seconds
difyのコンポーネントが起動しているかを確認します。
見た限りは前回ローカルで確認したコンポーネントと同じコンテナが起動されてそうです。
$ cd dify/docker $ docker compose ps NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS docker-api-1 langgenius/dify-api:1.8.1 "/bin/bash /entrypoi…" api 9 minutes ago Up 9 minutes 5001/tcp docker-db-1 postgres:15-alpine "docker-entrypoint.s…" db 9 minutes ago Up 9 minutes (healthy) 5432/tcp docker-nginx-1 nginx:latest "sh -c 'cp /docker-e…" nginx 9 minutes ago Up 9 minutes 0.0.0.0:80->80/tcp, [::]:80->80/tcp, 0.0.0.0:443->443/tcp, [::]:443->443/tcp docker-plugin_daemon-1 langgenius/dify-plugin-daemon:0.2.0-local "/bin/bash -c /app/e…" plugin_daemon 9 minutes ago Up 9 minutes 0.0.0.0:5003->5003/tcp, [::]:5003->5003/tcp docker-redis-1 redis:6-alpine "docker-entrypoint.s…" redis 9 minutes ago Up 9 minutes (healthy) 6379/tcp docker-sandbox-1 langgenius/dify-sandbox:0.2.12 "/main" sandbox 9 minutes ago Up 9 minutes (healthy) docker-ssrf_proxy-1 ubuntu/squid:latest "sh -c 'cp /docker-e…" ssrf_proxy 9 minutes ago Up 9 minutes 3128/tcp docker-weaviate-1 semitechnologies/weaviate:1.19.0 "/bin/weaviate --hos…" weaviate 9 minutes ago Up 9 minutes docker-web-1 langgenius/dify-web:1.8.1 "/bin/sh ./entrypoin…" web 9 minutes ago Up 9 minutes 3000/tcp docker-worker-1 langgenius/dify-api:1.8.1 "/bin/bash /entrypoi…" worker 9 minutes ago Up 9 minutes 5001/tcp docker-worker_beat-1 langgenius/dify-api:1.8.1 "/bin/bash /entrypoi…" worker_beat 9 minutes ago Up 9 minutes 5001/tcp
Difyアクセス
EC2のPublic IPv4 addressのIPアドレスを確認し、以下のURLでDifyにアクセスしてみます。
[http://your_server_ip/install]
想定通りDifyの管理者アカウントの設定画面が表示されました。

セットアップ完了後はDifyのTop画面が表示されました。

感想及び所感
ローカルで実施した内容をほぼそのままEC2上で実行しただけなので苦もなく作成ができました。 せっかく作成したので次はIaCツールを使用してDifyを起動してみたいと思います。
この記事がどなたかの参考になれば幸いです。