Skip to content

Commit 13526ec

Browse files
Abhra303Niennienzz
andauthored
feat(cloud): add cloud-run guide (#356)
* feat(cloud): add cloud-run guide Signed-off-by: Abhradeep Chakraborty <[email protected]> fix Signed-off-by: Abhradeep Chakraborty <[email protected]> * fix Signed-off-by: Abhradeep Chakraborty <[email protected]> * fix title Signed-off-by: Abhradeep Chakraborty <[email protected]> * styling fixes * styling fixes x02 * styling fixes x03 * go code and step fixes --------- Signed-off-by: Abhradeep Chakraborty <[email protected]> Co-authored-by: Joe Zhou <[email protected]>
1 parent 6e38732 commit 13526ec

File tree

3 files changed

+234
-30
lines changed

3 files changed

+234
-30
lines changed

docs/cloud/connect/serverless/aws-lambda.md

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,24 @@ import PageTitle from '@site/src/components/PageTitle';
88

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

11-
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.
11+
This guide explains how to create an AWS Lambda function that connects to a Dragonfly Cloud data store.
1212

13-
This guide explains how to create an AWS Lambda function that connects to a Dragonfly Cloud instance.
13+
[AWS Lambda](https://aws.amazon.com/lambda/) is a serverless compute service provided by Amazon Web Services (AWS).
14+
It allows you to run code without provisioning or managing servers.
15+
You simply upload your code, and Lambda automatically handles the execution, scaling, and availability.
16+
It supports various programming languages and integrates seamlessly with other AWS services,
17+
making it ideal for building scalable, event-driven applications.
18+
19+
**Note**: You can skip to the [Connecting to a Private Dragonfly Data Store](#connecting-to-a-private-dragonfly-data-store)
20+
section if you already have the Lambda function set up and just want to learn how to work with private Dragonfly Cloud data stores.
1421

1522
---
1623

1724
## Prerequisites
1825

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

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

36-
---
37-
38-
### 2. Add the Dragonfly URI as an Environment Variable
43+
### 2. Set Environment Variable(s)
3944

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

46-
---
47-
4851
### 3. Write the Lambda Function Code
4952

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

5255
```js
5356
import {createClient} from 'redis';
@@ -65,14 +68,14 @@ if (!redis.isReady) {
6568
console.log("ready")
6669
export const handler = async (event) => {
6770
try {
68-
// Test connection with basic operations
71+
// Test connection with some basic operations.
6972
await redis.set('lambda_test', JSON.stringify({
7073
timestamp: new Date().toISOString(),
7174
source: 'AWS Lambda'
7275
}));
73-
76+
7477
const result = await redis.get('lambda_test');
75-
78+
7679
return {
7780
statusCode: 200,
7881
body: JSON.stringify({
@@ -93,8 +96,6 @@ export const handler = async (event) => {
9396
};
9497
```
9598

96-
---
97-
9899
### 4. Build and Deploy the Lambda Function
99100

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

109-
2. **Package**: Aws Lambda takes zip file to load the code:
110+
2. **Package**: AWS Lambda takes a zip file to load the code:
110111

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

120-
---
121-
122121
### 5. Test the Lambda Function
123122

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

129128
---
130129

131-
### 6. Connecting to a Private Dragonfly Data Store
130+
## Connecting to a Private Dragonfly Data Store
132131

133-
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:
132+
Private data stores are hosted within a Virtual Private Cloud (VPC), which provides an isolated network environment.
133+
To enable your AWS Lambda function to securely connect to a private Dragonfly Cloud data store, follow these beginner-friendly steps:
134134

135-
#### 1. Set Up VPC Peering
135+
### 1. Set Up VPC Peering
136136

137137
1. Create a VPC in your AWS account within the same region as your data store.
138138
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).
139139

140-
#### 2. Adjust Security Group Rules
140+
### 2. Adjust Security Group Rules
141141

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

148-
#### 3. Grant Lambda the Necessary Permissions
148+
### 3. Grant Lambda Necessary Permissions
149149

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

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

157-
#### 4. Configure Lambda to Use the VPC
157+
### 4. Configure Lambda to Use the VPC
158158

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

166-
#### 5. Test the Connection
166+
### 5. Test the Connection
167167

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

173-
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.
173+
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.
174+
175+
---
174176

175177
## Conclusion
176178

177-
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.
179+
You have successfully created an AWS Lambda function that connects to Dragonfly Cloud,
180+
sets a test key-value pair, and verifies the connection.
181+
You can now extend this function to perform more complex operations with Dragonfly.
Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
---
2+
sidebar_position: 2
3+
---
4+
5+
import PageTitle from '@site/src/components/PageTitle';
6+
7+
# GCP Cloud Run
8+
9+
<PageTitle title="Connecting from GCP Cloud Run | Dragonfly Cloud" />
10+
11+
This guide explains how to create a GCP Cloud Run function that connects to a Dragonfly Cloud data store.
12+
13+
[GCP Cloud Run](https://cloud.google.com/run) is a serverless compute service provided by Google Cloud Platform (GCP).
14+
It allows you to run code without provisioning or managing servers.
15+
It supports various programming languages and integrates seamlessly with other GCP services,
16+
making it ideal for building scalable, event-driven applications.
17+
18+
Within the Cloud Run service,
19+
a [Cloud Run function](https://cloud.google.com/blog/products/serverless/google-cloud-functions-is-now-cloud-run-functions)
20+
is a deployment option that allows you to deploy inline code scripts or functions directly instead of deploying container images or code repositories.
21+
However, the process to connect to a Dragonfly Cloud data store is generally applicable to both Cloud Run services and functions.
22+
23+
**Note**: You can skip to the [Connecting to a Private Dragonfly Data Store](#connecting-to-a-private-dragonfly-data-store)
24+
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.
25+
26+
---
27+
28+
## Prerequisites
29+
30+
1. **Dragonfly Cloud Data Store**: Ensure you have a running [Dragonfly Cloud](https://dragonflydb.cloud/) data store and its connection URI.
31+
2. **GCP Console**: Access to Cloud Run and IAM services.
32+
3. **Go Toolchain**: In this guide, the Cloud Run service will be written in Go.
33+
4. **Redis Client Library**: Use the `go-redis` package to interact with Dragonfly.
34+
35+
---
36+
37+
## Cloud Run Function Example Code
38+
39+
For the purposes of this guide, a Cloud Run function implementation is provided for simplicity.
40+
Alternatively, a Cloud Run service deployment may be used instead.
41+
The connection process to a Dragonfly Cloud data store remains the same in either case.
42+
The following sample code will be deployed:
43+
44+
```go
45+
package helloworld
46+
47+
import (
48+
"encoding/json"
49+
"fmt"
50+
"html"
51+
"net/http"
52+
"os"
53+
"context"
54+
55+
"github.com/redis/go-redis/v9"
56+
"github.com/GoogleCloudPlatform/functions-framework-go/functions"
57+
)
58+
59+
func init() {
60+
functions.HTTP("HelloHTTP", helloHTTP)
61+
}
62+
63+
// An HTTP Cloud Function handler that responds to HTTP requests.
64+
func helloHTTP(w http.ResponseWriter, r *http.Request) {
65+
var req struct {
66+
Name string `json:"name"`
67+
}
68+
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
69+
fmt.Fprint(w, "Hello, World!")
70+
return
71+
}
72+
73+
setDragonflyValue(req.Name)
74+
if req.Name == "" {
75+
fmt.Fprint(w, "Hello, World!")
76+
return
77+
}
78+
fmt.Fprintf(w, "Hello, %s!", html.EscapeString(req.Name))
79+
}
80+
81+
func setDragonflyValue(name string) {
82+
ctx := context.Background()
83+
connectionURI := os.Getenv("DRAGONFLY_CONNECTION_URI")
84+
85+
// Dragonfly is compatible with the Redis protocol, we can use existing Redis libraries.
86+
opt, err := redis.ParseURL(connectionURI)
87+
if err != nil {
88+
fmt.Printf("Error parsing Dragonfly connection URI: %v\n", err)
89+
return
90+
}
91+
client := redis.NewClient(opt)
92+
93+
// Ping the server to test the connection.
94+
pong, err := client.Ping(ctx).Result()
95+
if err != nil {
96+
fmt.Printf("Error connecting to Dragonfly: %v\n", err)
97+
return
98+
}
99+
fmt.Printf("Connected to Dragonfly: %s\n", pong)
100+
101+
// Perform some test operations.
102+
err = client.Set(ctx, "test_key", name, 0).Err()
103+
if err != nil {
104+
fmt.Printf("Error setting key: %v\n", err)
105+
return
106+
}
107+
108+
value, err := client.Get(ctx, "test_key").Result()
109+
if err != nil {
110+
fmt.Printf("Error getting key: %v\n", err)
111+
return
112+
}
113+
fmt.Printf("Retrieved value: %s\n", value)
114+
115+
// Close the connection.
116+
err = client.Close()
117+
if err != nil {
118+
fmt.Printf("Error closing connection: %v\n", err)
119+
}
120+
}
121+
```
122+
123+
The example code requires an environment variable named `DRAGONFLY_CONNECTION_URI`.
124+
125+
---
126+
127+
## Connecting to a Public Dragonfly Data Store
128+
129+
### 1. Create a New Cloud Run Function
130+
131+
1. Go to the [Cloud Run](https://console.cloud.google.com/run) console.
132+
2. Click **Write a function**.
133+
3. Provide a name for your function/service (e.g., `HelloDragonfly`).
134+
4. Choose a Go runtime version that supports your function code.
135+
5. Expand the **Container(s)** section and add a new environment variable under the `Variables & Secrets` tab:
136+
- **Key**: `DRAGONFLY_CONNECTION_URI`
137+
- **Value**: Your Dragonfly Cloud connection URI (e.g., `rediss://<username>:<password>@<host>:<port>`).
138+
6. Click **Create** to create the function.
139+
7. Add your code or the code provided above to the inline editor and wait for the function to be deployed.
140+
141+
### 2. Test the Cloud Run Function
142+
143+
1. Click the **Test** button in the Cloud Run console.
144+
2. Create a new test event (you can use the default template).
145+
3. Run the test (you can use Cloud Shell for simplicity).
146+
4. Check the logs in **Logs** to verify the connection and the key-value pair operation.
147+
148+
---
149+
150+
## Connecting to a Private Dragonfly Data Store
151+
152+
Private data stores are hosted within a Virtual Private Cloud (VPC), which provides
153+
an isolated network environment. To enable your Cloud Run function to securely
154+
connect to a private Dragonfly Cloud data store, follow these beginner-friendly steps:
155+
156+
### 1. Set Up VPC Peering
157+
158+
1. Create a VPC in your GCP account within the same region as your data store.
159+
2. Establish a peering connection between your VPC and the data store's VPC.
160+
This allows the two networks to communicate. For detailed guidance, refer to the [VPC Peering Connections documentation](../../connections.md).
161+
162+
### 2. Adjust Firewall Rules
163+
164+
1. Open the [VPC Network Console](https://console.cloud.google.com/networking/networks/list).
165+
2. Select your network and open the **Firewall** settings.
166+
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.
167+
168+
### 3. Edit Cloud Run Settings
169+
170+
As the data store is private, you need to configure Cloud Run's network setting
171+
to the VPC network you just created.
172+
173+
1. Go to the [Cloud Run](https://console.cloud.google.com/run). Select your service.
174+
2. Navigate to the **Networking** tab.
175+
3. Select **Internal Ingress**. Save changes.
176+
4. Once deployed, click **Edit & deploy new version**.
177+
5. Make sure the `DRAGONFLY_CONNECTION_URI` environment variable is set to the data store's private connection URI.
178+
6. Go to **Networking**. Select **Connect to a VPC for outbound traffic**. Choose your VPC.
179+
7. Deploy the changes.
180+
181+
### 4. Test the Connection
182+
183+
1. Click **Test**. Copy the test command.
184+
2. You need to create a VM instance inside your VPC to run the test. Go to the **VM instances** tab and
185+
create a VM instance. Make sure you've configured the network interface to use your VPC.
186+
3. Update your firewall rules so that you can connect to the instance via SSH.
187+
4. SSH to your machine and run the test command.
188+
189+
You'll see in **Logs** that Dragonfly has stored the value.
190+
By following these steps, you can securely connect your Cloud Run service to a private Dragonfly Cloud data store,
191+
ensuring your application remains both scalable and secure.
192+
193+
---
194+
195+
## Conclusion
196+
197+
You have successfully created a GCP Cloud Run function that connects to Dragonfly Cloud,
198+
sets a test key-value pair, and verifies the connection.
199+
You can now extend this function to perform more complex operations with Dragonfly.

docs/cloud/connect/serverless/serverless.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ import PageTitle from '@site/src/components/PageTitle';
1414
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.
1515

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

0 commit comments

Comments
 (0)