Read path for http event in serverless.yml in Lambda function

I need to be able to read the path set for a specific function within serverless.yml within a Lambda. This is necessary because I need to build a redirect URL for a web service that ends up pointing at the endpoint for another lambda function, but I’d rather not hardcode the endpoint within my Lambda function, but rather refer to the other functions path potentially via its function name. This means that the path value within functions for http events essentially acts as routing configuration.

Anyone have any idea if its possible off the bat?

Not sure if I follow exactly what you’re trying to achieve. Are you able to provide a bit more clarity or an example use case?

In serverless.yml, you define a function with an event of http and a path. What I would like to do is write a function that could call another function via that path. So instrad of having to store the path to a function in an environment or stage variable I someone reference the function I want to call and get the path set for the http event. That way, all functions with an http event are routed to other functions via serverless.yml

Make sense?

Sorry not really, I guess I’m just trying to understand the actual use case. What exactly is it you want, say a user or someone calling one endpoint to do? Can you describe a user story? What will trigger one endpoint to need to call another without it being initiated on the client side?

Lambda functions are pretty self contained, the serverless.yml really stops at your deployment unless you want to inject stuff into the environment variables of the function. Is there any reason you would be calling the other functions through the API gateway, rather than just invoking them inside the function you want to call them with?

If you just invoke the function directly inside the calling function, you’ll cut out a lot of API gateway overhead. Also with serverless using a naming convention approach, you won’t need to know the path, as you can just use the function name.

The other option would be to include your serverless.yml file in your lambda function so you could read it into an object inside the function at runtime and reference whatever config items you need? Feels a bit hacky though

@garethmcc I’m also pretty confused by your messages. From the first message it sound like you need to redirect from one URL to another. Is that correct?

If your first Lambda is triggered by the API Gateway and you’re using Lambda Proxy Integration then you can work out the base URL using:

const baseUrl = `${event.headers["X-Forwarded-Proto"]}://${event.headers["hostname"]}/${event.requestContext["stage"]}`;

You can then construct the URL for any other resource by appending the resource path to the base URL.

I am not certain I understand the OP but I think the problem is similar enough to mine that I will add my case here.

I have a serverless application with two functions.
The first function is driven by an S3 event. The second function is driven by a proxy HTTP call via API gateway.

The two functions are used together as part of an async flow.

  • Function 1 is trigger by s3, picks up the S3 Object and sends it to a 3rd party web service for processing.
  • The web service implements a callback mechanism that will hit a URL of my choosing when processing is complete.
  • The callback URL can be set at the time of making the request
  • I do not know what the URL of function 2 is when constructing my request to the 3rd party as I am without the benefit of an HTTP request as @buggy described above.

Currently, I have the hostname semi-statically defined in the code of function 1. Something to the effect of : “https://XXXXXXXXX.execute-api.${region}${stage}/${path}”. The missing piece for me is the hostname

I hope this is clear enough to find some help.


@gavinhogan I’d look at passing the callback URL to the Lambda triggered by S3 using an environment variable set in your serverless.yml. You should be able to get the missing part using a ref. See

Thanks @buggy

You pointed me to the right path.
This what I ended up with.

            SERVICE_ENDPOINT: ${cf:${self:service}-${self:provider.stage}.ServiceEndpoint} 

Hope this helps someone else.

1 Like