API GateWay and Lambda


flowchart LR; subgraph protected A[API GateWay]-->B[Lambda] end C[API Key]-->protected
  1. Create Lambda. Test by,
  2. 
    ## event.json
    {
        "queryStringParameters": {
          "transactionId": "value1",
          "type": "value2",
          "amount": "value3"
        }
      }  
    ##
    aws-vault exec leo.leung -- aws lambda invoke --region=eu-west-2 --function-name=func2 response.json --payload file://event.json   
    ## it should respond with response.json
    {"statusCode": 200, 
        "headers": {"Content-Type": "application/json"}, 
        "body": "{\"transactionId\": 
            \"value1\", \"type\": \"value2\", 
            \"amount\": \"value3\", 
            \"message\": \"Hello from Lambda land\"}"}% 
    ##
            
  3. Create API Gateway.
  4. Configure Gateway, like add resource, add method, deploy.
  5. Test by Postman, or browser. https://xxxxxx.execute-api.eu-west-2.amazonaws.com/v1/predict?transactionId=1234&type=buy&amount=300
  6. Configure Gateway, method execution → api key required → api keys → Usage Plans.
  7. Test by Postman by including header x-api-key.

References


  1. API Authentication via API Keys | AWS API Gateway
  2. AWS API Gateway to Lambda Tutorial in Python | Build a REST API
  3. Invoke Lambda Functions with AWS CLI - Complete Guide