1

can you help me, guys? I have the problem that I am struggling with creating a serverless service that uses a headless chrome for generating PDFs.

My Problem:

If I invoke my function with sls invoke -f generate -v the response is a 502 Bad Gateway with:

{
    "errorMessage": "2018-02-05T16:00:35.487Z aefe06e6-0a8d-11e8-b463-614bcbb12dd5 Task timed out after 6.01 seconds"
}

So What I am doing wrong in setting up chromeless? Maybe you can help me fix it!

Regards!

My Files:

pdf.ts

import { Context, Callback } from 'aws-lambda';
import Chromeless from 'chromeless';
import { success, failure } from './libs/response.lib';

import { IPdfOptions } from './interfaces/pdf.interface';

export async function main(event: any, context: Context, callback: Callback) {

    const url = 'https://github.com/graphcool/chromeless';
    const pdfOptions: IPdfOptions = {
        landscape: false,
        displayHeaderFooter: false,
        printBackground: false,
        scale: 1,
        paperWidth: 8.27, // 210mm DIN A4
        paperHeight: 11.69, // 297mm
        marginTop: 0,
        marginBottom: 0,
        marginLeft: 0,
        marginRight: 0  
    };

    try {
        const chromeless = new Chromeless({
            remote: {
                endpointUrl: 'https://nefq434bvf.execute-api.eu-west-1.amazonaws.com/dev/generate',
                apiKey: 'APIKEYFfXkU8uDgiLRl2bdfIhxAPIKEYEIMde00'
            }
        });

        const pdf = await chromeless
            .goto(url);
            .pdf(pdfOptions);

        callback(null, success(pdf));

        await chromeless.end();

    } catch(e) {
        callback(null, failure({
            status: false,
            error: `Couldn\'t create PDF`,
            debug: { stackTrace: e}
        }));
    }
}

serverless.yml

service: pdf-service

custom:
    bucket: pdf-service-upload 
    stage: dev

provider:
    name: aws
    Runtime: nodejs6.10
    stage: ${self:custom.stage}
    region: eu-west-1
    apiKeys:
        - ${self:custom.stage}-chromeless-session-key

functions:
    generate:
        handler: api/pdf.main
            events:
                - http:
                path: generate
                method: get
                cors: true

plugins:
    - serverless-plugin-typescript

Response with local invoke

So when I try sls invoke local -f generate -v I get the following response:

{ Error: Response code 502 (Bad Gateway)
    at stream.catch.then.data (/Users/lukasholzer/Sites/typeflow/pdf-service-aws/node_modules/got/index.js:341:13)
    at <anonymous>
    at process._tickDomainCallback (internal/process/next_tick.js:228:7)
name: 'HTTPError',
host: 'neqq4t4bvf.execute-api.eu-west-1.amazonaws.com',
hostname: 'neqq4t4bvf.execute-api.eu-west-1.amazonaws.com',
method: 'GET',
path: '/dev/generate',
protocol: 'https:',
url: 'https://neqq4t4bvf.execute-api.eu-west-1.amazonaws.com/dev/generate',
statusCode: 502,
statusMessage: 'Bad Gateway',
headers: 
{ 'content-type': 'application/json',
    'content-length': '36',
    connection: 'close',
    date: 'Mon, 05 Feb 2018 16:20:51 GMT',
    'x-amzn-requestid': '84c632b0-0a90-11e8-8e16-fdf938845b71',
    'x-cache': 'Error from cloudfront',
    via: '1.1 f49041de8ffb6d4017d05ed1f9106b42.cloudfront.net (CloudFront)',
    'x-amz-cf-id': 'Duv92Vy8R7hvjU0QxpcfLxYTaOsEACpcfIpog9ZdgUfNJHRKdWwODg==' } }
{
    "statusCode": 500,
    "headers": {
        "Access-Control-Allow-Origin": "*",
        "Access-Control-Allow-Credentials": true
    },
    "body": "{\"status\":false,\"error\":\"Couldn't create PDF\",\"debug\":{\"stackTrace\":{}}}"
}
  • Any way to get more insight into what the error is that gets thrown? Seems like there should be _something_ in `stackTrace` in `\"debug\":{\"stackTrace\":{}}` – Marco Lüthy Feb 09 '18 at 15:27

0 Answers0