How do I get the url for a function in my serverless.yml file

So at first glance this is an odd use case, but hopefully the details make it understandable, or else provide enough background for more seasoned serverless framework veterans to provide alternate possibilities…
I have a full CI/CD pipeline building script that is generalized enough that I can use it for each serverless microservice. I use my own Docker image in codeBuild, which allows me to inject the stage as an env variable and utilize serverless within the pipeline. a side benefit is local development is done in the same exact docker image. The issue is that I also want to run some e2e level tests that invoke the microservice via http, and I want to do that in another lambda function. So integrationTestLambdaFunction needs to invoke targetMicroserviceLamdaToTest through the API gateway. Using serverless-offline I can test locally as the url is just http://localhost:3000/functionName. If I want to test it in aws for end to end and performance testing, I need the url, but I am new to this rodeo and not sure how to go about getting it.

I ran into the same thing when building this:

My workaround was to do the following:

serverless deploy | tee deploy.out

That way, at deployment time, the URLs you need are saved in deploy.out so that you can parse for them later.

sls info | grep etc etc was all I came up with so far, which I guess is the same thing you suggested. The issue is that at deploy time I need the tester function to know the url for the microserfice function. Unless there is some way to inject it after creation… Can ENV vars be set after the fact somehow? I can grep the URLs easily enough as part of the build process, and then leave them as an artifact for later stages in the pipeline… hmmmm this might work…

@jdchmiel Are both Lambda functions part of the same serverless project? You could pass the URL into your Lambda as an environment variable.

custom:
  stage: ${opt:stage, self:provider.stage}
  region: ${opt:region, self:provider.region}

environment:
  URL: { "Fn::Join" : ["", [" https://", { "Ref" : "ApiGatewayRestApi" }, ".execute-api.${self:custom.region}.amazonaws.com/${self:custom.stage}/path/to/resource" ] ]  }

If you have a lot of URL’s you might want to consider passing a base URL without the /path/to/resource as you can easily add that later.

6 Likes

Perfect! This is exactly what I needed! I did not know we could use cloudformation template syntax outside of the resources stanza of the serverless file! I will give this a shot.

From memory there are only three places you can use this in your serverless.yml. I seem to remember they are - resources, IAM statements and environment variables.