|
1 | 1 |
|
2 | 2 | # Hosting Langfuse on Amazon ECS with Fargate using CDK Python |
3 | 3 |
|
4 | | -> :information_source: This guide covers Langfuse v2. For Langfuse v3, see the [v3 documentation](https://langfuse.com/self-hosting). |
5 | | -
|
6 | | - |
7 | | - |
8 | 4 | This repository contains the AWS CDK Python code for deploying the [Langfuse](https://langfuse.com/) application using Amazon Elastic Container Registry (ECR) and Amazon Elastic Container Service (ECS). |
9 | 5 |
|
10 | 6 | Langfuse is an open-source LLM engineering platform that helps teams collaboratively debug, analyze, and iterate on their LLM applications. |
11 | 7 |
|
12 | | -The `cdk.json` file tells the CDK Toolkit how to execute your app. |
13 | | - |
14 | | -This project is set up like a standard Python project. The initialization |
15 | | -process also creates a virtualenv within this project, stored under the `.venv` |
16 | | -directory. To create the virtualenv it assumes that there is a `python3` |
17 | | -(or `python` for Windows) executable in your path with access to the `venv` |
18 | | -package. If for any reason the automatic creation of the virtualenv fails, |
19 | | -you can create the virtualenv manually. |
20 | | - |
21 | | -To manually create a virtualenv on MacOS and Linux: |
22 | | - |
23 | | -``` |
24 | | -$ python3 -m venv .venv |
25 | | -``` |
26 | | - |
27 | | -After the init process completes and the virtualenv is created, you can use the following |
28 | | -step to activate your virtualenv. |
29 | | - |
30 | | -``` |
31 | | -$ source .venv/bin/activate |
32 | | -``` |
33 | | - |
34 | | -If you are a Windows platform, you would activate the virtualenv like this: |
35 | | - |
36 | | -``` |
37 | | -% .venv\Scripts\activate.bat |
38 | | -``` |
39 | | - |
40 | | -Once the virtualenv is activated, you can install the required dependencies. |
41 | | - |
42 | | -``` |
43 | | -(.venv) $ pip install -r requirements.txt |
44 | | -``` |
45 | | - |
46 | | -> To add additional dependencies, for example other CDK libraries, just add |
47 | | -them to your `setup.py` file and rerun the `pip install -r requirements.txt` |
48 | | -command. |
49 | | - |
50 | | -### Set up `cdk.context.json` |
51 | | - |
52 | | -Then, we need to set approperly the cdk context configuration file, `cdk.context.json`. |
53 | | - |
54 | | -For example, |
55 | | - |
56 | | -``` |
57 | | -{ |
58 | | - "db_cluster_name": "langfuse-db", |
59 | | - "ecs_cluster_name": "langfuse-cluster", |
60 | | - "ecs_service_name": "langfuse-alb-service", |
61 | | - "image_version": "2", |
62 | | - "langfuse_env": { |
63 | | - "NODE_ENV": "production", |
64 | | - "NEXTAUTH_SECRET": "mysecret", |
65 | | - "SALT": "mysalt", |
66 | | - "TELEMETRY_ENABLED": "true", |
67 | | - "NEXT_PUBLIC_SIGN_UP_DISABLED": "false", |
68 | | - "LANGFUSE_ENABLE_EXPERIMENTAL_FEATURES": "true" |
69 | | - } |
70 | | -} |
71 | | -``` |
72 | | - |
73 | | -:information_source: This guide covers Langfuse v2. `image_version` should be set to `2`. |
74 | | - |
75 | | -:information_source: `NEXTAUTH_SECRET` and `SALT` can be created using `openssl rand -base64 32` on `MacOS` or `Ubuntu`. |
76 | | -(For more information, see [**Langfuse Configuring Environment Variables**](https://langfuse.com/docs/deployment/self-host#configuring-environment-variables)) |
77 | | - |
78 | | -### Deploy |
79 | | - |
80 | | -At this point you can now synthesize the CloudFormation template for this code. |
81 | | - |
82 | | -``` |
83 | | -(.venv) $ export CDK_DEFAULT_ACCOUNT=$(aws sts get-caller-identity --query Account --output text) |
84 | | -(.venv) $ export CDK_DEFAULT_REGION=$(aws configure get region) |
85 | | -(.venv) $ cdk synth --all |
86 | | -``` |
87 | | - |
88 | | -Use `cdk deploy` command to create the stack shown above. |
89 | | - |
90 | | -``` |
91 | | -(.venv) $ cdk deploy --require-approval never --all |
92 | | -``` |
93 | | - |
94 | | -We can list all the CDK stacks by using the `cdk list` command prior to deployment. |
95 | | - |
96 | | -``` |
97 | | -(.venv) $ cdk list |
98 | | -LangFuseECRStack |
99 | | -LangFuseVpcStack |
100 | | -LangFuseALBStack |
101 | | -LangFuseAuroraPostgreSQLStack |
102 | | -LangFuseECSClusterStack |
103 | | -LangFuseECSTaskStack |
104 | | -LangFuseECSAlbFargateServiceStack |
105 | | -``` |
106 | | - |
107 | | -## Clean Up |
108 | | - |
109 | | -Delete the CloudFormation stack by running the below command. |
110 | | - |
111 | | -``` |
112 | | -(.venv) $ cdk destroy --force --all |
113 | | -``` |
114 | | - |
115 | | -## Useful commands |
116 | | - |
117 | | - * `cdk ls` list all stacks in the app |
118 | | - * `cdk synth` emits the synthesized CloudFormation template |
119 | | - * `cdk deploy` deploy this stack to your default AWS account/region |
120 | | - * `cdk diff` compare deployed stack with current state |
121 | | - * `cdk docs` open CDK documentation |
122 | | - |
123 | | -Enjoy! |
124 | | - |
125 | | -## Tracing for your LLM Application with Langfuse |
126 | | - |
127 | | -After deploying all CDK stacks, you can find the **Langfuse URL** using the following command: |
128 | | - |
129 | | -```bash |
130 | | -aws cloudformation describe-stacks --stack-name LangFuseECSAlbFargateServiceStack | \ |
131 | | - jq -r '.Stacks[0].Outputs | map(select(.OutputKey == "LoadBalancerDNS")) | .[0].OutputValue' |
132 | | -``` |
133 | | - |
134 | | -Next, open the **Langfuse URL** in your browser to create a new project for tracking your LLM application with Langfuse. |
135 | | - |
136 | | -### Create a New Project in Langfuse |
137 | | - |
138 | | -1. Create a Langfuse Account |
139 | | - |
140 | | -  |
141 | | - |
142 | | -  |
143 | | -2. Create a New Project |
144 | | -  |
145 | | -3. Create New API Credentials in the Project Settings |
146 | | -  |
147 | | - |
148 | | -### Log Your First LLM Call to Langfuse |
149 | | - |
150 | | -Open the `tracing_for_langchain_bedrock` notebook in the `examples` folder and run it. (See [here](./examples/tracing_for_langchain_bedrock.ipynb) for more information) |
151 | | - |
152 | | -You will the see the list of traces as follows: |
153 | | - |
154 | | - |
155 | | -You will also see the details of the selected trace as follows: |
156 | | - |
157 | | - |
| 8 | +| Project | Architecture | |
| 9 | +|---------|--------------| |
| 10 | +| [langfuse-v2](./langfuse-v2/) |  | |
| 11 | +| [langfuse-v3](./langfuse-v3/) |  | |
158 | 12 |
|
159 | 13 | ## References |
160 | 14 |
|
161 | | - * [(AWS Korea Tech Blog) Hosting Langfuse on Amazon ECS with Fargate using AWS CDK Python (2024-08-09)](https://aws.amazon.com/ko/blogs/tech/hosting-langfuse-with-aws-cdk-python-using-amazon-ecs-and-aws-fargate/) |
162 | | - * [Langfuse Official Documents](https://langfuse.com/docs) - Langfuse is an open-source LLM engineering platform that helps teams collaboratively debug, analyze, and iterate on their LLM applications. |
163 | | - * [Langfuse Self-Hosting Guide (Docker)](https://langfuse.com/docs/deployment/self-host) |
164 | | - * [Langfuse Configuring Environment Variables](https://langfuse.com/docs/deployment/self-host#configuring-environment-variables) |
165 | | - * [Tracing for Langchain (Python & JS/TS)](https://langfuse.com/docs/integrations/langchain/tracing) |
166 | | - * [Langfuse Python SDK (Low-level)](https://langfuse.com/docs/sdk/python/low-level-sdk) |
167 | | - * [Langfuse Cookbook](https://github.com/langfuse/langfuse-docs/tree/main/cookbook) |
168 | | - * [(GitHub) Langfuse](https://github.com/langfuse/langfuse) - 🪢 Open source LLM engineering platform: Observability, metrics, evals, prompt management, playground, datasets. Integrates with LlamaIndex, Langchain, OpenAI SDK, LiteLLM, and more. |
169 | | - * [Langfuse ECR/ECS Deployment CDK TypeScript](https://github.com/AI4Organization/langfuse-ecr-ecs-deployment-cdk) - Deploy Langfuse to ECR and ECS with Fargate and AWS CDK TypeScript. |
170 | | - * [cdk-ecr-deployment](https://github.com/cdklabs/cdk-ecr-deployment) - A CDK construct to deploy docker image to Amazon ECR. |
| 15 | + * [(Official) Self-host Langfuse Guide](https://langfuse.com/self-hosting) |
| 16 | + * [(GitHub) langfuse](https://github.com/langfuse/langfuse/) |
| 17 | + * [AWS CDK Reference Documentation](https://docs.aws.amazon.com/cdk/api/v2/) |
171 | 18 |
|
172 | 19 | ## Security |
173 | 20 |
|
174 | | -See [CONTRIBUTING](CONTRIBUTING.md#security-issue-notifications) for more information. |
| 21 | +See [CONTRIBUTING](./CONTRIBUTING.md#security-issue-notifications) for more information. |
175 | 22 |
|
176 | 23 | ## License |
177 | 24 |
|
|
0 commit comments