Deploy Lambda on DigitalOcean

Bin Zhao
Bin Zhao
Jan 8 · 4 min read

In this article I’d like to show you how to deploy Lambda on DigtialOcean and persist function code in DigitalOcean Spaces leveraging Refunc and Kubernetes .

Refunc is an open-source kubernetes native serverless platform. Unlike other similar platforms, Refunc does not try to create something new, it lets developer keep using familiar tools to DevOps serverless functions (currently AWS Lambda API is supported) while avoiding the vendor lock-in.

It has been known for a while that DigitalOcean is friendly and simple for small businesses. Recently, it released managed Kubernetes to the public, so that we can use Kubernetes to build and deploy cloud-native applications and adopt microservices architecture on Droplets much easier. With Refunc, we are able to deploy Lambda functions natively alone with other workloads that maximizing the cluster’s usage.

Create Cluster

Firstly we need to create a kubernetes cluster and add some worker node to it. It is very straightforward to bring up a new cluster on DigitalOcean. All you have to do is just 4 clicks.

During the building process, we can follow the tutorial in waiting page to config our access to the cluster. The import thing is to download and save the “Config File”:

After one minute or less we should see our droplets are green:

Let’s use kubectl to check if our cluster is ready:

Deploy Refunc

Refunc can be easily deployed by using the following commands:

this will create a namespace of refunc-play and install necessary components stateless in it with RBAC enabled(by default).

Access Refunc

Refunc uses an AWS API compatible gateway to provide Lambda and S3 services, which makes it possible to use the AWS CLI to manage functions locally.

Before starting, we need to forward the gateway to your localhost, type the following command in a separate terminal:

kubectl port-forward deployment/aws-api-gw 9000:80 -n refunc-play

Launch a lambda function

we can follow any tutorial or official doc to build a function code, for simplicity we use a pre-packaged code building from lambda-python3.7-example.

Download the pre-built function for convenience:

cd /tmp
wget https://github.com/refunc/lambda-python3.7-example/releases/download/v0.0.1/lambda.zip

Create Function

aws --endpoint-url=http://127.0.0.1:9000 \
lambda create-function --function-name localtest \
--handler lambda_function.lambda_handler \
--zip-file fileb:///tmp/lambda.zip \
--runtime python3.7 \
--role arn:aws:iam::XXXXXXXXXXXXX:role/your_lambda_execution_role

Invoke Function

aws --endpoint-url=http://127.0.0.1:9000 \
lambda invoke --function-name localtest /tmp/output.json && cat /tmp/output.json

Persist Function Code

As mentioned before, refunc play generates a stateless deployment of Refunc, when updating the functions code will be lost.

Actually Refunc depends on Minio to provides a S3 Compatible service, DigitalOcean Spaces has a S3 API as well, so we can simply patch the config to persist function code.

In the Spaces page we create a new space with a unique name, the space in DO is identical to the Bucket in other clouds.

After space is created, we can find the endpoint in space’s setting page

Generate access_key and secrete_key from API page

Create a secret resource using kubectl , replacing key and secret with value in API page

kubectl create secret generic space -n refunc-play \
--from-literal=access-key=<key> \
--from-literal=secret-key=<secret>

Regenerate k8s’ templates with space name and folder prefix and space-endpoint:

Repeat the creating function process mentioned above and check through the Space Explorer Page, function code should placed at <folder-prefix>/<namespace>/<function-name>/blobs

Conclusion

Startups are always battles regarding time and money. In the beginning, we want to have low-cost cloud resources as well as rapid development. DigitalOcean’s Kubernetes is a good point to start to leverage advanced cloud-native technologies. With Refunc, developers can manage Lambda functions on Kubernetes. As businesses keep growing, IT teams can transfer or scale Lambda applications to more sophisticated cloud infrastructures seamlessly.

Refunc

A Kubernetes native serverless platform

Bin Zhao

Written by

Bin Zhao

Refunc

Refunc

A Kubernetes native serverless platform

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade