Skip to content

Commit e293672

Browse files
http2 gRPC go tutorial (#83)
* (wip) http2 gRPC go tutorial * complete readme + tutorial files * add testing step * replaced my container with placeholder * Apply suggestions from code review Co-authored-by: SamyOubouaziz <[email protected]> * smoothen packages --------- Co-authored-by: SamyOubouaziz <[email protected]>
1 parent 3db6e78 commit e293672

File tree

10 files changed

+584
-14
lines changed

10 files changed

+584
-14
lines changed

README.md

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Table of Contents:
1919
- [🚀 Functions](#-functions)
2020
- [📦 Containers](#-containers)
2121
- [⚙️ Jobs](#️-jobs)
22-
- [💬 Messaging and Queueing](#-mnq)
22+
- [💬 Messaging and Queueing](#-messaging-and-queueing)
2323
- [💜 Projects](#-projects)
2424
- [Contributing](#contributing)
2525

@@ -28,8 +28,9 @@ Table of Contents:
2828
### 🚀 Functions
2929

3030
<!-- markdownlint-disable MD033 -->
31+
3132
| Example | Runtime | Deployment |
32-
|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------|------------------------|
33+
| ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------- | ---------------------- |
3334
| **[Badge PHP](functions/badge-php/README.md)** <br/> A PHP function to generate repository badges. | php82 | [Serverless Framework] |
3435
| **[CORS Go](functions/cors-go/README.md)** <br/> A Go function which allows CORS requests. | go119 | [Serverless Framework] |
3536
| **[CORS Node](functions/cors-node/README.md)** <br/> A Node function which allows CORS requests. | node18 | [Serverless Framework] |
@@ -57,12 +58,12 @@ Table of Contents:
5758
| **[Typescript with Node runtime](functions/typescript-with-node/README.md)** <br/> A Typescript function using Node runtime. | node18 | [Serverless Framework] |
5859
| **[Serverless Gateway Python Example](functions/serverless-gateway-python/README.md)** <br/> A Python serverless API using Serverless Gateway. | python310 | [Python API Framework] |
5960
| **[Go and Transactional Email](functions/go-mail/README.md)** <br/> A Go function that send emails using Scaleway SDK. | go121 | [Serverless Framework] |
60-
| **[Rotate RDB Credentials](functions/secret-manager-rotate-secret/README.md)** <br/> A Go function that rotates RDB credentials stored in Secret Manager. | go120 | [Serverless Framework] |
61+
| **[Rotate RDB Credentials](functions/secret-manager-rotate-secret/README.md)** <br/> A Go function that rotates RDB credentials stored in Secret Manager. | go120 | [Serverless Framework] |
6162

6263
### 📦 Containers
6364

6465
| Example | Language | Deployment |
65-
|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|--------------|------------------------|
66+
| ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------ | ---------------------- |
6667
| **[Container Bash Script](containers/bash-scheduled-job/README.md)** <br/> A Bash script runnning on a schedule using serverless containers. | Bash | [Serverless Framework] |
6768
| **[Function Handler Java](containers/function-handler-java/README.md)** <br/> A Java function handler deployed on CaaS. | Java | [Serverless Framework] |
6869
| **[NGINX CORS Private](containers/nginx-cors-private-python/README.md)** <br/> An NGINX proxy to allow CORS requests to a private container. | Python Flask | [Terraform] |
@@ -71,26 +72,27 @@ Table of Contents:
7172
| **[Python S3 upload](containers/python-s3-upload/README.md)** <br/> A Python + Flask HTTP server that receives file uploads and writes them to S3. | Python | [Terraform] |
7273
| **[Terraform NGINX hello world](containers/terraform-nginx-hello-world/README.md)** <br/> A minimal example running the base NGINX image in a serverless container deployed with Terraform. | N/A | [Terraform] |
7374
| **[Triggers with Terraform](containers/terraform-triggers/README.md)** <br/> Configuring two SQS triggers, used to trigger two containers, one public, one private. | N/A | [Terraform] |
75+
| **[gRPC HTTP2 in Go](containers/grpc-http2-go/README.md)** <br/> A Go gRPC Container using http2 | Go/Protobuf | [CLI] |
7476

7577
### ⚙️ Jobs
7678

77-
| Example | Language | Deployment |
78-
|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|--------------|------------------------|
79-
| **[Serverless Jobs Hello World](jobs/terraform-hello-world/README.md)** <br/> An example of building a container image and running it as a Serverless Job using Terraform. | N/A |[Terraform]-[Console]|
80-
| **[Serverless MLOps](jobs/ml-ops/README.md)** <br/> An example of running a Serverless Machine Leaning workflow. | Python |[Terraform]-[Console]-[CLI]|
79+
| Example | Language | Deployment |
80+
| -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------- | --------------------------- |
81+
| **[Serverless Jobs Hello World](jobs/terraform-hello-world/README.md)** <br/> An example of building a container image and running it as a Serverless Job using Terraform. | N/A | [Terraform]-[Console] |
82+
| **[Serverless MLOps](jobs/ml-ops/README.md)** <br/> An example of running a Serverless Machine Leaning workflow. | Python | [Terraform]-[Console]-[CLI] |
8183

8284
### 💬 Messaging and Queueing
8385

84-
| Example | Services | Language | Deployment |
85-
|---------------------------------------------------------------------------------------------------------------------------------------------------------|------------------|----------|-------------|
86-
| **[Manage large message](mnq/large-messages/README.md)** <br/> An example of infrastructure to manage large messages. | PaaS & S3 | Python | [Terraform] |
87-
| **[Serverless scraping](mnq/serverless-scraping/README.md)** <br/> An example of infrastructure to scrape the hackernews website. | PaaS & RDB | Python | [Terraform] |
88-
| **[SNS Instances Notification System](mnq/sns-instances-notification-system/README.md)** <br/> An example of infrastructure to use SNS with Instances. | PaaS & Instances | Golang | [Terraform] |
86+
| Example | Services | Language | Deployment |
87+
| ------------------------------------------------------------------------------------------------------------------------------------------------------ | ---------------- | -------- | ----------- |
88+
| **[Manage large message](mnq/large-messages/README.md)** <br/> An example of infrastructure to manage large messages. | PaaS & S3 | Python | [Terraform] |
89+
| **[Serverless scraping](mnq/serverless-scraping/README.md)** <br/> An example of infrastructure to scrape the hackernews website. | PaaS & RDB | Python | [Terraform] |
90+
| **[SNS Instances Notification System](mnq/sns-instances-notification-system/README.md)** <br/> An example of infrastructure to use SNS with Instances. | PaaS & Instances | Golang | [Terraform] |
8991

9092
### 💜 Projects
9193

9294
| Example | Services | Language | Deployment |
93-
|-------------------------------------------------------------------------------------------------------------------------------------------|-------------|----------|------------------------|
95+
| ----------------------------------------------------------------------------------------------------------------------------------------- | ----------- | -------- | ---------------------- |
9496
| **[Kong API Gateway](projects/kong-api-gateway/README.md)** <br/> Deploying a Kong Gateway on containers to provide routing to functions. | CaaS & FaaS | Python | [Serverless Framework] |
9597
| **[Serverless Gateway](https://github.com/scaleway/serverless-gateway)** <br/> Our serverless gateway for functions and containers. | API Gateway | Python | [Python API Framework] |
9698
| **[Monitoring Glaciers](projects/blogpost-glacier/README.md)** <br/> A project to monitor glaciers and the impact of global warming. | S3 & RDB | Golang | [Serverless Framework] |
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
FROM golang:1.22-bookworm as build
2+
3+
# Set working directory
4+
WORKDIR /app
5+
6+
# Copy required files
7+
COPY go.mod .
8+
COPY go.sum .
9+
COPY server/*.go ./server/
10+
COPY hello.proto ./
11+
12+
# We install the protobuf compilation stack on the image directly to simplify workflow
13+
RUN apt-get update && apt-get install -y protobuf-compiler
14+
RUN go install google.golang.org/protobuf/cmd/[email protected]
15+
RUN go install google.golang.org/grpc/cmd/[email protected]
16+
17+
# Generate go protobuf files
18+
RUN protoc --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative hello.proto
19+
20+
# Build the executable
21+
RUN GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build server/main.go
22+
23+
# Put the executable in a light scratch image
24+
FROM scratch
25+
26+
COPY --from=build /app/main /server
27+
28+
ENTRYPOINT ["/server"]

containers/grpc-http2-go/README.md

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# gRPC HTTP2 Server in Go using CLI
2+
3+
This example demonstrates the deployment of a gRPC service on Scaleway Serverless Containers.
4+
5+
For this example, we will use the CLI to deploy the container, but you can use [other methods](https://www.scaleway.com/en/docs/serverless/containers/reference-content/deploy-container/).
6+
You can also use the CLI directly from Scaleway console without having to use your credentials.
7+
8+
## Workflow
9+
10+
Here are the different steps we are going to proceed:
11+
12+
- Quick set-up of Container Registry to host our gRPC container
13+
- Deploy the Serverless Container
14+
- Run gRPC test command ot ensure everything is ok
15+
16+
## Deployment
17+
18+
### Requirements
19+
20+
To complete the actions presented below, you must have:
21+
- installed and configured the [Scaleway CLI](https://www.scaleway.com/en/docs/developer-tools/scaleway-cli/quickstart/)
22+
- installed [Docker](https://docs.docker.com/engine/install/) to build the image
23+
- installed the common [gRPC stack](https://grpc.io/blog/installation/)) to test locally (optional)
24+
25+
### Building the image
26+
27+
1. Run the following command in a terminal to create Container Registry namespace to store the image:
28+
29+
```bash
30+
scw registry namespace create name=hello-grpc
31+
```
32+
33+
The registry namespace information displays.
34+
35+
1. Copy the namespace endpoint (in this case, `rg.fr-par.scw.cloud/hello-grpc`).
36+
37+
1. Log into the Container Registry namespace you created using Docker:
38+
39+
```bash
40+
docker login rg.fr-par.scw.cloud/hello-grpc -u nologin --password-stdin <<< "$SCW_SECRET_KEY"
41+
```
42+
43+
At this point, you have correctly set up Docker to be able to push your image online.
44+
45+
1. In a terminal, access this directory (containing the Dockerfile), and run the following command to build the image:
46+
47+
```bash
48+
docker build -t grpc:latest .
49+
```
50+
51+
1. Tag and push the image to the registry namespace:
52+
53+
```bash
54+
docker tag grpc:latest rg.fr-par.scw.cloud/hello-grpc/grpc:latest
55+
docker push rg.fr-par.scw.cloud/hello-grpc/grpc:latest
56+
```
57+
58+
### Deploying the image
59+
60+
In a terminal, run the following command to create a Serverless Containers namespace:
61+
62+
```bash
63+
scw container namespace create name=grpc-test
64+
```
65+
The namespace information displays.
66+
67+
1. Copy the namespace ID.
68+
69+
1. Run the following command to create and deploy the container (make sure to use the `h2c` protocol to connect via HTTP2):
70+
71+
```bash
72+
scw container container create namespace-id=<PREVIOUS_NAMESPACE_ID> protocol=h2c name=grpc-test registry-image=rg.fr-par.scw.cloud/hello-grpc/grpc:latest
73+
```
74+
The container information displays.
75+
76+
1. Copy the DomainName (endpoint) to test your container.
77+
78+
### Testing
79+
80+
Make sure your container is in a `ready` status before testing it.
81+
82+
1. In the `client/client.go` file, replace the constant `containerEndpoint` with the `DomainName` copied previously.
83+
84+
1. Make sure to keep the `:80` port at the end even if you container port is set to 8080, as these are two different settings.
85+
86+
1. Run the command below to check if your container responds:
87+
88+
`go run client/client.go'
89+
90+
## Additional content
91+
92+
- [Basic Go gRPC tutorial](https://grpc.io/docs/languages/go/basics/)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"flag"
6+
"log"
7+
"time"
8+
9+
pb "github.com/scaleway/serverless-examples/containers/grpc-http2-go"
10+
"google.golang.org/grpc"
11+
)
12+
13+
const containerEndpoint = "YOUR_CONTAINER_ENDPOINT:80"
14+
15+
func main() {
16+
flag.Parse()
17+
// Set up a connection to the server.
18+
conn, err := grpc.Dial(containerEndpoint, grpc.WithInsecure())
19+
if err != nil {
20+
log.Fatalf("did not connect: %v", err)
21+
}
22+
23+
defer conn.Close()
24+
c := pb.NewGreeterClient(conn)
25+
26+
// Contact the server and print out its response.
27+
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
28+
defer cancel()
29+
30+
r, err := c.SayHello(ctx, &pb.HelloRequest{Name: "Scaleway"})
31+
if err != nil {
32+
log.Fatalf("could not greet: %v", err)
33+
}
34+
35+
log.Printf("Greeting: %s", r.GetMessage())
36+
}

containers/grpc-http2-go/go.mod

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module github.com/scaleway/serverless-examples/containers/grpc-http2-go
2+
3+
go 1.22.1
4+
5+
require (
6+
google.golang.org/grpc v1.63.2
7+
google.golang.org/protobuf v1.33.0
8+
)
9+
10+
require (
11+
golang.org/x/net v0.24.0 // indirect
12+
golang.org/x/sys v0.19.0 // indirect
13+
golang.org/x/text v0.14.0 // indirect
14+
google.golang.org/genproto/googleapis/rpc v0.0.0-20240415180920-8c6c420018be // indirect
15+
)
16+
17+
replace github.com/scaleway/serverless-examples/containers/grpc-http2-go => .

containers/grpc-http2-go/go.sum

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
2+
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
3+
golang.org/x/net v0.24.0 h1:1PcaxkF854Fu3+lvBIx5SYn9wRlBzzcnHZSiaFFAb0w=
4+
golang.org/x/net v0.24.0/go.mod h1:2Q7sJY5mzlzWjKtYUEXSlBWCdyaioyXzRB2RtU8KVE8=
5+
golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o=
6+
golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
7+
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
8+
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
9+
google.golang.org/genproto/googleapis/rpc v0.0.0-20240415180920-8c6c420018be h1:LG9vZxsWGOmUKieR8wPAUR3u3MpnYFQZROPIMaXh7/A=
10+
google.golang.org/genproto/googleapis/rpc v0.0.0-20240415180920-8c6c420018be/go.mod h1:WtryC6hu0hhx87FDGxWCDptyssuo68sk10vYjF+T9fY=
11+
google.golang.org/grpc v1.63.2 h1:MUeiw1B2maTVZthpU5xvASfTh3LDbxHd6IJ6QQVU+xM=
12+
google.golang.org/grpc v1.63.2/go.mod h1:WAX/8DgncnokcFUldAxq7GeB5DXHDbMF+lLvDomNkRA=
13+
google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI=
14+
google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=

0 commit comments

Comments
 (0)