Skip to content

armgit / burr

Public Imported from GitHub
0

Build applications that make decisions (chatbots, agents, simulations, etc...). Monitor, trace, persist, and execute on your own infrastructure.

Adds instructions on how to deploy Burr on AWS Lambda

Stefan Krawczyk5e0492e96696
diff --git a/docs/examples/deployment.rst b/docs/examples/deployment.rst
new file mode 100644
index 00000000..41ca182e
--- /dev/null
+++ b/docs/examples/deployment.rst
@@ -0,0 +1,14 @@
+=======================
+Deployment
+=======================
+
+This section describes a few ways to deploy your Burr application.
+
+----------------
+AWS Lambda
+----------------
+Burr can be deployed within a Lambda function. This is a good option if you want to run your
+application in response to events, or if you want to run your application in a serverless environment.
+
+See `this example <https://github.com/DAGWorks-Inc/burr/tree/main/examples/deployment/aws/lambda>`_ for step
+by step instructions on how to deploy a Burr application to AWS Lambda.
diff --git a/docs/examples/index.rst b/docs/examples/index.rst
index ee467151..ab34bf07 100644
--- a/docs/examples/index.rst
+++ b/docs/examples/index.rst
@@ -17,3 +17,4 @@ Examples of more complex/powerful use-cases of Burr. Download/copy these to adap
simulation
creating_tests
web-server
+ deployment
diff --git a/examples/deployment/aws/lambda/Dockerfile b/examples/deployment/aws/lambda/Dockerfile
new file mode 100644
index 00000000..4884a131
--- /dev/null
+++ b/examples/deployment/aws/lambda/Dockerfile
@@ -0,0 +1,11 @@
+FROM public.ecr.aws/lambda/python:3.11
+
+COPY requirements.txt ./
+RUN pip install -r requirements.txt
+
+# add env variables as needed.
+# ENV ..
+
+COPY app ./app
+
+CMD ["app.lambda_handler.lambda_handler"]
diff --git a/examples/deployment/aws/lambda/README.md b/examples/deployment/aws/lambda/README.md
new file mode 100644
index 00000000..9048f077
--- /dev/null
+++ b/examples/deployment/aws/lambda/README.md
@@ -0,0 +1,96 @@
+# Deploy Burr in AWS Lambda
+
+[AWS Lambda](https://aws.amazon.com/lambda/) - serverless computation service in AWS.
+
+Here we have an example how to deploy "hello-world" AWS Lambda with a simple Burr application.
+This example is based on the official instruction: https://docs.aws.amazon.com/lambda/latest/dg/python-image.html#python-image-instructions
+
+## Prerequisites
+
+- **AWS CLI Setup**: Make sure the AWS CLI is set up on your machine. If you haven't done this yet, no worries! You can follow the [Quick Start guide](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-quickstart.html) for easy setup instructions.
+
+## Step-by-Step Guide
+
+### 1. Build Docker image:
+
+- **Build Docker image for deploy in AWS ECR**
+
+ ```shell
+ docker build --platform linux/amd64 -t aws-lambda-burr .
+ ```
+
+- **Local tests:**
+
+ Run Docker container:
+
+ ```shell
+ docker run -p 9000:8080 aws-lambda-burr
+ ```
+
+ Send test request to check if Docker container executes it correctly:
+
+ ```shell
+ curl -XPOST "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{"body": {"number":3}}'
+ ```
+
+### 2. Create AWS ECR repository:
+
+Ensure the AWS account number (`111122223333`) is correctly replaced with yours:
+
+- **Authenticate Docker to Amazon ECR**:
+
+ Retrieve an authentication token to authenticate your Docker client to your Amazon Elastic Container Registry (ECR):
+
+ ```shell
+ aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin 111122223333.dkr.ecr.us-east-1.amazonaws.com
+ ```
+
+- **Create the ECR Repository**:
+
+ ```shell
+ aws ecr create-repository --repository-name aws-lambda-burr \
+ --region us-east-1 \
+ --image-scanning-configuration scanOnPush=true \
+ --image-tag-mutability MUTABLE
+ ```
+
+### 3. Deploy the Image to AWS ECR
+
+Ensure the AWS account number (`111122223333`) is correctly replaced with yours:
+
+```shell
+docker tag aws-lambda-burr 111122223333.dkr.ecr.us-east-1.amazonaws.com/aws-lambda-burr:latest
+docker push 111122223333.dkr.ecr.us-east-1.amazonaws.com/aws-lambda-burr:latest
+```
+
+### 4. Create a simple AWS Lambda role:
+
+Example of creating an AWS Role for Lambda execution:
+
+```shell
+aws iam create-role \
+ --role-name lambda-ex \
+ --assume-role-policy-document '{"Version": "2012-10-17","Statement": [{ "Effect": "Allow", "Principal": {"Service": "lambda.amazonaws.com"}, "Action": "sts:AssumeRole"}]}'
+```
+
+### 5. Create AWS Lambda
+
+Ensure the AWS account number (`111122223333`) is correctly replaced with yours:
+
+```shell
+aws lambda create-function \
+ --function-name aws-lambda-burr \
+ --package-type Image \
+ --code ImageUri=111122223333.dkr.ecr.us-east-1.amazonaws.com/aws-lambda-burr:latest \
+ --role arn:aws:iam::111122223333:role/lambda-ex
+```
+
+### 6. Test AWS Lambda
+
+```shell
+aws lambda invoke \
+ --function-name aws-lambda-burr \
+ --cli-binary-format raw-in-base64-out \
+ --payload '{"body": {"number": 5}}' \
+ response.json
+```
diff --git a/examples/deployment/aws/lambda/app/__init__.py b/examples/deployment/aws/lambda/app/__init__.py
new file mode 100644
index 00000000..e69de29b
diff --git a/examples/deployment/aws/lambda/app/counter_app.py b/examples/deployment/aws/lambda/app/counter_app.py
new file mode 100644
index 00000000..cee06a8b
--- /dev/null
+++ b/examples/deployment/aws/lambda/app/counter_app.py
@@ -0,0 +1,42 @@
+"""
+This is a very simple counting application.
+
+It's here to help you get the mechanics of deploying a Burr application to AWS Lambda.
+"""
+
+import time
+
+import burr.core
+from burr.core import Application, Result, State, default, expr
+from burr.core.action import action
+from burr.core.graph import GraphBuilder
+
+
+@action(reads=["counter"], writes=["counter"])
+def counter(state: State) -> State:
+ result = {"counter": state["counter"] + 1}
+ time.sleep(0.5) # sleep to simulate a longer running function
+ return state.update(**result)
+
+
+# our graph.
+graph = (
+ GraphBuilder()
+ .with_actions(counter=counter, result=Result("counter"))
+ .with_transitions(
+ ("counter", "counter", expr("counter < counter_limit")),
+ ("counter", "result", default),
+ )
+ .build()
+)
+
+
+def application(count_up_to: int = 10) -> Application:
+ """function to return a burr application"""
+ return (
+ burr.core.ApplicationBuilder()
+ .with_graph(graph)
+ .with_state(**{"counter": 0, "counter_limit": count_up_to})
+ .with_entrypoint("counter")
+ .build()
+ )
diff --git a/examples/deployment/aws/lambda/app/lambda_handler.py b/examples/deployment/aws/lambda/app/lambda_handler.py
new file mode 100644
index 00000000..d8fca4aa
--- /dev/null
+++ b/examples/deployment/aws/lambda/app/lambda_handler.py
@@ -0,0 +1,14 @@
+from . import counter_app
+
+
+def lambda_handler(event, context):
+ count_up_to = int(event["body"]["number"])
+
+ app = counter_app.application(count_up_to)
+ action, result, state = app.run(halt_after=["result"])
+
+ return {"statusCode": 200, "body": state.serialize()}
+
+
+if __name__ == "__main__":
+ print(lambda_handler({"body": {"number": 10}}, None))
diff --git a/examples/deployment/aws/lambda/requirements.txt b/examples/deployment/aws/lambda/requirements.txt
new file mode 100644
index 00000000..c10793cb
--- /dev/null
+++ b/examples/deployment/aws/lambda/requirements.txt
@@ -0,0 +1,2 @@
+burr
+# for tracking you'd add extra dependencies
diff --git a/examples/validate_examples.py b/examples/validate_examples.py
index f28c0562..1404545f 100644
--- a/examples/validate_examples.py
+++ b/examples/validate_examples.py
@@ -23,6 +23,7 @@ FILTERLIST = [
"web-server",
"integrations",
"templates",
+ "deployment",
]