Best practice for calling another function in the same project

I’m trying to organize my project where two functions (render-test and render-final) will call a common function (query-data).

What I’ve currently done, which seems to work okay, is:

const axios = require('axios');

exports.handler = async (event) => {
    try {
        const p = event.rawUrl.replace('render-test', 'query-data');
        const result = await axios.get(p);
        return {
            statusCode: 200,
            body: "Made It!",
    } catch(err) {
        return {
            statusCode: 500,
            body: err.message

I’m curious if this is the best way to go about the call and if it’s possible to use a relative reference instead of the full URL.

Hi @jhsu,

You can write the common code in a separate file and import that in your Functions. For example,

// ./functions/utils/data.js
function dataClient(bodyContent, status) {
  return {
    body: JSON.stringify(bodyContent),
    statusCode: status
export {
// ./functions/foo.js
import {dataClient} from './utils/data'
export async function handler() {
  return dataClient({
    message: `Something`
  }, 200)

This way, you don’t have to declare a separate function.

But if you have to declare a separate function for whatever reason, you’ll always have to use the absolute URL. Browsers are able to parse relative URLs, AWS Lambda is not.


Thanks @hrishikesh this makes sense to import as a separate file, especially if I don’t want to expose that common code as a function in the first place.

1 Like

Thanks for coming back and letting us know. Happy building!