From 19096e45b791e8ee8f99b9ea23e035a283555ca7 Mon Sep 17 00:00:00 2001 From: Abhradeep Chakraborty Date: Tue, 8 Apr 2025 11:36:40 +0530 Subject: [PATCH 1/3] feat(cloud): add cloud-run guide Signed-off-by: Abhradeep Chakraborty fix Signed-off-by: Abhradeep Chakraborty --- docs/cloud/connect/serverless/aws-lambda.md | 12 +- docs/cloud/connect/serverless/cloud-run.md | 178 ++++++++++++++++++++ 2 files changed, 184 insertions(+), 6 deletions(-) create mode 100644 docs/cloud/connect/serverless/cloud-run.md diff --git a/docs/cloud/connect/serverless/aws-lambda.md b/docs/cloud/connect/serverless/aws-lambda.md index d0f4448..286ba00 100644 --- a/docs/cloud/connect/serverless/aws-lambda.md +++ b/docs/cloud/connect/serverless/aws-lambda.md @@ -124,16 +124,16 @@ 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: -#### 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: @@ -141,7 +141,7 @@ Private data stores are hosted within a Virtual Private Cloud (VPC), which provi - **Port Range**: `6379` (Dragonfly port). - **Source**: CIDR of the private network. -#### 3. Grant Lambda the Necessary Permissions +### 3. Grant Lambda the Necessary Permissions To allow Lambda to interact with your VPC, you need to update its execution role: @@ -150,7 +150,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**. @@ -159,7 +159,7 @@ 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. diff --git a/docs/cloud/connect/serverless/cloud-run.md b/docs/cloud/connect/serverless/cloud-run.md new file mode 100644 index 0000000..ab88ee0 --- /dev/null +++ b/docs/cloud/connect/serverless/cloud-run.md @@ -0,0 +1,178 @@ +--- +sidebar_position: 2 +--- + +# Connect from Cloud Run services + +Cloud Run is a serverless compute service provided by 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. + +This guide explains how to connect a Cloud run service to a Dragonfly Cloud instance. + +--- + +## Prerequisites + +1. **Dragonfly Cloud Instance**: Ensure you have a running Dragonfly Cloud instance and its connection URI. +2. **GCP Console**: Access to Cloud Run and IAM services. +3. **Go Runtime**: The Cloud Run service will be written in NodeJS. +4. **Redis Client Library**: Use the `go-redis/v9` package to interact with Dragonfly. + +--- + +## Cloud Run function code + +I am writing a function for this guide for simplicity. You can deploy a service +instead. The process to connect to a dragonfly data store is same. I will deploy +the below sample code - + +```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" + +) + +var addr string +var pass string + +func init() { + functions.HTTP("HelloHTTP", helloHTTP) +} + +// helloHTTP is an HTTP Cloud Function with a request parameter. +func helloHTTP(w http.ResponseWriter, r *http.Request) { + var d struct { + Name string `json:"name"` + } + if err := json.NewDecoder(r.Body).Decode(&d); err != nil { + fmt.Fprint(w, "Hello, World!") + return + } + + setDragonflyValue(d.Name) + if d.Name == "" { + fmt.Fprint(w, "Hello, World!") + return + } + fmt.Fprintf(w, "Hello, %s!", html.EscapeString(d.Name)) +} + +func setDragonflyValue(name string) { + ctx := context.Background() + addr = os.Getenv("DFADDR") // format- : + pass = os.Getenv("DFPASS") // datastore password + + // Create a Redis client + client := redis.NewClient(&redis.Options{ + Addr: addr, + Password: pass, + DB: 0, // Use default DB + }) + + // 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 code uses two environment variables `DFADDR` and `DFPASS`. + +## Steps to Connect from the Cloud Run Function to a public data store + +### 1. Create a New Cloud Run Function + +1. Go to the [Cloud Run](https://console.cloud.google.com/run). +2. Click **Write a function**. +3. Choose **Go Runtime**. +4. Provide a name for your function (e.g., `DragonflyConnector`). +5. Expand the Containers section. +6. Edit the **Container Port** to match with the data store's port. +7. Add `DFADDR` and `DFPASS` environment variables. +8. Click **Create**. + +### 2. Test the Cloud Run Function + +1. Click the **Test** button in the console. +2. Create a new test event (you can use the default template). +3. Run the test. +4. Check the logs in **Logs** to verify the connection and the key-value pair operation. + +--- + +## Connect to a Private Datastore + +Private datastores 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 datastore, follow these beginner-friendly steps: + +### 1. Set Up VPC Peering + +1. Create a VPC in your GCP account within the same region as your datastore. +2. Establish a peering connection between your VPC and the datastore'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 datastore vpc. Put datastore vpc CIDR range in the Source Ipv4 range field. Allow all ports. + +### 3. Edit Cloud Run settings + +As the data store is private, you need to configure your cloud run service'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. Edit container port to your datastore's port. Update `DFADDR` and `DFPASS` (empty if passkey is not set). +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 **VM instance** tab and + create a vm instance. Make sure you've configured the network interface to use your vpc. +3. Update your firewall rule so that you can connect to the instance via ssh. +4. SSH to your machine. Run the command. + +By following these steps, you can securely connect your Cloud run service to a private Dragonfly datastore, 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. From 9ed45216b06ef5c0eacae405c8a415c593d8b53d Mon Sep 17 00:00:00 2001 From: Abhradeep Chakraborty Date: Tue, 8 Apr 2025 12:13:53 +0530 Subject: [PATCH 2/3] fix Signed-off-by: Abhradeep Chakraborty --- docs/cloud/connect/serverless/cloud-run.md | 6 ++++-- docs/cloud/connect/serverless/serverless.md | 1 + 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/docs/cloud/connect/serverless/cloud-run.md b/docs/cloud/connect/serverless/cloud-run.md index ab88ee0..537b477 100644 --- a/docs/cloud/connect/serverless/cloud-run.md +++ b/docs/cloud/connect/serverless/cloud-run.md @@ -128,7 +128,7 @@ The code uses two environment variables `DFADDR` and `DFPASS`. 1. Click the **Test** button in the console. 2. Create a new test event (you can use the default template). -3. Run the test. +3. Run the test in cloud shell. 4. Check the logs in **Logs** to verify the connection and the key-value pair operation. --- @@ -169,7 +169,9 @@ VPC network you just created. 2. You need to create a vm instance inside your vpc to run the test. Go to **VM instance** tab and create a vm instance. Make sure you've configured the network interface to use your vpc. 3. Update your firewall rule so that you can connect to the instance via ssh. -4. SSH to your machine. Run the command. +4. SSH to your machine. Run the test command. + +You'll see the logs in **Logs** that dragonfly has stored the value. By following these steps, you can securely connect your Cloud run service to a private Dragonfly datastore, ensuring your application remains both scalable and secure. diff --git a/docs/cloud/connect/serverless/serverless.md b/docs/cloud/connect/serverless/serverless.md index e6c7492..28bf905 100644 --- a/docs/cloud/connect/serverless/serverless.md +++ b/docs/cloud/connect/serverless/serverless.md @@ -10,3 +10,4 @@ slug: /cloud/connect/serverless 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](cloud-run.md) From 400780a41674ca9ead2037c6cf230f03bf492d1e Mon Sep 17 00:00:00 2001 From: Abhradeep Chakraborty Date: Tue, 8 Apr 2025 13:03:20 +0530 Subject: [PATCH 3/3] fix title Signed-off-by: Abhradeep Chakraborty --- docs/cloud/connect/serverless/cloud-run.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/cloud/connect/serverless/cloud-run.md b/docs/cloud/connect/serverless/cloud-run.md index 537b477..d73fff0 100644 --- a/docs/cloud/connect/serverless/cloud-run.md +++ b/docs/cloud/connect/serverless/cloud-run.md @@ -2,7 +2,7 @@ sidebar_position: 2 --- -# Connect from Cloud Run services +# Cloud Run Cloud Run is a serverless compute service provided by 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.