0

I'm using a simple api call that needs Digest Auth:

const { default: AxiosDigestAuth } = require('@mhoc/axios-digest-auth');

exports.handler = async () => {
    
    const digestAuth = new AxiosDigestAuth({
      username: user,
      password: pass,
    });

    await digestAuth.request({
      headers: {
        'Content-Type': 'application/json',
        Connection: 'keep-alive',
        Accept: 'application/json',
      },
      method: 'POST',
      url: 'https://sms.voxbone.com:4443/sms/v1/',
      data,
    });
};

I thought the issue is with the digest library I'm using but after running this piece of code locally it worked like a charm, though when I run it in my AWS lambda function I end up getting a 401 Unauthorized

Is there a chance AWS is manipulating my headers? Or maybe an issue with the port used? (4443)?

I can't think of any other reasons to why this would work locally but not on an AWS Lambda

Ali_Nass
  • 838
  • 1
  • 9
  • 27

1 Answers1

0

I kept having this issue for a long time on a lambda but not locally (even if I invoke the lambda locally it was working just not on the cloud)

What ended up working for me is to configure my axios instance in a different way!

  const httpsAgent = new https.Agent({ keepAlive: true });
  const httpAgent = new https.Agent({ keepAlive: true });
  const axiosInstance = axios.create({
    httpsAgent,
    httpAgent,
  });

This way we can ensure that the connection will stay alive.

To understand why this configuration is a good solution (and why it helps it work on a lambda) we need to understand how digest authentication work, here is a good explanation.

As you can see we need to have some kind of an http handshake (call -> fail -> receive info to call again -> call -> works!)

Ali_Nass
  • 838
  • 1
  • 9
  • 27