question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

DX: Make it easier to use API gateway base URL in environment variables

See original GitHub issue

This post http://www.goingserverless.com/blog/api-gateway-url outlines how to grab the base API gateway url from cloud-formation.

This is a pretty common url case and prevents folks from having to hardcode strings

It works but is god awful to write. No human should ever ever ever ever ever ever have to write:

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

Here is a working serverless.yml example:

service: the-service

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

provider:
  name: aws
  runtime: nodejs6.10
  environment:
    IT_WORKS: { "Fn::Join" : ["", [ "https://", { "Ref" : "ApiGatewayRestApi" }, ".execute-api.${self:custom.region}.amazonaws.com/${self:custom.stage}" ] ]  }

functions:
  myFunc:
    handler: handler.test
    events:
      - http:
          path: whatever
          method: post
          cors: true
// hander.js
module.exports.test = (event, context, callback) => {
  console.log('API URL IT_WORKS', process.env.IT_WORKS)
  // now I can call other urls of service without hardcoding stuff
}

Suggestion: Make a shorthand variable to reference this instead of the verbose cloudformation.

Something like: ${apiGatewayUrl} (or something)

Let’s make it easy to write and read! =)

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:20
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
mim-Armandcommented, Apr 11, 2019

AN old topic but thought I’d share this and bump it so we close it if it’s not needed anymore, I use this and it works fine:

baseUrl: ${cf:my-api-name-${self:provider.stage}.ServiceEndpoint}
1reaction
medikoocommented, Dec 15, 2020

I’m closing it for two reasons:

  1. AFAIK API Gateway API url (as generated by AWS) cannot be known until actual API is deployed - therefore trying to attach to it in properties which are required for a deployment of a stack, cannot work (at least not for first deployment)
  2. For cases where API was already deployed a solution posted in this comment https://github.com/serverless/serverless/issues/3986#issuecomment-481964975 by @mim-Armand feels as most appropriate way to address this use case.

Let me know if missed something

Read more comments on GitHub >

github_iconTop Results From Across the Web

Setting up stage variables for a REST API deployment
To use a stage variable to customize the HTTP integration endpoint, you must first configure a stage variable of a specified name (for...
Read more >
Reference Your API Gateway URL from within Your Code
If you've wanted to be able to use the URL of your service before you've deployed it then this is the video for...
Read more >
S3 Bucket Integration for API Gateway - Terraform Registry
This example demonstrates how to create an S3 Proxy using AWS API Gateway. It takes you through listing the buckets of the API...
Read more >
Connecting a web app to your PyTorch model using Amazon ...
Ideally, we could connect this URL directly to API Gateway. ... To do so, we should create an environment variable for the SageMaker...
Read more >
Edge Functions are now available in Supabase
Your project's URLs, API keys, and database connection strings are made available as environment variables within your Function to make this ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found