Skip to content

Commit 1bd4e9b

Browse files
authored
Merge pull request #226 from JC-Coder/use-lambda-invoke-instead-of-http
update call-profiles to invoke call-profile using lambda Invoke to reduce latency
2 parents 663bc5c + e98dc11 commit 1bd4e9b

File tree

4 files changed

+76
-20
lines changed

4 files changed

+76
-20
lines changed

call-profiles/main.go

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
package main
22

33
import (
4-
"bytes"
54
"context"
65
"fmt"
76
"identity-service/layer/utils"
87
"log"
9-
"net/http"
10-
"os"
118
"sync"
129
"time"
1310

@@ -20,18 +17,16 @@ import (
2017
var wg sync.WaitGroup
2118

2219
func callProfile(userId string, sessionId string) {
23-
2420
defer wg.Done()
2521

26-
httpClient := &http.Client{}
27-
jsonBody := []byte(fmt.Sprintf(`{"userId": "%s", "sessionId": "%s"}`, userId, sessionId))
28-
bodyReader := bytes.NewReader(jsonBody)
22+
payload := utils.ProfileLambdaCallPayload{
23+
UserId: userId,
24+
SessionID: sessionId,
25+
}
2926

30-
requestURL := fmt.Sprintf("%s/profile", os.Getenv("baseURL"))
31-
req, _ := http.NewRequest(http.MethodPost, requestURL, bodyReader)
32-
_, err1 := httpClient.Do(req)
33-
if err1 != nil {
34-
fmt.Println("error getting profile data", err1)
27+
err := utils.InvokeProfileLambda(payload)
28+
if err != nil {
29+
log.Println("error calling profile lambda", err)
3530
}
3631
}
3732

layer/utils/auth.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func getParameter(parameter string) string {
4747
Name: &parameterName,
4848
})
4949
if err != nil {
50-
log.Fatalf(err.Error())
50+
log.Print(err.Error())
5151
}
5252

5353
return *results.Parameter.Value

layer/utils/lambdacalls.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package utils
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"os"
7+
8+
"github.com/aws/aws-sdk-go/aws"
9+
"github.com/aws/aws-sdk-go/aws/session"
10+
"github.com/aws/aws-sdk-go/service/lambda"
11+
)
12+
13+
// ProfileLambdaCallPayload represents the payload to send to the CallProfileFunction
14+
type ProfileLambdaCallPayload struct {
15+
UserId string `json:"userId"`
16+
SessionID string `json:"sessionId"`
17+
}
18+
19+
// APIGatewayProxyRequestWrapper wraps the ProfileLambdaCallPayload inside the Body field
20+
type APIGatewayProxyRequestWrapper struct {
21+
Body string `json:"body"`
22+
}
23+
24+
func InvokeProfileLambda(payload ProfileLambdaCallPayload) error {
25+
session := session.Must(session.NewSession())
26+
client := lambda.New(session)
27+
28+
payloadBytes, err := json.Marshal(payload)
29+
if err != nil {
30+
return fmt.Errorf("error marshalling payload: %w", err)
31+
}
32+
33+
// wrap the payload inside the body field
34+
wrapper := APIGatewayProxyRequestWrapper{
35+
Body: string(payloadBytes),
36+
}
37+
38+
// marshal the wrapper back to json
39+
wrapperBytes, err := json.Marshal(wrapper)
40+
if err != nil {
41+
return fmt.Errorf("error marshalling wrapper: %w", err)
42+
}
43+
44+
functionName := os.Getenv("profileFunctionLambdaName")
45+
if functionName == "" {
46+
return fmt.Errorf("profileFunctionLambdaName is not set")
47+
}
48+
49+
input := &lambda.InvokeInput{
50+
FunctionName: aws.String(functionName),
51+
Payload: wrapperBytes,
52+
}
53+
54+
_, err = client.Invoke(input)
55+
if err != nil {
56+
return fmt.Errorf("error invoking lambda: %w", err)
57+
}
58+
59+
return nil
60+
}

template.yaml

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
AWSTemplateFormatVersion: "2010-09-09"
1+
AWSTemplateFormatVersion: '2010-09-09'
22
Transform: AWS::Serverless-2016-10-31
33
Description: >
44
identity-service
@@ -17,9 +17,9 @@ Globals:
1717
baseURL: YourBaseAPIURL
1818
discordBotURL: DiscordBotURL
1919
identityServicePrivateKey: YourIdentityServicePrivateKey
20+
profileFunctionLambdaName: identity-service-CallProfileFunction
2021

2122
Resources:
22-
2323
UtilitiesLayer:
2424
Type: AWS::Serverless::LayerVersion
2525
Properties:
@@ -58,7 +58,7 @@ Resources:
5858
Handler: bootstrap
5959
Runtime: provided.al2023
6060
Layers:
61-
- !Ref UtilitiesLayer
61+
- !Ref UtilitiesLayer
6262
Architectures:
6363
- x86_64
6464
Tracing: Active
@@ -83,7 +83,7 @@ Resources:
8383
Handler: bootstrap
8484
Runtime: provided.al2023
8585
Layers:
86-
- !Ref UtilitiesLayer
86+
- !Ref UtilitiesLayer
8787
Architectures:
8888
- x86_64
8989
Tracing: Active
@@ -103,7 +103,7 @@ Resources:
103103
Handler: bootstrap
104104
Runtime: provided.al2023
105105
Layers:
106-
- !Ref UtilitiesLayer
106+
- !Ref UtilitiesLayer
107107
Architectures:
108108
- x86_64
109109
Tracing: Active
@@ -124,11 +124,12 @@ Resources:
124124
Metadata:
125125
BuildMethod: go1.x
126126
Properties:
127+
FunctionName: identity-service-CallProfileFunction
127128
CodeUri: call-profile/
128129
Handler: bootstrap
129130
Runtime: provided.al2023
130131
Layers:
131-
- !Ref UtilitiesLayer
132+
- !Ref UtilitiesLayer
132133
Architectures:
133134
- x86_64
134135
Tracing: Active
@@ -137,4 +138,4 @@ Resources:
137138
Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api
138139
Properties:
139140
Path: /profile
140-
Method: POST
141+
Method: POST

0 commit comments

Comments
 (0)