2
votes

I'm using the Serverless Framework for my Lambda functions on AWS. How can I send authorization headers? My current config:

Lambda function:

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

     let response = {
            statusCode: 302,
            headers: {
                "Authorization": userUtil.getToken(userId)
            },
            body: ""
        };
    callback(null, response);
    return;
}

serverless.yml:

createUser:
    handler: functions/user.createUser
    events:
      - http:
          path: users
          method: post
          cors: true
          integration: lambda

The above config sends the response object as body. The status is still 200.

I'm using Serverless v1.10

1

1 Answers

1
votes

If you want to set the status code, headers and body in your code, instead of adding them in your configuration, you need to use the Lambda-proxy setting. See more here.

Lambda-proxy is the default type. So you need to remove the integration: lambda in your serverless.yml. Also, remove the cors: true setting and modify the handler.js to add CORS headers.

Modify your Lambda function to:

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

    let response = {
        statusCode: 302,
        headers: {
            "Authorization": userUtil.getToken(userId),
            "Access-Control-Allow-Origin" : "*", // Required for CORS support to work
            "Access-Control-Allow-Credentials" : true // Required for cookies, authorization headers with HTTPS 
        },
        body: ""
    };

    callback(null, response);
    return;
}

Modify your serverless.yml file to:

createUser:
    handler: functions/user.createUser
    events:
      - http:
          path: users
          method: post