Skip to content

Commit 4d5f926

Browse files
committed
environment
1 parent 9014d26 commit 4d5f926

File tree

8 files changed

+22
-5
lines changed

8 files changed

+22
-5
lines changed

bin/darwin/lambda-local

4 KB
Binary file not shown.

bin/linux/lambda-local

0 Bytes
Binary file not shown.

controller/server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func (se *Server) StartConfig() {
8585
for _, functions := range se.JSON.Functions {
8686
check, parameters := checkPath(functions.Events[0].HttpEvent, r.URL.RequestURI(), r.Method)
8787
if check {
88-
result, off := lambda.ExecuteDockerLambda(se.Volume, se.Network, functions.Handler, se.JSON.Provider["runtime"], r.Body, parameters)
88+
result, off := lambda.ExecuteDockerLambda(se.Volume, se.Network, functions.Handler, se.JSON.Provider["runtime"], se.JSON.Environment, r.Body, parameters)
8989
if result.StatusCode == 0 {
9090
w.WriteHeader(400)
9191
fmt.Println(off)

example/go/adapters/hello/hello.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package main
22

33
import (
4+
"os"
5+
46
"github.com/aws/aws-lambda-go/events"
57
"github.com/aws/aws-lambda-go/lambda"
68
)
79

810
func HandleRequest(request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
911
return events.APIGatewayProxyResponse{
10-
Body: "Hello",
12+
Body: "Hello:" + os.Getenv("DATABASE") + os.Getenv("TABLE_NAME"),
1113
StatusCode: 200,
1214
}, nil
1315
}

example/go/bin/hello

0 Bytes
Binary file not shown.

example/go/serverless.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ provider:
44
name: aws
55
runtime: go1.x
66

7+
environment:
8+
DATABASE: "mydatabase"
9+
TABLE_NAME: table-${opt:stage, self:provider.stage}
10+
711
functions:
812
hello:
913
handler: bin/hello

lambda/lambda.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,25 @@ func PullImageDocker(runtime string) {
3737
io.Copy(os.Stdout, reader)
3838
}
3939

40-
func ExecuteDockerLambda(volume string, net string, handler string, runtime string, body io.ReadCloser, parameters map[string]string) (model.Result, string) {
40+
func ReplaceEnvironment(env string) string {
41+
return strings.ReplaceAll(env, "${opt:stage, self:provider.stage}", "dev")
42+
}
43+
44+
func ExecuteDockerLambda(volume string, net string, handler string, runtime string, environment map[string]string, body io.ReadCloser, parameters map[string]string) (model.Result, string) {
4145
var result model.Result
4246
var output bytes.Buffer
4347
var contentRequest ContentRequest
4448
buf := new(bytes.Buffer)
4549
buf.ReadFrom(body)
4650
bodyStr := buf.String()
51+
var strEnv []string
4752

4853
imageName := "lambci/lambda:" + runtime
4954

55+
for n, env := range environment {
56+
strEnv = append(strEnv, n+"="+ReplaceEnvironment(env))
57+
}
58+
5059
ctx := context.Background()
5160
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
5261
if err != nil {
@@ -76,6 +85,7 @@ func ExecuteDockerLambda(volume string, net string, handler string, runtime stri
7685
resp, err := cli.ContainerCreate(ctx, &container.Config{
7786
Image: imageName,
7887
Cmd: executeCommand,
88+
Env: strEnv,
7989
}, &container.HostConfig{
8090
Binds: []string{volume + ":/var/task"},
8191
}, networkingConfig, "")

model/serverless.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package model
22

33
type Serverless struct {
4-
Functions map[string]Functions `json:"functions"`
5-
Provider map[string]string `json:"provider"`
4+
Functions map[string]Functions `json:"functions"`
5+
Provider map[string]string `json:"provider"`
6+
Environment map[string]string `json:"environment"`
67
}
78

89
type Functions struct {

0 commit comments

Comments
 (0)