generated from Real-Dev-Squad/website-template
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
4 changed files
with
76 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package utils | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/session" | ||
"github.com/aws/aws-sdk-go/service/lambda" | ||
) | ||
|
||
// ProfileLambdaCallPayload represents the payload to send to the CallProfileFunction | ||
type ProfileLambdaCallPayload struct { | ||
UserId string `json:"userId"` | ||
SessionID string `json:"sessionId"` | ||
} | ||
|
||
// APIGatewayProxyRequestWrapper wraps the ProfileLambdaCallPayload inside the Body field | ||
type APIGatewayProxyRequestWrapper struct { | ||
Body string `json:"body"` | ||
} | ||
|
||
func InvokeProfileLambda(payload ProfileLambdaCallPayload) error { | ||
session := session.Must(session.NewSession()) | ||
client := lambda.New(session) | ||
|
||
payloadBytes, err := json.Marshal(payload) | ||
if err != nil { | ||
return fmt.Errorf("error marshalling payload: %w", err) | ||
} | ||
|
||
// wrap the payload inside the body field | ||
wrapper := APIGatewayProxyRequestWrapper{ | ||
Body: string(payloadBytes), | ||
} | ||
|
||
// marshal the wrapper back to json | ||
wrapperBytes, err := json.Marshal(wrapper) | ||
if err != nil { | ||
return fmt.Errorf("error marshalling wrapper: %w", err) | ||
} | ||
|
||
functionName := os.Getenv("profileFunctionLambdaName") | ||
if functionName == "" { | ||
return fmt.Errorf("profileFunctionLambdaName is not set") | ||
} | ||
|
||
input := &lambda.InvokeInput{ | ||
FunctionName: aws.String(functionName), | ||
Payload: wrapperBytes, | ||
} | ||
|
||
_, err = client.Invoke(input) | ||
if err != nil { | ||
return fmt.Errorf("error invoking lambda: %w", err) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters