2
votes

I am just getting started with AWS and have made a basic calculator in AWS Lambda as below:

import json

def lambda_handler(event, context):
op1 = event.get('op1',0)
op2 = event.get('op2',0)
operator = event.get('op', None)
if op1 == 0 or op2 == 0 or operator == None:
    result = 'Please enter valid input data'
    ret_code = 219
else:
    if operator in {'add', 'plus', '+'}:
        result = op1 + op2
        ret_code = 278
    elif operator in {'subtract', 'minus', '-'}:
        result = op1 - op2
        ret_code = 278
    elif operator in {'multiply', '*'}:
        result = op1 * op2
        ret_code = 278
    elif operator in {'divide', '/'}:
        result = op1 / op2
        ret_code = 278
    else:
        result = 'Please enter valid operator'
        ret_code = 219
return {
    'isBase64Encoded': False,
    'statusCode': ret_code,
    'headers': {'Content-Type': 'application/json'},
    'body': json.dumps(result)
}

This function works fine when I test Lambda using the following

{
"op1": 7,
"op2": 9,
"op": "divide"
}

I then made an API in Amazon API Gateway and configured it to use Lambda Proxy integration, as shown in API Setup.

However, now when I try and trigger the AWS Lambda through a PUT or POST through the same input data as before, I get a 219 error which means that the data isn't being passed into the Lambda event but instead Lambda is using the default zeros, giving the error. The response that I get from API Gateway is as follows:

Execution log for request test-request
Tue Nov 21 12:19:34 UTC 2017 : Starting execution for request: test-invoke-request
Tue Nov 21 12:19:34 UTC 2017 : HTTP Method: PUT, Resource Path: /
Tue Nov 21 12:19:34 UTC 2017 : Method request path: {}
Tue Nov 21 12:19:34 UTC 2017 : Method request query string: {}
Tue Nov 21 12:19:34 UTC 2017 : Method request headers: {}
Tue Nov 21 12:19:34 UTC 2017 : Method request body before transformations: {
"op1": 7,
"op2": 9,
"op": "divide"
}
Tue Nov 21 12:19:34 UTC 2017 : Endpoint request URI: https://lambda.eu-west-2.amazonaws.com/2015-03-31/functions/arn:aws:lambda:eu-west-2:*:function:newCalc/invocations
Tue Nov 21 12:19:34 UTC 2017 : Endpoint request headers: {x-amzn-lambda-integration-tag=test-request, Authorization=*, X-Amz-Date=20171121T121934Z, x-amzn-apigateway-api-id=*, X-Amz-Source-Arn=arn:aws:execute-api:eu-west-2:*:wrsio8dvp3/null/PUT/{proxy+}, Accept=application/json, User-Agent=AmazonAPIGateway_*, X-Amz-Security-Token=*
Tue Nov 21 12:19:34 UTC 2017 : Endpoint request body after transformations: {"resource":"/{proxy+}","path":"/","httpMethod":"PUT","headers":null,"queryStringParameters":null,"pathParameters":null,"stageVariables":null,"requestContext":{"path":"/{proxy+}","accountId":"*","resourceId":"fsamcn","stage":"test-invoke-stage","requestId":"test-invoke-request","identity":{"cognitoIdentityPoolId":null,"accountId":"*","cognitoIdentityId":null,"caller":"*","apiKey":"test-invoke-api-key","sourceIp":"test-invoke-source-ip","accessKey":"*","cognitoAuthenticationType":null,"cognitoAuthenticationProvider":null,"userArn":"arn:aws:iam::*:user/*","userAgent":"Apache-HttpClient/4.5.x (Java/1.8.0_144)","user":"*"},"resourcePath":"/{proxy+}","httpMethod":"PUT","apiId":"wrsio8dvp3"},"body":"{\n  \"op1\": 7,\n  \"op2\": 9,\n  \"op\": \"divide\"\n}","isBase64Encoded":false}
Tue Nov 21 12:19:34 UTC 2017 : Sending request to https://lambda.eu-west-2.amazonaws.com/2015-03-31/functions/arn:aws:lambda:eu-west-2:*:function:newCalc/invocations
Tue Nov 21 12:19:34 UTC 2017 : Received response. Integration latency: 287 ms
Tue Nov 21 12:19:34 UTC 2017 : Endpoint response body before transformations: {"isBase64Encoded": false, "statusCode": 219, "headers": {"Content-Type": "application/json"}, "body": "\"Please enter valid input data\""}
Tue Nov 21 12:19:34 UTC 2017 : Endpoint response headers: {x-amzn-Remapped-Content-Length=0, Connection=keep-alive, x-amzn-RequestId=3befcbd6-ceb6-11e7-a4dd-c3db02aba4a2, Content-Length=139, Date=Tue, 21 Nov 2017 12:19:34 GMT, X-Amzn-Trace-Id=root=1-5a1419d6-106413cfbf4bf985d840894f;sampled=0, Content-Type=application/json}
Tue Nov 21 12:19:34 UTC 2017 : Method response body after transformations: "Please enter valid input data"
Tue Nov 21 12:19:34 UTC 2017 : Method response headers: {X-Amzn-Trace-Id=sampled=0;root=1-5a1419d6-106413cfbf4bf985d840894f, Content-Type=application/json}
Tue Nov 21 12:19:34 UTC 2017 : Successfully completed execution
Tue Nov 21 12:19:34 UTC 2017 : Method completed with status: 219

So, would you please be able to help me figure out what I'm doing wrong - and indeed how to properly format my request body to be able to trigger the Lambda function.

1

1 Answers

1
votes

The issue you are facing comes from the proxy integration of the api gateway. If you check this, the input structure given to your lambda is no longer the plain payload given to you PUT operation.

Your event parameter actually consists of the following structure (in your log the line Endpoint request body after transformations)

{
    "resource": "/{proxy+}",
    "path": "/",
    "httpMethod": "PUT",
    "headers": null,
    "queryStringParameters": null,
    "pathParameters": null,
    ....
    "body": "{\n  \"op1\": 7,\n  \"op2\": 9,\n  \"op\": \"divide\"\n}",
    "isBase64Encoded": false
}

So to access your raw event, you have to decode the event.body element.