Search code examples
aws-lambdaaws-samaws-sam-clisam

AWS SAM - Different Response from Local Invoke x Api Gateway | Python


I am not understanding why the event["body"] results are different when testing the lambda locally versus directly to the Aip Getaway... below I am posting the responses:

Lambda:

from pysafebrowsing import SafeBrowsing
import json


def lambda_handler(event, context):
    domain = event["queryStringParameters"]["domain"]
    data = SafeBrowsingScan().scanUrl(domain)
    return {
        "statusCode": 200,
        "body": json.dumps([data)),
    }


class SafeBrowsingScan:
    def scanUrl(self, domain):

        api_key = "<protected>"
        urls = domain
        try:
            s = SafeBrowsing(api_key)
            r = s.lookup_urls(urls)
        except:
            print("An exception occurred")

        return r
  1. Locally, using the command: sam local invoke -e events/event.json produces the expected response:
{"statusCode": 200, "body": "[\"{'lifeenhancingarts.com': {'malicious': False}}\"]"}
  1. Lambda/Api Gateway response (incorrect):
["{'l': {'malicious': False}, 'i': {'malicious': False}, 'f': {'malicious': False}, 'e': {'malicious': False}, 'n': {'malicious': False}, 'h': {'malicious': False}, 'a': {'malicious': False}, 'c': {'malicious': False}, 'g': {'malicious': False}, 'r': {'malicious': False}, 't': {'malicious': False}, 's': {'malicious': False}, 'o': {'malicious': False}, 'm': {'malicious': False}}"]

Observing the latter response, with the api gateway the method GET is invoked for each letter, rather than consider the full string...

I have no idea where the problem is because observing the logs, i get this:

Wed Mar 08 17:26:44 UTC 2023 : Received response. Status: 200, Integration latency: 161 ms
Wed Mar 08 17:26:44 UTC 2023 : Endpoint response headers: {Date=Wed, 08 Mar 2023 17:26:44 GMT, Content-Type=application/json, Content-Length=415, Connection=keep-alive, x-amzn-RequestId=b095a47a-554e-4bc1-a4a9-501fc9142412, x-amzn-Remapped-Content-Length=0, X-Amz-Executed-Version=$LATEST, X-Amzn-Trace-Id=root=1-6408c554-e17d0d253490ef2e46d367b0;sampled=0}
Wed Mar 08 17:26:44 UTC 2023 : Endpoint response body before transformations: {"statusCode": 200, "body": "[\"{'l': {'malicious': False}, 'i': {'malicious': False}, 'f': {'malicious': False}, 'e': {'malicious': False}, 'n': {'malicious': False}, 'h': {'malicious': False}, 'a': {'malicious': False}, 'c': {'malicious': False}, 'g': {'malicious': False}, 'r': {'malicious': False}, 't': {'malicious': False}, 's': {'malicious': False}, 'o': {'malicious': False}, 'm': {'malicious': False}}\"]"}
Wed Mar 08 17:26:44 UTC 2023 : Method response body after transformations: ["{'l': {'malicious': False}, 'i': {'malicious': False}, 'f': {'malicious': False}, 'e': {'malicious': False}, 'n': {'malicious': False}, 'h': {'malicious': False}, 'a': {'malicious': False}, 'c': {'malicious': False}, 'g': {'malicious': False}, 'r': {'malicious': False}, 't': {'malicious': False}, 's': {'malicious': False}, 'o': {'malicious': False}, 'm': {'malicious': False}}"]
Wed Mar 08 17:26:44 UTC 2023 : Method response headers: {X-Amzn-Trace-Id=Root=1-sssssss-saaaaaa;Sampled=0}
Wed Mar 08 17:26:44 UTC 2023 : Successfully completed execution
Wed Mar 08 17:26:44 UTC 2023 : Method completed with status: 200

SAM Template:

Resources:
  GoogleSafeBrowsingFunction:
    Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
    Properties:
      PackageType: Image
      Architectures:
        - x86_64
      Events:
        GoogleSafeBrowsing:
          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: '/api/gsb'
            Method: get
          RequestParameters:
            - method.request.querystring.domain:
                Required: true
                Caching: false
    Metadata:
      Dockerfile: Dockerfile
      DockerContext: ./googlesafebrowsing
      DockerTag: python3.9-v1

I am using Lambda Proxy integration...

Does anyone experienced this issue and could help me? Thanks


Solution

  • I managed to find the issue... There was an error when assigning the value of the event to the variable domain... this fixed the issue:

    domain = [event["queryStringParameters"]["domain"]]