Skip to content

Commit

Permalink
update call-profiles to invoke call-profile using lambdaInvokd
Browse files Browse the repository at this point in the history
  • Loading branch information
JC-Coder committed Oct 4, 2024
1 parent 663bc5c commit b09a524
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 19 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
env.json
samconfig.toml
*/firestore-debug.log
scripts/local-dev.sh
scripts/deploy-local.sh
26 changes: 15 additions & 11 deletions call-profiles/main.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package main

import (
"bytes"
"context"
"fmt"
"identity-service/layer/utils"
"log"
"net/http"
"os"
"sync"
"time"

Expand All @@ -20,25 +17,27 @@ import (
var wg sync.WaitGroup

func callProfile(userId string, sessionId string) {
log.Printf("Calling profile for user with ID: %s\n", userId)

defer wg.Done()

httpClient := &http.Client{}
jsonBody := []byte(fmt.Sprintf(`{"userId": "%s", "sessionId": "%s"}`, userId, sessionId))
bodyReader := bytes.NewReader(jsonBody)
payload := utils.ProfileLambdaCallPayload{
UserId: userId,
SessionID: sessionId,
}

requestURL := fmt.Sprintf("%s/profile", os.Getenv("baseURL"))
req, _ := http.NewRequest(http.MethodPost, requestURL, bodyReader)
_, err1 := httpClient.Do(req)
if err1 != nil {
fmt.Println("error getting profile data", err1)
err := utils.InvokeProfileLambda(payload)
if err != nil {
log.Println("error calling profile lambda", err)
}
}

func handler(request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
ctx := context.Background()
client, err := utils.InitializeFirestoreClient(ctx)

fmt.Println("Calling profiles - entry point")

if err != nil {
return events.APIGatewayProxyResponse{}, err
}
Expand All @@ -53,7 +52,12 @@ func handler(request events.APIGatewayProxyRequest) (events.APIGatewayProxyRespo

totalProfilesCalled := 0

// TODO: remove this
allUsers := client.Collection("users").Documents(ctx)
log.Println("All users", allUsers)

iter := client.Collection("users").Where("profileStatus", "==", "VERIFIED").Documents(ctx)
log.Println("Iterating over users", iter)
for {
doc, err := iter.Next()
if err == iterator.Done {
Expand Down
2 changes: 1 addition & 1 deletion layer/utils/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func getParameter(parameter string) string {
Name: &parameterName,
})
if err != nil {
log.Fatalf(err.Error())
log.Print(err.Error())
}

return *results.Parameter.Value
Expand Down
44 changes: 44 additions & 0 deletions layer/utils/lambdacalls.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package utils

import (
"encoding/json"
"fmt"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/lambda"
)

type ProfileLambdaCallPayload struct {
UserId string `json:"userId"`
SessionID string `json:"sessionId"`
}

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)
}

functionName := "CallProfileFunction"
fmt.Println("functionName", functionName)
if functionName == "" {
return fmt.Errorf("PROFILE_LAMBDA_FUNCTION_ARN is not set")
}

input := &lambda.InvokeInput{
FunctionName: aws.String(functionName),
Payload: payloadBytes,
}

_, err = client.Invoke(input)
if err != nil {
fmt.Println("error invoking lambda", err)
return fmt.Errorf("error invoking lambda: %w", err)
}

return nil
}
14 changes: 7 additions & 7 deletions template.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
AWSTemplateFormatVersion: "2010-09-09"
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
identity-service
Expand All @@ -19,7 +19,6 @@ Globals:
identityServicePrivateKey: YourIdentityServicePrivateKey

Resources:

UtilitiesLayer:
Type: AWS::Serverless::LayerVersion
Properties:
Expand Down Expand Up @@ -58,7 +57,7 @@ Resources:
Handler: bootstrap
Runtime: provided.al2023
Layers:
- !Ref UtilitiesLayer
- !Ref UtilitiesLayer
Architectures:
- x86_64
Tracing: Active
Expand All @@ -83,7 +82,7 @@ Resources:
Handler: bootstrap
Runtime: provided.al2023
Layers:
- !Ref UtilitiesLayer
- !Ref UtilitiesLayer
Architectures:
- x86_64
Tracing: Active
Expand All @@ -103,7 +102,7 @@ Resources:
Handler: bootstrap
Runtime: provided.al2023
Layers:
- !Ref UtilitiesLayer
- !Ref UtilitiesLayer
Architectures:
- x86_64
Tracing: Active
Expand All @@ -124,11 +123,12 @@ Resources:
Metadata:
BuildMethod: go1.x
Properties:
FunctionName: CallProfileFunction
CodeUri: call-profile/
Handler: bootstrap
Runtime: provided.al2023
Layers:
- !Ref UtilitiesLayer
- !Ref UtilitiesLayer
Architectures:
- x86_64
Tracing: Active
Expand All @@ -137,4 +137,4 @@ Resources:
Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api
Properties:
Path: /profile
Method: POST
Method: POST

0 comments on commit b09a524

Please sign in to comment.