Skip to content

Commit 6035ae5

Browse files
committed
Add README.md
1 parent ff669f6 commit 6035ae5

File tree

1 file changed

+86
-2
lines changed

1 file changed

+86
-2
lines changed

README.md

Lines changed: 86 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,86 @@
1-
# lambda-go
2-
Serverless Computing
1+
# Lambda-Go
2+
3+
This project is an attempt to understand how the most popular serverless computing solution, Lambda functions, works.
4+
5+
It was inspired by the most popular solution, [AWS Lambda](https://aws.amazon.com/lambda/).
6+
7+
## How it works
8+
9+
At this stage, the project is a web server with two endpoints.
10+
Server port should be specified in the `APP_SERVER_PORT` environment variable.
11+
12+
Also we need to specify logs level in the `APP_LOG_LEVEL` environment variable.
13+
14+
For proper operation, the server must have access to the ***Docker*** daemon,
15+
which is used to deploy our functions in containers.
16+
17+
### Create function
18+
19+
This endpoint allows us to upload an archive with Lambda function code to the server.
20+
21+
```shell
22+
curl --location 'localhost:9000/lambda/{func_name}/create' \
23+
--form 'file=@"/func.tar.gz"'
24+
```
25+
26+
To create a new Lambda function, you need to specify the function name `{func_name}` and a tar.gz archive.
27+
28+
The archive must contain three files:
29+
* main.go
30+
* go.mod
31+
* go.sum
32+
33+
The code for the application should look like the following:
34+
35+
```go
36+
package main
37+
38+
import (
39+
"bytes"
40+
"context"
41+
"encoding/json"
42+
"fmt"
43+
44+
"github.com/ihippik/lambda-go/lambda"
45+
)
46+
47+
type Data struct {
48+
Name string `json:"name"`
49+
}
50+
51+
func hello(_ context.Context, data []byte) ([]byte, error) {
52+
var req Data
53+
54+
if err := json.NewDecoder(bytes.NewReader(data)).Decode(&req); err != nil {
55+
return nil, err
56+
}
57+
58+
return []byte(fmt.Sprintf("Hello %s!", req.Name)), nil
59+
}
60+
61+
func main() {
62+
lambda.Start(hello)
63+
}
64+
```
65+
66+
All handlers must have the following signature:
67+
68+
```go
69+
type Handler func(ctx context.Context, payload []byte) ([]byte, error)
70+
```
71+
72+
To prepare the archive, you can use the following command:
73+
74+
```shell
75+
tar -czvf func.tar.gz main.go go.mod go.sum
76+
```
77+
78+
### Invoke function
79+
80+
This endpoint allows us to run a previously uploaded function `{func_name}`
81+
82+
```shell
83+
curl --location 'localhost:9000/lambda/{func_name}/invoke' \
84+
--header 'Content-Type: application/json' \
85+
--data '{"name": "Ivan"}'
86+
```

0 commit comments

Comments
 (0)