19
votes

How can i get the aws lambda response as the HTML page. Please provide the step wise procedure to solve this.

2
i just inserted the HTML code as string and it returns correct output but there is a double quotes shows outside html code - ARUNBALAN NV
I have the same problem, I try to create a Lambda function which returns HTML content, and later call it through AWS API Gateway to return the website markup. I am also struggling with the double quotes around the HTML content which breaks stuff. Did you manage to solve your issue by any chance? - domderen
it's simple. just add an output mapping template in Integration Response settings from API gateway. - ARUNBALAN NV
first you just store your HTML markup in a variable in lambda function. Then Return it after the execution of Lambda. Here is an example of mapping template #set($inputRoot = $input.path('$')) $inputRoot.variableHTML .here variableHTML contains the HTML markup.after that you needed to create an Response model for http Status, go through Method Response. here add Response model Content-Type as text/html. Then you get the HTML page without quotes. - ARUNBALAN NV

2 Answers

19
votes

Store the HTML markup in a variable and return it to avoid the text being wrapped in quotes. First store your HTML markup in a variable in the lambda function then return it. For example in Node.js:

context.succeed({ variableHTML: myContentHtml })

Here is an example of the mapping template:

#set($inputRoot = $input.path('$')) $inputRoot.variableHTML .

Here variableHTML contains the HTML markup passed from the lambda function. After that you needed to create an Response model for HTTP Status, which is accessible through Method Response. Here set the Response model Content-Type as text/html. Then you'll get the HTML page without quotes and the browser recognizes it as HTML.

4
votes

You don't need Lambda to print out HTML.

Adding the HTML code:

  • go to your GET method -> Integration Response -> Body Mapping Templates

  • delete application/json (by default)

  • add text/html mapping

  • in the empty field to the right, just paste your HTML (delete anything else)

You will also need to update the content type in the Method Response:

  • expand 200 response

  • under Response Body for 200, delete application/json and add text/html with an empty model

Then just deploy your API and you're done.