This is a complementary demo application to go with the Apache Flink community blog post, Stateful Functions Internals: Behind the scenes of Stateful Serverless, which walks you through the details of Stateful Functions' runtime.
The demo is a simple shopping cart application, whose architecture consists of the following parts:
- Functions written using Stateful Functions' Python SDK, serviced via AWS Lambda and AWS API Gateway as a serverless deployment.
- A StateFun cluster running on Kubernetes (AWS EKS), playing the role of an event-driven database that manages state and message routing for the serverless functions.
- AWS Kinesis streams serving as the ingress and egress of application events.
- An AWS S3 bucket to persist periodic checkpoints of the function states for durability and fault-tolerance.
Please refer to the blog post for further details.
app/
: Contains the function code that defines the business logic, written as AWS Lambda functions.protobuf/
: Contains Protobuf definitions of the message and state types that the functions use.Dockerfile
: Dockerfile for building the image that will be used to deploy the StateFun cluster.k8s/
: Contains Helm templates to deploy the StateFun cluster on Kubernetes.build-*.sh
: Tool scripts for preparing deployment resources, such as AWS Lambda packages and Kubernetes deployment templates.
- Have Helm, Docker, and
kubectl
installed. - A running Kubernetes cluster with access to AWS Kinesis and AWS S3. This demo assumes that you are using AWS EKS, which would contain the necessary credentials in the cluster node environments.
- Created AWS Kinesis streams
cart_events
andcart_results
. - A S3 bucket for persisting periodic checkpoints. The tutorial below assumes you've created the bucket at
s3:///checkpoints/
.
$ ./build_lambda_packages.sh
This creates a package at aws_lambda_package/shopping_cart_lambda_function.zip
.
Once done, you need to:
- Upload the Lambda function. Please see the AWS Lambda docs on how to do that.
- Service the function by configuring an API Gateway HTTP endpoint as the Lambda trigger.
After completing this step, you should now have an API endpoint URL (e.g. https://abcdxyz.execute-api.us-west-1.amazonaws.com/default/shopping-cart
) for invoking the functions.
$ ./build_k8s_resources.sh -t <image-tag> -e <lambda-endpoint-url> -c <s3-checkpoint-dir>
For example, assuming the resources we've prepared so far:
$ ./build_k8s_resources.sh \
-t tzulitai/statefun-aws-demo \
-e https://abcdxyz.execute-api.us-west-1.amazonaws.com/default/shopping-cart \
-s s3:///checkpoints/
This builds the Docker image for the StateFun cluster, as well as a statefun-k8s.yaml
for deploying the cluster.
Once done, you need to:
- Upload the Docker image to the Dockerhub public registry.
kubectl create -f statefun-k8s.yaml
to deploy the cluster.