Apigateway + VTL

0

hello, I have an ec2 with apache and two visible headers, above is a NLB with the target group to port 80 and then I have an apigateway configured with VPC Link pointing to the NLB. What I want is how do I configure the VTL so that when I do a GET query to the API it returns only the two headers created from the apache.

1 Answer
0

From your query, I understand that you want to access the headers returned from the backend integration (NLB having EC2 in the Target group) using a mapping template(VTL). Aditionally, the EC2 instance is having an Apache server installed on it. There are two ways to integrate the backend with API Gateway: Proxy Integration, Non-Proxy Integration.

Case1 : Proxy Integration

If, the API Gateway (REST) is using a Proxy Integration to integrate with NLB, the headers cannot be modified using mapping templates (VTL). This is because when Proxy Integration is used, the output returned from the backend cannot be modified in the integration response (using the mapping template) and the response is returned as-is from the backend.

Case2 : Non-Proxy Integration

Using, Non-Proxy Integration with NLB, there are two ways in which Headers can be returned from the Backend.

If the headers from the backend are present in Response Headers field when the API Gateway is tested out from the console, it is not possible to access headers/query strings/status codes etc. returned from the backend in the Response Body Mapping templates using VTL, and you can only access the Response Body from the backend in the templates.

The only workaround currently is to send the required headers/status codes to the response body and access it in the mapping template after that or use the header mapping section in integration response. Further, you can refer this document for using header mapping section in Integration response[1].

That being said, there is an existing feature request for accessing headers/query strings/status codes present in the Response Headers field. I went ahead and added your voice to this feature request by doing a +1 on the feature request. Meanwhile, I request you to keep an eye on our What's New[2] and Blog pages[3] for any new feature announcements.


References:

[1] https://docs.aws.amazon.com/apigateway/latest/developerguide/request-response-data-mappings.html#mapping-response-parameters
[2] https://aws.amazon.com/new/
[3] https://aws.amazon.com/blogs/aws/

AWS
answered 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions