Occasionally you need to know the API Gateway URL for your services inside your Lambda. This happened to me recently when one of my Lambda’s needed to provide a callback URL to a third party service that it was using.

From what I’ve read on the Serverless forum a lot of people are deploying their API’s using Serverless, then copying the URL and redeploying again with that URL hard coded into their serverless.yml as an environment variable.

If you’re one of those people then STOP. This is both the hard way and it has two very negative side effects.

  1. It’s difficult to deploy your application to other stages, and
  2. It introduces a risk that you’ll break something by depolying to one stage with the API Gateway URL for the different stage.

So how do I get around this?

Instead of hard coding the API GW URL you can reconstruct it using a combination of Serverless variables and CloudFormation. Here’s how I set the API gateway URL so it’s passed to my Lambda as the GW_URL environment variable.

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

Inside my code I can now reference the current API GW URL using.


If you’re using the serverless-offline plugin (or similar) then you will probably want to combine this with per stage environment variables. You’ll need to use per stage environment variables because this approach assumes you’re deploying with CloudFormation but running it locally means no CloudFormation to resolve the URL. Instead you’ll need to hard code a URL for local development.

The “cheat sheet” version is below but I strongly recommend you read the post as the full version explains it in more depth and covers topics like having separate files for each environment.

    GW_URL: ${self:provider.${self:custom.stage}.GW_URL}

  stage: "${opt:stage, self:provider.stage}"
    GW_URL: { "Fn::Join" : ["", [ "https://", { "Ref" : "ApiGatewayRestApi" }, ".execute-api.${self:custom.region}.amazonaws.com/${self:custom.stage}" ] ]  }
    GW_URL: "http://localhost:3000/"