🧑‍💻

AWS上でDify環境を作ってくれ、とお願いされたときにパッと作る方法

に公開

はじめに

AWS上でDify環境を作ってくれと依頼されたので、簡単に手順をまとめました。
Cloud Formationを使えば簡単に構築できたので、記事の内容はそんなに大したことありません。

結論

aws-samples/dify-self-hosted-on-awsを使う

手順

$ git clone git@github.com:aws-samples/dify-self-hosted-on-aws.git
$ cd dify-self-hosted-on-aws

# デフォルトだとリージョンがusなので変更する
# 複数想定してStackの命名を少し変える: 今回はDifyOnAwsStack-PJ-XXX
$ vim bin/cdk.ts
bin/cdk.ts
diff --git a/bin/cdk.ts b/bin/cdk.ts
index 16f9954..13b2809 100644
--- a/bin/cdk.ts
+++ b/bin/cdk.ts
@@ -6,7 +6,7 @@ import { UsEast1Stack } from '../lib/us-east-1-stack';
 import { EnvironmentProps } from '../lib/environment-props';
 
 export const props: EnvironmentProps = {
-  awsRegion: 'us-west-2',
+  awsRegion: 'ap-northeast-1',
   awsAccount: process.env.CDK_DEFAULT_ACCOUNT!,
   // Set Dify version
   difyImageTag: '1.3.1',
@@ -28,7 +28,7 @@ let virginia: UsEast1Stack | undefined = undefined;
 if ((props.useCloudFront ?? true) && (props.domainName || props.allowedIPv4Cidrs || props.allowedIPv6Cidrs)) {
   // add a unique suffix to prevent collision with different Dify instances in the same account.
   virginia = new UsEast1Stack(app, `DifyOnAwsUsEast1Stack${props.subDomain ? `-${props.subDomain}` : ''}`, {
-    env: { region: 'us-east-1', account: props.awsAccount },
+    env: { region: 'ap-northeast-1', account: props.awsAccount },
     crossRegionReferences: true,
     domainName: props.domainName,
     allowedIpV4AddressRanges: props.allowedIPv4Cidrs,
@@ -36,7 +36,7 @@ if ((props.useCloudFront ?? true) && (props.domainName || props.allowedIPv4Cidrs
   });
 }

-new DifyOnAwsStack(app, 'DifyOnAwsStack', {
+new DifyOnAwsStack(app, 'DifyOnAwsStack-PJ-XXX', {
   env: { region: props.awsRegion, account: props.awsAccount },
   crossRegionReferences: true,
   ...props,

あとはReadme.mdにある通りコマンドを実行し、CloudFormationをデプロイしましょう

# install npm dependencies
$ npm ci
# bootstrap the AWS account (required only once per account and region)
$ npx cdk bootstrap
# deploy the CDK stack
$ npx cdk deploy --all

その他のTips

Difyの環境変数の値を変更したい場合

Difyの環境変数を変えたい場合(例えば、タイムアウトの時間を伸ばしたい場合など)は、additionalEnvironmentVariablesをパラメータをEnvironmentPropsに追加すると良さそうです。コードはこのあたり

bin/cdk.ts
export const props: EnvironmentProps = {
  ...省略
  // Please see EnvironmentProps in lib/environment-props.ts for all the available properties
  additionalEnvironmentVariables: [
    {
      key: "GUNICORN_TIMEOUT",
      value: "6000",
      targets: [
        "api"
      ]
    }
  ]
};

ランニングコストを少しでも安く運用したい場合

ClassmethodさんのDevelopersIOを記事が参考になると思います。

まとめ

aws-samples/dify-self-hosted-on-awsを使えば、簡単にAWS環境上でDifyを作ることができました!

余談

実は、AWS上にはDifyPremiumと呼ばれるAMIがあるのですが(公式サイトで紹介されている)、バージョンが古かったりちょいと高めだったりして、使い勝手悪かったのでいまいちでした(バージョン更新も試してみたのだが、うまくいかなくて諦めた)

株式会社エクスプラザ

Discussion