From b09a5240f76679fd1ad18e6f1b812aa448512347 Mon Sep 17 00:00:00 2001 From: JC CODER Date: Fri, 4 Oct 2024 01:23:51 +0100 Subject: [PATCH 1/6] update call-profiles to invoke call-profile using lambdaInvokd --- .gitignore | 2 ++ call-profiles/main.go | 26 ++++++++++++---------- layer/utils/auth.go | 2 +- layer/utils/lambdacalls.go | 44 ++++++++++++++++++++++++++++++++++++++ template.yaml | 14 ++++++------ 5 files changed, 69 insertions(+), 19 deletions(-) create mode 100644 layer/utils/lambdacalls.go diff --git a/.gitignore b/.gitignore index 7a0a5ad..3d16dc0 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,5 @@ env.json samconfig.toml */firestore-debug.log +scripts/local-dev.sh +scripts/deploy-local.sh diff --git a/call-profiles/main.go b/call-profiles/main.go index cfd72eb..8c80535 100644 --- a/call-profiles/main.go +++ b/call-profiles/main.go @@ -1,13 +1,10 @@ package main import ( - "bytes" "context" "fmt" "identity-service/layer/utils" "log" - "net/http" - "os" "sync" "time" @@ -20,18 +17,18 @@ 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) } } @@ -39,6 +36,8 @@ func handler(request events.APIGatewayProxyRequest) (events.APIGatewayProxyRespo ctx := context.Background() client, err := utils.InitializeFirestoreClient(ctx) + fmt.Println("Calling profiles - entry point") + if err != nil { return events.APIGatewayProxyResponse{}, err } @@ -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 { diff --git a/layer/utils/auth.go b/layer/utils/auth.go index 95bcd6e..69a770f 100644 --- a/layer/utils/auth.go +++ b/layer/utils/auth.go @@ -47,7 +47,7 @@ func getParameter(parameter string) string { Name: ¶meterName, }) if err != nil { - log.Fatalf(err.Error()) + log.Print(err.Error()) } return *results.Parameter.Value diff --git a/layer/utils/lambdacalls.go b/layer/utils/lambdacalls.go new file mode 100644 index 0000000..0d0da93 --- /dev/null +++ b/layer/utils/lambdacalls.go @@ -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 +} diff --git a/template.yaml b/template.yaml index fc0d2a3..8a14674 100644 --- a/template.yaml +++ b/template.yaml @@ -1,4 +1,4 @@ -AWSTemplateFormatVersion: "2010-09-09" +AWSTemplateFormatVersion: '2010-09-09' Transform: AWS::Serverless-2016-10-31 Description: > identity-service @@ -19,7 +19,6 @@ Globals: identityServicePrivateKey: YourIdentityServicePrivateKey Resources: - UtilitiesLayer: Type: AWS::Serverless::LayerVersion Properties: @@ -58,7 +57,7 @@ Resources: Handler: bootstrap Runtime: provided.al2023 Layers: - - !Ref UtilitiesLayer + - !Ref UtilitiesLayer Architectures: - x86_64 Tracing: Active @@ -83,7 +82,7 @@ Resources: Handler: bootstrap Runtime: provided.al2023 Layers: - - !Ref UtilitiesLayer + - !Ref UtilitiesLayer Architectures: - x86_64 Tracing: Active @@ -103,7 +102,7 @@ Resources: Handler: bootstrap Runtime: provided.al2023 Layers: - - !Ref UtilitiesLayer + - !Ref UtilitiesLayer Architectures: - x86_64 Tracing: Active @@ -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 @@ -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 \ No newline at end of file + Method: POST From 0a70b72c0e61099fa3d98cd3b7a0fedb8f7e5c0b Mon Sep 17 00:00:00 2001 From: JC CODER Date: Sat, 5 Oct 2024 17:12:33 +0100 Subject: [PATCH 2/6] fix: update call-profiles to use lambdaInvoked for profile calls - Removed unnecessary logging statements - Simplified the InvokeProfileLambda function by removing redundant logging - Removed unused local development scripts from .gitignore --- .gitignore | 2 -- call-profiles/main.go | 9 --------- layer/utils/lambdacalls.go | 2 -- 3 files changed, 13 deletions(-) diff --git a/.gitignore b/.gitignore index 3d16dc0..7a0a5ad 100644 --- a/.gitignore +++ b/.gitignore @@ -3,5 +3,3 @@ env.json samconfig.toml */firestore-debug.log -scripts/local-dev.sh -scripts/deploy-local.sh diff --git a/call-profiles/main.go b/call-profiles/main.go index 8c80535..730c7ee 100644 --- a/call-profiles/main.go +++ b/call-profiles/main.go @@ -17,8 +17,6 @@ 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() payload := utils.ProfileLambdaCallPayload{ @@ -36,8 +34,6 @@ func handler(request events.APIGatewayProxyRequest) (events.APIGatewayProxyRespo ctx := context.Background() client, err := utils.InitializeFirestoreClient(ctx) - fmt.Println("Calling profiles - entry point") - if err != nil { return events.APIGatewayProxyResponse{}, err } @@ -52,12 +48,7 @@ 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 { diff --git a/layer/utils/lambdacalls.go b/layer/utils/lambdacalls.go index 0d0da93..d8d6ea7 100644 --- a/layer/utils/lambdacalls.go +++ b/layer/utils/lambdacalls.go @@ -24,7 +24,6 @@ func InvokeProfileLambda(payload ProfileLambdaCallPayload) error { } functionName := "CallProfileFunction" - fmt.Println("functionName", functionName) if functionName == "" { return fmt.Errorf("PROFILE_LAMBDA_FUNCTION_ARN is not set") } @@ -36,7 +35,6 @@ func InvokeProfileLambda(payload ProfileLambdaCallPayload) error { _, err = client.Invoke(input) if err != nil { - fmt.Println("error invoking lambda", err) return fmt.Errorf("error invoking lambda: %w", err) } From 3dd0e32de627d80a0d017e41c391bee5f902cc98 Mon Sep 17 00:00:00 2001 From: JC CODER Date: Sun, 6 Oct 2024 02:11:57 +0100 Subject: [PATCH 3/6] update lambda invoke to wrap request body properly and also get function name from env --- layer/utils/lambdacalls.go | 22 ++++++++++++++++++++-- template.yaml | 1 + 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/layer/utils/lambdacalls.go b/layer/utils/lambdacalls.go index d8d6ea7..548999b 100644 --- a/layer/utils/lambdacalls.go +++ b/layer/utils/lambdacalls.go @@ -3,17 +3,24 @@ 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) @@ -23,14 +30,25 @@ func InvokeProfileLambda(payload ProfileLambdaCallPayload) error { return fmt.Errorf("error marshalling payload: %w", err) } - functionName := "CallProfileFunction" + // 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("PROFILE_FUNCTION_LAMBDA_NAME") if functionName == "" { return fmt.Errorf("PROFILE_LAMBDA_FUNCTION_ARN is not set") } input := &lambda.InvokeInput{ FunctionName: aws.String(functionName), - Payload: payloadBytes, + Payload: wrapperBytes, } _, err = client.Invoke(input) diff --git a/template.yaml b/template.yaml index 8a14674..611a311 100644 --- a/template.yaml +++ b/template.yaml @@ -17,6 +17,7 @@ Globals: baseURL: YourBaseAPIURL discordBotURL: DiscordBotURL identityServicePrivateKey: YourIdentityServicePrivateKey + PROFILE_FUNCTION_LAMBDA_NAME: CallProfileFunction Resources: UtilitiesLayer: From 15a8b8fb817a74e827e7da0577d57abf82d81484 Mon Sep 17 00:00:00 2001 From: JC CODER Date: Sun, 6 Oct 2024 02:14:21 +0100 Subject: [PATCH 4/6] update lambda invoke to wrap request body properly and also get function name from env --- layer/utils/lambdacalls.go | 4 ++-- template.yaml | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/layer/utils/lambdacalls.go b/layer/utils/lambdacalls.go index 548999b..464b4e3 100644 --- a/layer/utils/lambdacalls.go +++ b/layer/utils/lambdacalls.go @@ -41,9 +41,9 @@ func InvokeProfileLambda(payload ProfileLambdaCallPayload) error { return fmt.Errorf("error marshalling wrapper: %w", err) } - functionName := os.Getenv("PROFILE_FUNCTION_LAMBDA_NAME") + functionName := os.Getenv("profileFunctionLambdaName") if functionName == "" { - return fmt.Errorf("PROFILE_LAMBDA_FUNCTION_ARN is not set") + return fmt.Errorf("profileFunctionLambdaName is not set") } input := &lambda.InvokeInput{ diff --git a/template.yaml b/template.yaml index 611a311..0c6ada6 100644 --- a/template.yaml +++ b/template.yaml @@ -17,7 +17,7 @@ Globals: baseURL: YourBaseAPIURL discordBotURL: DiscordBotURL identityServicePrivateKey: YourIdentityServicePrivateKey - PROFILE_FUNCTION_LAMBDA_NAME: CallProfileFunction + profileFunctionLambdaName: CallProfileFunction Resources: UtilitiesLayer: From 21935ec4be7e5d688ad10a55f2738276a2305594 Mon Sep 17 00:00:00 2001 From: JC CODER Date: Thu, 10 Oct 2024 00:43:06 +0100 Subject: [PATCH 5/6] update profile function name --- template.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/template.yaml b/template.yaml index 0c6ada6..d01d125 100644 --- a/template.yaml +++ b/template.yaml @@ -17,7 +17,7 @@ Globals: baseURL: YourBaseAPIURL discordBotURL: DiscordBotURL identityServicePrivateKey: YourIdentityServicePrivateKey - profileFunctionLambdaName: CallProfileFunction + profileFunctionLambdaName: identity-service-CallProfileFunction Resources: UtilitiesLayer: From e98dc11b9dfa6e0005bef228df95105374793e1c Mon Sep 17 00:00:00 2001 From: JC CODER Date: Mon, 14 Oct 2024 20:11:48 +0100 Subject: [PATCH 6/6] update profile function lambda name --- template.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/template.yaml b/template.yaml index d01d125..8a1b5ef 100644 --- a/template.yaml +++ b/template.yaml @@ -124,7 +124,7 @@ Resources: Metadata: BuildMethod: go1.x Properties: - FunctionName: CallProfileFunction + FunctionName: identity-service-CallProfileFunction CodeUri: call-profile/ Handler: bootstrap Runtime: provided.al2023