20

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

ARUNBALAN NV
  • 1,634
  • 4
  • 17
  • 39
  • 1
    please refer this URl http://docs.aws.amazon.com/lambda/latest/dg/walkthroughs.html & https://www.topcoder.com/blog/amazon-lambda-demo-tutorial/ [Demo] – Dipesh Shihora Aug 03 '15 at 05:24
  • 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 Aug 03 '15 at 07:02
  • 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 Aug 10 '15 at 15:52
  • it's simple. just add an output mapping template in Integration Response settings from API gateway. – ARUNBALAN NV Aug 10 '15 at 17:12
  • @ARUNBALANNV I was experimenting with the output mapping template, but I cannot get a grip on the right one. Could you provide me with some help? – domderen Aug 10 '15 at 21:06
  • 2
    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 Aug 11 '15 at 05:27
  • @ARUNBALANNV This worked, thank you very much! I would propose that you add this as an answer to the question. It is a tricky one, to pass that correctly. – domderen Aug 11 '15 at 08:20

3 Answers3

19

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.

Fergal
  • 5,213
  • 6
  • 35
  • 44
ARUNBALAN NV
  • 1,634
  • 4
  • 17
  • 39
  • 1
    Where are you actually putting that code? I'm in the API Gateway dashboard and poking around but not sure where you're doing all this – Scott Decker Jan 18 '16 at 21:33
  • 1
    Nm, found it. Inside your API, click on the resource (GET, POST, etc) in the left hand Resources pane, then click "Integration Response" in the workflow, then click the little drop down arrow next to the 200 response, then drop down "Mapping Templates" and add a content type ('text/html'). Then click the pencil next to "Output Passthrough" to create the mapping. – Scott Decker Jan 18 '16 at 21:47
  • 1
    Don't forget to click the blue button and Deploy API!! – Scott Decker Jan 19 '16 at 20:29
  • 1
    Another good answer as a followup: http://stackoverflow.com/questions/33614198/return-html-from-aws-api-gateway?lq=1 –  Apr 08 '16 at 06:05
  • A good tutorial here: https://kennbrodhagen.net/2016/01/31/how-to-return-html-from-aws-api-gateway-lambda/ – standac Dec 28 '16 at 15:01
6

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.

phoenix
  • 1,629
  • 20
  • 11
1
exports.handler = async lambdaEvent => {
    const content = `<h1>Hello world</h1>`;
    return {
        "statusCode": 200,
        "body": content,
        "headers": {
            'Content-Type': 'text/html',
        }
    };
}
Oded Breiner
  • 28,523
  • 10
  • 105
  • 71