使用AWS Lambda和Serverless设置Chromeless

时间:2018-02-05 16:45:42

标签: amazon-web-services lambda google-chrome-headless serverless chromeless

伙计,你能帮助我吗?我遇到的问题是我正在努力创建一个使用无头chrome来生成PDF的无服务器服务。

我的问题:

如果我使用sls invoke -f generate -v 调用我的功能,则响应为502 Bad Gateway with:

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

那么我在设置chromeless时做错了什么? 也许你可以帮我解决它!

问候!

我的档案:

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

使用本地调用的响应

所以当我尝试sls invoke local -f generate -v时 我收到以下回复:

{ 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\":{}}}"
}

0 个答案:

没有答案