From 48c4a719b69c4da55ea32a03357b028ef7dbc033 Mon Sep 17 00:00:00 2001 From: Max Techera Date: Fri, 12 Apr 2024 14:03:27 -0300 Subject: [PATCH 1/5] Add copilot deploys --- .dockerignore | 2 + copilot/.workspace | 1 + .../environments/addons/addons.parameters.yml | 3 + .../environments/addons/flowise-cluster.yml | 149 ++++++++++++++++++ copilot/environments/staging/manifest.yml | 21 +++ copilot/flowise/manifest.yml | 64 ++++++++ packages/server/src/commands/start.ts | 9 ++ 7 files changed, 249 insertions(+) create mode 100644 copilot/.workspace create mode 100644 copilot/environments/addons/addons.parameters.yml create mode 100644 copilot/environments/addons/flowise-cluster.yml create mode 100644 copilot/environments/staging/manifest.yml create mode 100644 copilot/flowise/manifest.yml diff --git a/.dockerignore b/.dockerignore index 0f21e7f12af..f7d4fa8a4f7 100644 --- a/.dockerignore +++ b/.dockerignore @@ -32,6 +32,8 @@ **/build **/dist **/.sst +**/copilot +copilot docker node_modules diff --git a/copilot/.workspace b/copilot/.workspace new file mode 100644 index 00000000000..194b51fbcdf --- /dev/null +++ b/copilot/.workspace @@ -0,0 +1 @@ +application: flowise diff --git a/copilot/environments/addons/addons.parameters.yml b/copilot/environments/addons/addons.parameters.yml new file mode 100644 index 00000000000..cde0469ee73 --- /dev/null +++ b/copilot/environments/addons/addons.parameters.yml @@ -0,0 +1,3 @@ +Parameters: + VPCID: !Ref VPC + PrivateSubnets: !Join [ ',', [ !Ref PrivateSubnet1, !Ref PrivateSubnet2 ] ] \ No newline at end of file diff --git a/copilot/environments/addons/flowise-cluster.yml b/copilot/environments/addons/flowise-cluster.yml new file mode 100644 index 00000000000..4309de067f4 --- /dev/null +++ b/copilot/environments/addons/flowise-cluster.yml @@ -0,0 +1,149 @@ +Parameters: + App: + Type: String + Description: Your application's name. + Env: + Type: String + Description: The name of the environment being deployed. + # Customize your Aurora Serverless cluster by setting the default value of the following parameters. + flowiseclusterDBName: + Type: String + Description: The name of the initial database to be created in the Aurora Serverless v2 cluster. + Default: flowise + # Cannot have special characters + # Naming constraints: https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/CHAP_Limits.html#RDS_Limits.Constraints + VPCID: + Type: String + Description: The ID of the VPC in which to create the Aurora Serverless v2 cluster. + Default: "" + PrivateSubnets: + Type: String + Description: The IDs of the private subnets in which to create the Aurora Serverless v2 cluster. + Default: "" + +Mappings: + flowiseclusterEnvScalingConfigurationMap: + staging: + "DBMinCapacity": 0.5 # AllowedValues: from 0.5 through 128 + "DBMaxCapacity": 8 # AllowedValues: from 0.5 through 128 + + All: + "DBMinCapacity": 0.5 # AllowedValues: from 0.5 through 128 + "DBMaxCapacity": 8 # AllowedValues: from 0.5 through 128 + +Resources: + flowiseclusterDBSubnetGroup: + Type: 'AWS::RDS::DBSubnetGroup' + Properties: + DBSubnetGroupDescription: Group of private subnets for Aurora Serverless v2 cluster. + SubnetIds: + !Split [',', !Ref PrivateSubnets] + + flowiseclusterWorkloadSecurityGroup: + Metadata: + 'aws:copilot:description': 'A security group for one or more workloads to access the Aurora Serverless v2 cluster flowisecluster' + Type: 'AWS::EC2::SecurityGroup' + Properties: + GroupDescription: 'The Security Group to access Aurora Serverless v2 cluster flowisecluster.' + VpcId: !Ref VPCID + Tags: + - Key: Name + Value: !Sub 'copilot-${App}-${Env}-Aurora' + + flowiseclusterDBClusterSecurityGroup: + Metadata: + 'aws:copilot:description': 'A security group for your Aurora Serverless v2 cluster flowisecluster' + Type: AWS::EC2::SecurityGroup + Properties: + GroupDescription: The Security Group for the Aurora Serverless v2 cluster. + VpcId: !Ref VPCID + Tags: + - Key: Name + Value: !Sub 'copilot-${App}-${Env}-Aurora' + + flowiseclusterDBClusterSecurityGroupIngressFromWorkload: + Type: AWS::EC2::SecurityGroupIngress + Properties: + Description: Ingress from one or more workloads in the environment. + GroupId: !Ref flowiseclusterDBClusterSecurityGroup + IpProtocol: tcp + ToPort: 5432 + FromPort: 5432 + SourceSecurityGroupId: !Ref flowiseclusterWorkloadSecurityGroup + + flowiseclusterAuroraSecret: + Metadata: + 'aws:copilot:description': 'A Secrets Manager secret to store your DB credentials' + Type: AWS::SecretsManager::Secret + Properties: + Description: !Sub Aurora main user secret for ${AWS::StackName} + GenerateSecretString: + SecretStringTemplate: '{"username": "postgres"}' + GenerateStringKey: "password" + ExcludePunctuation: true + IncludeSpace: false + PasswordLength: 16 + flowiseclusterDBClusterParameterGroup: + Metadata: + 'aws:copilot:description': 'A DB parameter group for engine configuration values' + Type: 'AWS::RDS::DBClusterParameterGroup' + Properties: + Description: !Ref 'AWS::StackName' + Family: 'aurora-postgresql14' + Parameters: + client_encoding: 'UTF8' + + flowiseclusterDBCluster: + Metadata: + 'aws:copilot:description': 'The flowisecluster Aurora Serverless v2 database cluster' + Type: 'AWS::RDS::DBCluster' + Properties: + MasterUsername: + !Join [ "", [ '{{resolve:secretsmanager:', !Ref flowiseclusterAuroraSecret, ":SecretString:username}}" ]] + MasterUserPassword: + !Join [ "", [ '{{resolve:secretsmanager:', !Ref flowiseclusterAuroraSecret, ":SecretString:password}}" ]] + DatabaseName: !Ref flowiseclusterDBName + Engine: 'aurora-postgresql' + EngineVersion: '14.4' + DBClusterParameterGroupName: !Ref flowiseclusterDBClusterParameterGroup + DBSubnetGroupName: !Ref flowiseclusterDBSubnetGroup + Port: 5432 + VpcSecurityGroupIds: + - !Ref flowiseclusterDBClusterSecurityGroup + ServerlessV2ScalingConfiguration: + # Replace "All" below with "!Ref Env" to set different autoscaling limits per environment. + MinCapacity: !FindInMap [flowiseclusterEnvScalingConfigurationMap, All, DBMinCapacity] + MaxCapacity: !FindInMap [flowiseclusterEnvScalingConfigurationMap, All, DBMaxCapacity] + + flowiseclusterDBWriterInstance: + Metadata: + 'aws:copilot:description': 'The flowisecluster Aurora Serverless v2 writer instance' + Type: 'AWS::RDS::DBInstance' + Properties: + DBClusterIdentifier: !Ref flowiseclusterDBCluster + DBInstanceClass: db.serverless + Engine: 'aurora-postgresql' + PromotionTier: 1 + AvailabilityZone: !Select + - 0 + - !GetAZs + Ref: AWS::Region + + flowiseclusterSecretAuroraClusterAttachment: + Type: AWS::SecretsManager::SecretTargetAttachment + Properties: + SecretId: !Ref flowiseclusterAuroraSecret + TargetId: !Ref flowiseclusterDBCluster + TargetType: AWS::RDS::DBCluster + +Outputs: + flowiseclusterSecret: + Description: "The JSON secret that holds the database username and password. Fields are 'host', 'port', 'dbname', 'username', 'password', 'dbClusterIdentifier' and 'engine'" + Value: !Ref flowiseclusterAuroraSecret + Export: + Name: !Sub ${App}-${Env}-flowiseclusterAuroraSecret + flowiseclusterSecurityGroup: + Description: "The security group to attach to the workload." + Value: !Ref flowiseclusterWorkloadSecurityGroup + Export: + Name: !Sub ${App}-${Env}-flowiseclusterSecurityGroup diff --git a/copilot/environments/staging/manifest.yml b/copilot/environments/staging/manifest.yml new file mode 100644 index 00000000000..43264539592 --- /dev/null +++ b/copilot/environments/staging/manifest.yml @@ -0,0 +1,21 @@ +# The manifest for the "staging" environment. +# Read the full specification for the "Environment" type at: +# https://aws.github.io/copilot-cli/docs/manifest/environment/ + +# Your environment name will be used in naming your resources like VPC, cluster, etc. +name: staging +type: Environment + +# Import your own VPC and subnets or configure how they should be created. +# network: +# vpc: +# id: + +# Configure the load balancers in your environment, once created. +# http: +# public: +# private: + +# Configure observability for your environment resources. +observability: + container_insights: false diff --git a/copilot/flowise/manifest.yml b/copilot/flowise/manifest.yml new file mode 100644 index 00000000000..54e13e4badd --- /dev/null +++ b/copilot/flowise/manifest.yml @@ -0,0 +1,64 @@ +# The manifest for the "flowise" service. +# Read the full specification for the "Load Balanced Web Service" type at: +# https://aws.github.io/copilot-cli/docs/manifest/lb-web-service/ + +# Your service name will be used in naming your resources like log groups, ECS services, etc. +name: flowise +type: Load Balanced Web Service + +# Distribute traffic to your service. +http: + # Requests to this path will be forwarded to your service. + # To match all requests you can use the "/" path. + path: '/' + # You can specify a custom health check path. The default is "/". + # healthcheck: '/' + alias: ${COPILOT_ENVIRONMENT_NAME}.flowise.theanswer.ai + +# Configuration for your containers and service. +image: + # Docker build arguments. For additional overrides: https://aws.github.io/copilot-cli/docs/manifest/lb-web-service/#image-build + build: Dockerfile + cache_from: + - theanswerai/flowise:latest + + # Port exposed through your container to route traffic to it. + port: 4000 + +cpu: 256 # Number of CPU units for the task. +memory: 512 # Amount of memory i n MiB used by the task. +count: 1 # Number of tasks that should be running in your service. +exec: true # Enable running commands in your container. +network: + connect: true # Enable Service Connect for intra-environment traffic between services. + vpc: + security_groups: + - from_cfn: ${COPILOT_APPLICATION_NAME}-${COPILOT_ENVIRONMENT_NAME}-flowiseclusterSecurityGroup + +storage: + volumes: + myManagedEFSVolume: + efs: true + path: /var/efs + read_only: false +# readonly_fs: true # Limit to read-only access to mounted root filesystems. + +# Optional fields for more advanced use-cases. +# +#variables: # Pass environment variables as key value pairs. +# LOG_LEVEL: info + +#secrets: # Pass secrets from AWS Systems Manager (SSM) Parameter Store. +# GITHUB_TOKEN: GITHUB_TOKEN # The key is the name of the environment variable, the value is the name of the SSM parameter. + +secrets: + DB_SECRET: + from_cfn: ${COPILOT_APPLICATION_NAME}-${COPILOT_ENVIRONMENT_NAME}-flowiseclusterAuroraSecret + +# You can override any of the values defined above by environment. +environments: + staging: + env_file: /${COPILOT_ENVIRONMENT_NAME}.env +# count: 2 # Number of tasks to run for the "test" environment. +# deployment: # The deployment strategy for the "test" environment. +# rolling: 'recreate' # Stops existing tasks before new ones are started for faster deployments. diff --git a/packages/server/src/commands/start.ts b/packages/server/src/commands/start.ts index f8877e26425..62aea427162 100644 --- a/packages/server/src/commands/start.ts +++ b/packages/server/src/commands/start.ts @@ -122,6 +122,15 @@ export default class Start extends Command { if (flags.DATABASE_SSL) process.env.DATABASE_SSL = flags.DATABASE_SSL if (flags.DATABASE_SSL_KEY_BASE64) process.env.DATABASE_SSL_KEY_BASE64 = flags.DATABASE_SSL_KEY_BASE64 + if (process.env.MYCLUSTER_SECRET) { + const { host, port, dbname, username, password } = JSON.parse(process.env.MYCLUSTER_SECRET) + process.env.DATABASE_HOST = host + process.env.DATABASE_PORT = port + process.env.DATABASE_NAME = dbname + process.env.DATABASE_USER = username + process.env.DATABASE_PASSWORD = password + } + // Langsmith tracing if (flags.LANGCHAIN_TRACING_V2) process.env.LANGCHAIN_TRACING_V2 = flags.LANGCHAIN_TRACING_V2 if (flags.LANGCHAIN_ENDPOINT) process.env.LANGCHAIN_ENDPOINT = flags.LANGCHAIN_ENDPOINT From 52a0a1814da8077e3f55199a759c91beba629f3b Mon Sep 17 00:00:00 2001 From: Max Techera Date: Fri, 12 Apr 2024 14:08:40 -0300 Subject: [PATCH 2/5] Add deployment documentation --- DEPLOYMENT.md | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 DEPLOYMENT.md diff --git a/DEPLOYMENT.md b/DEPLOYMENT.md new file mode 100644 index 00000000000..b0673ebb1b9 --- /dev/null +++ b/DEPLOYMENT.md @@ -0,0 +1,60 @@ +## Deploying an Environment for an Existing Application with AWS Copilot CLI + +Before you start, make sure you have the following prerequisites: + +- AWS account +- AWS CLI installed and configured with the new customer's AWS account +- Docker installed +- Copilot CLI installed +- Environment variables for the application + +Here are the environment variables required: + +| Variable | Description | +| -------------------------------- | ----------------------------------------------------- | +| PORT | The port the application runs on | +| APIKEY_PATH | The path to the API key | +| SECRETKEY_PATH | The path to the secret key | +| LOG_PATH | The path to the log files | +| DISABLE_FLOWISE_TELEMETRY | Flag to disable telemetry | +| IFRAME_ORIGINS | Origins allowed to embed the application in an iframe | +| MY_APP_VITE_AUTH_DOMAIN | Auth0 domain | +| MY_APP_VITE_AUTH_CLIENT_ID | Auth0 client ID | +| MY_APP_VITE_AUTH_AUDIENCE | Auth0 audience | +| MY_APP_VITE_AUTH_ORGANIZATION_ID | Auth0 organization ID | +| DOMAIN | The domain of the application | +| ANSWERAI_DOMAIN | The domain of AnswerAI | +| AUTH0_JWKS_URI | URI of Auth0 JWKS | +| AUTH0_ISSUER_BASE_URL | Base URL of Auth0 issuer | +| AUTH0_BASE_URL | Base URL of Auth0 | +| AUTH0_CLIENT_ID | Client ID of Auth0 | +| AUTH0_CLIENT_SECRET | Client secret of Auth0 | +| AUTH0_AUDIENCE | Audience of Auth0 | +| AUTH0_SCOPE | Scope of Auth0 | +| AUTH0_TOKEN_SIGN_ALG | Token signing algorithm of Auth0 | +| AUTH0_ORGANIZATION_ID | Organization ID of Auth0 | + +Here are the steps to deploy an environment for an existing application: + +1. **Clone the application repository**. This will get you the application code and configuration. + +```bash +git clone https://github.com/answers-AI/Flowise +cd Flowise +``` + +2. **Create a new environment**. This will create a new environment, which is a standalone instance of your application. + +```bash +copilot env init --name --profile default + +3. **Deploy the service**. This will build your Docker image, push it to Amazon ECR, and deploy it to Amazon ECS. +``` + +````bash +copilot svc deploy --env + +4. **Access the service**. You can see the URL of your service by running: +```bash +copilot svc show +```` From 296e6c395be3b8a2aa4307860a80192fd8e29c82 Mon Sep 17 00:00:00 2001 From: Max Techera Date: Fri, 12 Apr 2024 14:33:24 -0300 Subject: [PATCH 3/5] Update database secret --- copilot/flowise/manifest.yml | 7 ++----- packages/server/src/commands/start.ts | 6 ++++-- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/copilot/flowise/manifest.yml b/copilot/flowise/manifest.yml index 54e13e4badd..2de1809b642 100644 --- a/copilot/flowise/manifest.yml +++ b/copilot/flowise/manifest.yml @@ -48,11 +48,8 @@ storage: #variables: # Pass environment variables as key value pairs. # LOG_LEVEL: info -#secrets: # Pass secrets from AWS Systems Manager (SSM) Parameter Store. -# GITHUB_TOKEN: GITHUB_TOKEN # The key is the name of the environment variable, the value is the name of the SSM parameter. - -secrets: - DB_SECRET: +secrets: # Pass secrets from AWS Systems Manager (SSM) Parameter Store. + DATABASE_SECRET: from_cfn: ${COPILOT_APPLICATION_NAME}-${COPILOT_ENVIRONMENT_NAME}-flowiseclusterAuroraSecret # You can override any of the values defined above by environment. diff --git a/packages/server/src/commands/start.ts b/packages/server/src/commands/start.ts index 62aea427162..5f756663677 100644 --- a/packages/server/src/commands/start.ts +++ b/packages/server/src/commands/start.ts @@ -122,8 +122,10 @@ export default class Start extends Command { if (flags.DATABASE_SSL) process.env.DATABASE_SSL = flags.DATABASE_SSL if (flags.DATABASE_SSL_KEY_BASE64) process.env.DATABASE_SSL_KEY_BASE64 = flags.DATABASE_SSL_KEY_BASE64 - if (process.env.MYCLUSTER_SECRET) { - const { host, port, dbname, username, password } = JSON.parse(process.env.MYCLUSTER_SECRET) + // Copilot database secret + + if (process.env.DATABASE_SECRET) { + const { host, port, dbname, username, password } = JSON.parse(process.env.DATABASE_SECRET) process.env.DATABASE_HOST = host process.env.DATABASE_PORT = port process.env.DATABASE_NAME = dbname From 2aad933576dbc6c180065fff02c6f95ac4c2fe9c Mon Sep 17 00:00:00 2001 From: Max Techera Date: Fri, 12 Apr 2024 14:33:52 -0300 Subject: [PATCH 4/5] Ignore *.env --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 01841b5aead..c294e0384ae 100644 --- a/.gitignore +++ b/.gitignore @@ -24,6 +24,7 @@ .DS_Store ## env +*.env .env.local .env.development.local .env.test.local From 58a0b0d5dfd857745999477019d355c12bc8ec67 Mon Sep 17 00:00:00 2001 From: Max Techera Date: Fri, 12 Apr 2024 14:43:58 -0300 Subject: [PATCH 5/5] Fix missing errorg --- packages/ui/src/App.jsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/ui/src/App.jsx b/packages/ui/src/App.jsx index e63c926fb76..bf742b49c2c 100644 --- a/packages/ui/src/App.jsx +++ b/packages/ui/src/App.jsx @@ -19,7 +19,7 @@ import useNotifyParentOfNavigation from './utils/useNotifyParentOfNavigation' const App = () => { const customization = useSelector((state) => state.customization) - const { getAccessTokenSilently } = useAuth0() + const { getAccessTokenSilently, error } = useAuth0() useNotifyParentOfNavigation() React.useEffect(() => { ;(async () => {