Dify Self-Hosted on AWS

repository·main·Indexed 41 days ago

https://github.com/aws-samples/dify-self-hosted-on-aws

A project to self-host the Dify LLM application development platform on AWS using AWS CDK. It utilizes managed services including Aurora Serverless v2, ECS Fargate, and ElastiCache. Features include integration with Amazon Bedrock Knowledge Bases for RAG, support for closed network VPC deployments, and configurable scaling for ECS, Redis, and Postgres.

Tokens
11.6K
Snippets
25
Records
50
Agent score
50%

What's inside Dify

  1. Connect Dify to Amazon Bedrock Knowledge Bases

    main

    This project provides an External Knowledge Base API designed to bridge Dify with Amazon Bedrock Knowledge Bases. This allows Dify to leverage Bedrock's managed RAG (Retrieval-Augmented Generation) capabilities as an external knowledge source.

    To implement this connection, you must follow the Dify guide for connecting to an external knowledge base.

  2. Setup Dify to use Amazon Bedrock

    main

    To use Bedrock LLMs in Dify, you must first enable the desired models in the AWS Bedrock management console. Once enabled, follow these steps in the Dify UI:

    1. Log in to Dify.
    2. Click your profile in the top-right corner.
    3. Navigate to WORKSPACE -> Model Provider.
    4. Select AWS Bedrock model.
    5. Select the AWS region where your models are enabled.
    6. Click Save.

    IAM policies are pre-configured by this deployment to allow access to Bedrock.

  3. Deploy Dify on AWS using CloudShell

    main

    If you are using an environment with limited storage, such as AWS CloudShell, you can use the provided simple-deploy.sh script to automate the process. This script will guide you through the deployment via interactive prompts and eventually output the DifyOnAwsStack.DifyUrl.

    git clone https://github.com/aws-samples/dify-self-hosted-on-aws.git
    cd dify-self-hosted-on-aws
    ./simple-deploy.sh
  4. Prerequisites for Dify on AWS deployment

    main

    Before deploying, ensure your local environment or deployment instance meets the following requirements:

    • Node.js: version v22 or newer
    • Docker: installed and running
    • AWS CLI: installed and configured with an IAM profile that has the Administrator policy attached
  5. Deploy Dify on AWS using CDK

    main

    To deploy Dify using the AWS CDK, ensure you have configured your environment and then follow the standard npm and CDK command sequence. You can customize deployment parameters like AWS regions by modifying bin/cdk.ts. For a full list of configurable parameters, refer to the EnvironmentProps interface in lib/environment-props.ts.

    # 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
  6. Add Python packages to Dify code execution

    main

    You can extend the available Python libraries in Dify's code execution feature by editing the requirements file.

    1. Edit ./lib/constructs/dify-services/docker/python-requirements.txt using the standard Requirements File Format.
    2. If certain libraries require additional system calls, you can enable them by setting the allowAnySysCalls flag in bin/cdk.ts.
    WARNING

    Enabling allowAnySysCalls allows all system calls in the Dify sandbox. Only do this if you fully trust the code being executed in your Dify tenant.

  7. Connect to Dify API container for maintenance

    main

    To run manual commands (e.g., for migrations or maintenance), use the ConsoleConnectToTaskCommand output provided by the CDK deployment. This command uses AWS ECS Execute Command.

    # Example command structure
    aws ecs execute-command \
      --region <region> \
      --cluster <cluster-name> \
      --container Main \
      --interactive \
      --command "bash" \
      --task <task-id>
  8. Connect Dify to Amazon Bedrock Knowledge Bases

    main

    You can connect Dify to Amazon Bedrock Knowledge Bases using the External Knowledge Base feature. The external knowledge API is deployed as a sidecar to the Dify API.

    Step 1: Add External Knowledge API in Dify

    1. In Dify, go to Knowledge -> click Add an External Knowledge API.
    2. Fill in the form:
      • Name: e.g., Bedrock Knowledge Bases
      • API Endpoint: http://localhost:8000
      • API Key: dummy-key (Note: This key is validated against the BEARER_TOKEN environment variable in ./lib/constructs/dify-services/api.ts).

    Step 2: Connect the Knowledge Base

    1. In Dify, go to Knowledge -> Create Knowledge -> Connect to an External Knowledge Base.
    2. Fill in the form:
      • External Knowledge Name / Description: Any string.
      • External Knowledge API: Select the API created in Step 1.
      • External Knowledge ID: The Bedrock Knowledge Base ID.
        • Note: The default region is us-west-2. To use a different region, use the format region:ID (e.g., us-east-1:QWERTYASDF).
  9. Connect Dify to Notion

    main

    To enable Notion integration, follow these steps:

    1. Get Notion Token: Obtain a Notion Secret Token from Notion Developer Settings.
    2. Store in Secrets Manager:
      NOTION_INTERNAL_SECRET="YOUR_NOTION_TOKEN"
      aws secretsmanager create-secret \
         --name NOTION_INTERNAL_SECRET \
         --description "Secret for Notion internal use" \
         --secret-string ${NOTION_INTERNAL_SECRET}
    3. Configure CDK: Update bin/cdk.ts to inject the secret and set the integration type:
      export const props: EnvironmentProps = {
        additionalEnvironmentVariables: [
          {
            key: 'NOTION_INTEGRATION_TYPE',
            value: 'internal',
            targets: ['api'], 
          },
          {
            key: 'NOTION_INTERNAL_SECRET',
            value: { secretName: 'NOTION_INTERNAL_SECRET'},
            targets: ['api'], 
          },
        ],
      }
    4. Deploy: Run cdk deploy.
  10. Upgrade Dify without automatic migrations

    main

    If you need to handle breaking changes manually during an upgrade:

    1. Set autoMigration: false in lib/dify-on-aws-stack.ts within the ApiService construct.
    2. Deploy the project with the new Dify version.
    3. Use the ConsoleConnectToTaskCommand to connect to the container.
    4. Run the required migration or maintenance commands manually.
    5. Set autoMigration: true and deploy again to re-enable automatic migrations.