5

I have made an API in AWS API Gateway which calls a Lambda function. This is the code which I have used to return the application/json response almost similar to a Python Lambda Blueprint:

def response(status_code, response_body=None):
    return {
        'statusCode': status_code,
        'body': json.dumps(response_body) if response_body else json.dumps({}),
        'headers': {
            'Content-Type': 'application/json',
        },
    }

Currently I have only one HTTP Status code 200. I am having a hard time making the model schema for this response.

How do I get the body out of this response and properly display it to the consumer?


EDIT: I needed to create my API with Lambda Proxy Integration because here I am returning the response from Lambda and not transforming it at all. Also, there is no need for Models Schemas here. For more info, read the accepted answer.

Note: To avoid No 'Access-Control-Allow-Origin' header is present on the requested resource. error. Simply add 'Access-Control-Allow-Origin': '*' in the headers along with Content-Type

Cheers!

1 Answer 1

1

Just to make sure we're on the same page with API Gateway terminology:

Model schemas are only needed to model the input/output of your API if you plan to generate SDKs for your API (currently supports Java, iOS, Android, Javascript)

Mapping templates are what you can use to transform input from the method request into the integration request and output from the integration response into the final method response.

Proxy resource types in API Gateway allow you to proxy/pass-through the method request into your integration and integration response through to the method response without needing to deal with mapping templates, if you don't need to do any transformations.

Unless you explicitly need to transform your Lambda output at the API Gateway layer, I would recommend you look into the proxy resource type, which along with the ANY method and greedy path variables should simply your API Gateway configuration for the simplest pass-through/proxy use case.

https://aws.amazon.com/blogs/aws/api-gateway-update-new-features-simplify-api-development/

Sign up to request clarification or add additional context in comments.

1 Comment

You are right, I need to use Lambda Proxy Integration for this response to work because I won't be transforming any of the response. I will send the response (including status code, body, headers) myself. All I needed to use is Lambda Proxy Integration without any Model schemas. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.