Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return HTML code from Lambda function in NodeJS?

I have the following Lambda function. I need to return some custom html when the function is called.

I tried:

    exports.handler = async (event, context, callback) => {   
        const response = {
            statusCode: 200,
            headers: {
                'Content-Type': 'text/html',
            },
            body: String("Hi there !"),
        };
        return response;
    }

But when invoking the function, I'm getting the following error : The Lambda function returned an invalid entry in the headers object: Each header entry in the headers object must be an array. We can't connect to the server for this app or website at this time.

I took the code from AWS blueprint :

enter image description here

Original code from AWS :

enter image description here

What am I doing wrong?

like image 264
Thomas Carlton Avatar asked Jul 02 '26 09:07

Thomas Carlton


1 Answers

You appear to have used the regular AWS Lambda blueprint. Edge Lambda functions are different e.g. the status code is returned in status, not in statusCode.

Based on the documented example:

exports.handler = (event, context, callback) => {
    const response = {
        status: '200',
        statusDescription: 'OK',
        headers: {
            'cache-control': [{
                key: 'Cache-Control',
                value: 'max-age=100'
            }],
            'content-type': [{
                key: 'Content-Type',
                value: 'text/html'
            }]
        },
        body: "some HTML content here",
    };
    callback(null, response);
};
like image 199
jarmod Avatar answered Jul 04 '26 12:07

jarmod



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!