0
votes

I have a problem with correcltly returning the response of a Lambda function that I deploy using the Serverless framework:

module.exports.hello = async (event, context, callback) => {

  const content = fs.readFileSync('./cn23_template.html', 'utf-8')

  const Vue = require('vue')
  const app = new Vue({
    template: content,
    data: function () {
      return event
    }
  })

  const renderer = require('vue-server-renderer').createRenderer()
  const html = await renderer.renderToString(app)

  const browser = await chromium.puppeteer.launch({
    // Required
    executablePath: await chromium.executablePath,

    // Optional
    args: chromium.args,
    defaultViewport: chromium.defaultViewport,
    headless: chromium.headless || true
  });

  const page = await browser.newPage();
  await page.setContent(html);
  let pdf = await page.pdf({ pageRanges: '1', format: 'a4', printBackground: true });

  await browser.close();

  return {
    statusCode: 200,
    headers: {
      'Content-Type': 'application/pdf',
      'Content-Length': pdf.length
    },
    body: pdf ? pdf.toString('base64') : null,
    isBase64Encoded: true
  }
}

My serverless.yml file:

functions:
  hello:
    handler: handler.hello
    events:
      - http:
          path: hello
          method: post
          integration: lambda
          response:
            headers:
              Content-Type: "'Test'"
              Cache-Control: "'max-age=120'"

The problem is that what I return from the function is not correctly mapped to the response. The response doesn't include the statusCode and headers, it just uses the whole returned object as body of the response.

Besides this, the headers as configured in the .yml are also not being used.

It seems like a very silly mistake, but I'm just doing exactly what is inside the Serverless API gateway docs.

So my question is: How do I properly configure the response properties so that the HTTP request gives the correct response using the Serverless framework?

1
I believe you need to have integration configuration as lambda-proxy, not lambda. - LostJon

1 Answers

0
votes

You need to configure your lambda function to be invoked as a lambda-proxy event.

See Serverless Example Lambda Proxy