Skip to content

feat(cloud): add cloud-run guide #356

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jun 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 34 additions & 30 deletions docs/cloud/connect/serverless/aws-lambda.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,24 @@ import PageTitle from '@site/src/components/PageTitle';

<PageTitle title="Connecting from AWS Lambda | Dragonfly Cloud" />

AWS Lambda is a serverless compute service provided by Amazon Web Services (AWS). It allows you to run code without provisioning or managing servers. You simply upload your code, and Lambda automatically handles the execution, scaling, and availability. It supports various programming languages and integrates seamlessly with other AWS services, making it ideal for building scalable, event-driven applications.
This guide explains how to create an AWS Lambda function that connects to a Dragonfly Cloud data store.

This guide explains how to create an AWS Lambda function that connects to a Dragonfly Cloud instance.
[AWS Lambda](https://aws.amazon.com/lambda/) is a serverless compute service provided by Amazon Web Services (AWS).
It allows you to run code without provisioning or managing servers.
You simply upload your code, and Lambda automatically handles the execution, scaling, and availability.
It supports various programming languages and integrates seamlessly with other AWS services,
making it ideal for building scalable, event-driven applications.

**Note**: You can skip to the [Connecting to a Private Dragonfly Data Store](#connecting-to-a-private-dragonfly-data-store)
section if you already have the Lambda function set up and just want to learn how to work with private Dragonfly Cloud data stores.

---

## Prerequisites

1. **Dragonfly Cloud Instance**: Ensure you have a running Dragonfly Cloud instance and its connection URI.
1. **Dragonfly Cloud Data Store**: Ensure you have a running [Dragonfly Cloud](https://dragonflydb.cloud/) data store and its connection URI.
2. **AWS Account**: Access to AWS Lambda and IAM services.
3. **Node Runtime**: The Lambda function will be written in NodeJS.
3. **Node Runtime**: In this guide, the Lambda function will be written in NodeJS.
4. **Redis Client Library**: Use the `redis` package to interact with Dragonfly.

---
Expand All @@ -33,21 +40,17 @@ This guide explains how to create an AWS Lambda function that connects to a Drag
4. Provide a name for your function (e.g., `DragonflyConnector`).
5. Click **Create function**.

---

### 2. Add the Dragonfly URI as an Environment Variable
### 2. Set Environment Variable(s)

1. In the Lambda function configuration, go to the **Configuration** tab.
1. Within the Lambda function, go to the **Configuration** tab.
2. Select **Environment variables**.
3. Add a new variable:
- **Key**: `DRAGONFLY_CONNECTION_URI`
- **Value**: Your Dragonfly Cloud connection URI (e.g., `rediss://<username>:<password>@<host>:<port>`).

---

### 3. Write the Lambda Function Code

Create a module Javascript file (e.g., `index.mjs`) with the following code:
Create a module JavaScript file (e.g., `index.mjs`) with the following code:

```js
import {createClient} from 'redis';
Expand All @@ -65,14 +68,14 @@ if (!redis.isReady) {
console.log("ready")
export const handler = async (event) => {
try {
// Test connection with basic operations
// Test connection with some basic operations.
await redis.set('lambda_test', JSON.stringify({
timestamp: new Date().toISOString(),
source: 'AWS Lambda'
}));

const result = await redis.get('lambda_test');

return {
statusCode: 200,
body: JSON.stringify({
Expand All @@ -93,8 +96,6 @@ export const handler = async (event) => {
};
```

---

### 4. Build and Deploy the Lambda Function

1. Your directory should look like this:
Expand All @@ -106,7 +107,7 @@ export const handler = async (event) => {
package-lock.json
```

2. **Package**: Aws Lambda takes zip file to load the code:
2. **Package**: AWS Lambda takes a zip file to load the code:

```sh
zip -r dragonfly-lambda.zip .
Expand All @@ -117,8 +118,6 @@ export const handler = async (event) => {
- Go to the [AWS Lambda Console](https://console.aws.amazon.com/lambda/).
- In the **Code** section, upload the `dragonfly-lambda.zip` file.

---

### 5. Test the Lambda Function

1. Go to the **Test** tab in the Lambda Console.
Expand All @@ -128,24 +127,25 @@ export const handler = async (event) => {

---

### 6. Connecting to a Private Dragonfly Data Store
## Connecting to a Private Dragonfly Data Store

Private data stores are hosted within a Virtual Private Cloud (VPC), which provides an isolated network environment. To enable your AWS Lambda function to securely connect to a private Dragonfly data store, follow these beginner-friendly steps:
Private data stores are hosted within a Virtual Private Cloud (VPC), which provides an isolated network environment.
To enable your AWS Lambda function to securely connect to a private Dragonfly Cloud data store, follow these beginner-friendly steps:

#### 1. Set Up VPC Peering
### 1. Set Up VPC Peering

1. Create a VPC in your AWS account within the same region as your data store.
2. Establish a peering connection between your VPC and the data store's VPC. This allows the two networks to communicate. For detailed guidance, refer to the [VPC Peering Connections documentation](../../connections.md).

#### 2. Adjust Security Group Rules
### 2. Adjust Security Group Rules

1. Open the [VPC Console](https://console.aws.amazon.com/vpc/) and locate the security group associated with your vpc.
2. Add an inbound rule to allow traffic from your vpc:
1. Open the [VPC Console](https://console.aws.amazon.com/vpc/) and locate the security group associated with your VPC.
2. Add an inbound rule to allow traffic from your VPC:
- **Type**: Custom TCP Rule
- **Port Range**: `6379` (Dragonfly port).
- **Source**: CIDR of the private network.

#### 3. Grant Lambda the Necessary Permissions
### 3. Grant Lambda Necessary Permissions

To allow Lambda to interact with your VPC, you need to update its execution role:

Expand All @@ -154,7 +154,7 @@ To allow Lambda to interact with your VPC, you need to update its execution role
3. Under **Permissions**, click the execution role name.
4. Add the **AmazonEC2FullAccess** permission to the role. This ensures Lambda can connect to your VPC.

#### 4. Configure Lambda to Use the VPC
### 4. Configure Lambda to Use the VPC

1. In the [AWS Lambda Console](https://console.aws.amazon.com/lambda/), select your function.
2. Go to the **Configuration** tab and choose **VPC**.
Expand All @@ -163,15 +163,19 @@ To allow Lambda to interact with your VPC, you need to update its execution role
- **Subnets**: Choose subnets with access to the data store.
- **Security Groups**: Select the security group that allows traffic to the data store.

#### 5. Test the Connection
### 5. Test the Connection

1. Deploy your Lambda function as described earlier.
2. Update the `DRAGONFLY_CONNECTION_URI` environment variable with the private data store's connection URL.
3. Run a test event in the Lambda Console.
4. Check the logs or query the data store to confirm the connection is successful.

By following these steps, you can securely connect your Lambda function to a private Dragonfly data store, ensuring your application remains both scalable and secure.
By following these steps, you can securely connect your Lambda function to a private Dragonfly Cloud data store, ensuring your application remains both scalable and secure.

---

## Conclusion

You have successfully created an AWS Lambda function that connects to Dragonfly Cloud, sets a test key-value pair, and verifies the connection. You can now extend this function to perform more complex operations with Dragonfly.
You have successfully created an AWS Lambda function that connects to Dragonfly Cloud,
sets a test key-value pair, and verifies the connection.
You can now extend this function to perform more complex operations with Dragonfly.
199 changes: 199 additions & 0 deletions docs/cloud/connect/serverless/gcp-cloud-run.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
---
sidebar_position: 2
---

import PageTitle from '@site/src/components/PageTitle';

# GCP Cloud Run

<PageTitle title="Connecting from GCP Cloud Run | Dragonfly Cloud" />

This guide explains how to create a GCP Cloud Run function that connects to a Dragonfly Cloud data store.

[GCP Cloud Run](https://cloud.google.com/run) is a serverless compute service provided by Google Cloud Platform (GCP).
It allows you to run code without provisioning or managing servers.
It supports various programming languages and integrates seamlessly with other GCP services,
making it ideal for building scalable, event-driven applications.

Within the Cloud Run service,
a [Cloud Run function](https://cloud.google.com/blog/products/serverless/google-cloud-functions-is-now-cloud-run-functions)
is a deployment option that allows you to deploy inline code scripts or functions directly instead of deploying container images or code repositories.
However, the process to connect to a Dragonfly Cloud data store is generally applicable to both Cloud Run services and functions.

**Note**: You can skip to the [Connecting to a Private Dragonfly Data Store](#connecting-to-a-private-dragonfly-data-store)
section if you already have the Cloud Run function set up and just want to learn how to work with private Dragonfly Cloud data stores.

---

## Prerequisites

1. **Dragonfly Cloud Data Store**: Ensure you have a running [Dragonfly Cloud](https://dragonflydb.cloud/) data store and its connection URI.
2. **GCP Console**: Access to Cloud Run and IAM services.
3. **Go Toolchain**: In this guide, the Cloud Run service will be written in Go.
4. **Redis Client Library**: Use the `go-redis` package to interact with Dragonfly.

---

## Cloud Run Function Example Code

For the purposes of this guide, a Cloud Run function implementation is provided for simplicity.
Alternatively, a Cloud Run service deployment may be used instead.
The connection process to a Dragonfly Cloud data store remains the same in either case.
The following sample code will be deployed:

```go
package helloworld

import (
"encoding/json"
"fmt"
"html"
"net/http"
"os"
"context"

"github.com/redis/go-redis/v9"
"github.com/GoogleCloudPlatform/functions-framework-go/functions"
)

func init() {
functions.HTTP("HelloHTTP", helloHTTP)
}

// An HTTP Cloud Function handler that responds to HTTP requests.
func helloHTTP(w http.ResponseWriter, r *http.Request) {
var req struct {
Name string `json:"name"`
}
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
fmt.Fprint(w, "Hello, World!")
return
}

setDragonflyValue(req.Name)
if req.Name == "" {
fmt.Fprint(w, "Hello, World!")
return
}
fmt.Fprintf(w, "Hello, %s!", html.EscapeString(req.Name))
}

func setDragonflyValue(name string) {
ctx := context.Background()
connectionURI := os.Getenv("DRAGONFLY_CONNECTION_URI")

// Dragonfly is compatible with the Redis protocol, we can use existing Redis libraries.
opt, err := redis.ParseURL(connectionURI)
if err != nil {
fmt.Printf("Error parsing Dragonfly connection URI: %v\n", err)
return
}
client := redis.NewClient(opt)

// Ping the server to test the connection.
pong, err := client.Ping(ctx).Result()
if err != nil {
fmt.Printf("Error connecting to Dragonfly: %v\n", err)
return
}
fmt.Printf("Connected to Dragonfly: %s\n", pong)

// Perform some test operations.
err = client.Set(ctx, "test_key", name, 0).Err()
if err != nil {
fmt.Printf("Error setting key: %v\n", err)
return
}

value, err := client.Get(ctx, "test_key").Result()
if err != nil {
fmt.Printf("Error getting key: %v\n", err)
return
}
fmt.Printf("Retrieved value: %s\n", value)

// Close the connection.
err = client.Close()
if err != nil {
fmt.Printf("Error closing connection: %v\n", err)
}
}
```

The example code requires an environment variable named `DRAGONFLY_CONNECTION_URI`.

---

## Connecting to a Public Dragonfly Data Store

### 1. Create a New Cloud Run Function

1. Go to the [Cloud Run](https://console.cloud.google.com/run) console.
2. Click **Write a function**.
3. Provide a name for your function/service (e.g., `HelloDragonfly`).
4. Choose a Go runtime version that supports your function code.
5. Expand the **Container(s)** section and add a new environment variable under the `Variables & Secrets` tab:
- **Key**: `DRAGONFLY_CONNECTION_URI`
- **Value**: Your Dragonfly Cloud connection URI (e.g., `rediss://<username>:<password>@<host>:<port>`).
6. Click **Create** to create the function.
7. Add your code or the code provided above to the inline editor and wait for the function to be deployed.

### 2. Test the Cloud Run Function

1. Click the **Test** button in the Cloud Run console.
2. Create a new test event (you can use the default template).
3. Run the test (you can use Cloud Shell for simplicity).
4. Check the logs in **Logs** to verify the connection and the key-value pair operation.

---

## Connecting to a Private Dragonfly Data Store

Private data stores are hosted within a Virtual Private Cloud (VPC), which provides
an isolated network environment. To enable your Cloud Run function to securely
connect to a private Dragonfly Cloud data store, follow these beginner-friendly steps:

### 1. Set Up VPC Peering

1. Create a VPC in your GCP account within the same region as your data store.
2. Establish a peering connection between your VPC and the data store's VPC.
This allows the two networks to communicate. For detailed guidance, refer to the [VPC Peering Connections documentation](../../connections.md).

### 2. Adjust Firewall Rules

1. Open the [VPC Network Console](https://console.cloud.google.com/networking/networks/list).
2. Select your network and open the **Firewall** settings.
3. Add an ingress rule to allow traffic from your data store VPC. Put the data store VPC CIDR range in the source IPV4 range field, and allow all ports.

### 3. Edit Cloud Run Settings

As the data store is private, you need to configure Cloud Run's network setting
to the VPC network you just created.

1. Go to the [Cloud Run](https://console.cloud.google.com/run). Select your service.
2. Navigate to the **Networking** tab.
3. Select **Internal Ingress**. Save changes.
4. Once deployed, click **Edit & deploy new version**.
5. Make sure the `DRAGONFLY_CONNECTION_URI` environment variable is set to the data store's private connection URI.
6. Go to **Networking**. Select **Connect to a VPC for outbound traffic**. Choose your VPC.
7. Deploy the changes.

### 4. Test the Connection

1. Click **Test**. Copy the test command.
2. You need to create a VM instance inside your VPC to run the test. Go to the **VM instances** tab and
create a VM instance. Make sure you've configured the network interface to use your VPC.
3. Update your firewall rules so that you can connect to the instance via SSH.
4. SSH to your machine and run the test command.

You'll see in **Logs** that Dragonfly has stored the value.
By following these steps, you can securely connect your Cloud Run service to a private Dragonfly Cloud data store,
ensuring your application remains both scalable and secure.

---

## Conclusion

You have successfully created a GCP Cloud Run function that connects to Dragonfly Cloud,
sets a test key-value pair, and verifies the connection.
You can now extend this function to perform more complex operations with Dragonfly.
1 change: 1 addition & 0 deletions docs/cloud/connect/serverless/serverless.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ import PageTitle from '@site/src/components/PageTitle';
Dragonfly Cloud offers seamless integration with serverless services, enabling you to leverage its high-performance in-memory data store for your applications. This guide will walk you through the steps to connect your serverless environment to Dragonfly Cloud, ensuring optimal performance and compatibility. Whether you're using AWS Lambda, Google Cloud Functions, or Azure Functions, Dragonfly Cloud provides the tools and support you need to get started quickly and efficiently.

- [AWS Lambda](aws-lambda.md)
- [GCP Cloud Run](gcp-cloud-run.md)