Multiple API Gateway instances, one lambda function











up vote
1
down vote

favorite












I am trying to build a system where multiple APIs gateway instances should execute the same lambda function.



My problem is that I would like to change only the lambda configuration in function of the API gateway used.



Let's take the name of a database as an example that should change if the lambda is being triggered from one API or the other.



Example:



API Gateways:
https://my-first-api-gateway.execute-api.eu-west-1.amazonaws.com
https://my-second-api-gateway.execute-api.eu-west-1.amazonaws.com


I then have one lambda function being called by the two APIs: say_hello.



This function has to retrieve a quote from a database. If the function has been called from my-first-api-gateway the lambda function has to use my_first_database and if the function has been called from my-second-api-gateway, it has to use my_second_database.



The only solution I came with is to deploy as many lambda functions as I have API Gateways. And then use environment variables to store my database name.



I don't like my solution because when I update a single line of code I'll have to redeploy all of my lambda functions.. (if I have 300 different databases to use, that would mean to update 300 lambda functions at once..)



Thank you for your ideas on this subject!










share|improve this question


























    up vote
    1
    down vote

    favorite












    I am trying to build a system where multiple APIs gateway instances should execute the same lambda function.



    My problem is that I would like to change only the lambda configuration in function of the API gateway used.



    Let's take the name of a database as an example that should change if the lambda is being triggered from one API or the other.



    Example:



    API Gateways:
    https://my-first-api-gateway.execute-api.eu-west-1.amazonaws.com
    https://my-second-api-gateway.execute-api.eu-west-1.amazonaws.com


    I then have one lambda function being called by the two APIs: say_hello.



    This function has to retrieve a quote from a database. If the function has been called from my-first-api-gateway the lambda function has to use my_first_database and if the function has been called from my-second-api-gateway, it has to use my_second_database.



    The only solution I came with is to deploy as many lambda functions as I have API Gateways. And then use environment variables to store my database name.



    I don't like my solution because when I update a single line of code I'll have to redeploy all of my lambda functions.. (if I have 300 different databases to use, that would mean to update 300 lambda functions at once..)



    Thank you for your ideas on this subject!










    share|improve this question
























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I am trying to build a system where multiple APIs gateway instances should execute the same lambda function.



      My problem is that I would like to change only the lambda configuration in function of the API gateway used.



      Let's take the name of a database as an example that should change if the lambda is being triggered from one API or the other.



      Example:



      API Gateways:
      https://my-first-api-gateway.execute-api.eu-west-1.amazonaws.com
      https://my-second-api-gateway.execute-api.eu-west-1.amazonaws.com


      I then have one lambda function being called by the two APIs: say_hello.



      This function has to retrieve a quote from a database. If the function has been called from my-first-api-gateway the lambda function has to use my_first_database and if the function has been called from my-second-api-gateway, it has to use my_second_database.



      The only solution I came with is to deploy as many lambda functions as I have API Gateways. And then use environment variables to store my database name.



      I don't like my solution because when I update a single line of code I'll have to redeploy all of my lambda functions.. (if I have 300 different databases to use, that would mean to update 300 lambda functions at once..)



      Thank you for your ideas on this subject!










      share|improve this question













      I am trying to build a system where multiple APIs gateway instances should execute the same lambda function.



      My problem is that I would like to change only the lambda configuration in function of the API gateway used.



      Let's take the name of a database as an example that should change if the lambda is being triggered from one API or the other.



      Example:



      API Gateways:
      https://my-first-api-gateway.execute-api.eu-west-1.amazonaws.com
      https://my-second-api-gateway.execute-api.eu-west-1.amazonaws.com


      I then have one lambda function being called by the two APIs: say_hello.



      This function has to retrieve a quote from a database. If the function has been called from my-first-api-gateway the lambda function has to use my_first_database and if the function has been called from my-second-api-gateway, it has to use my_second_database.



      The only solution I came with is to deploy as many lambda functions as I have API Gateways. And then use environment variables to store my database name.



      I don't like my solution because when I update a single line of code I'll have to redeploy all of my lambda functions.. (if I have 300 different databases to use, that would mean to update 300 lambda functions at once..)



      Thank you for your ideas on this subject!







      amazon-web-services lambda aws-lambda aws-api-gateway






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 8 at 13:15









      Hammerbot

      5,07331647




      5,07331647
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          If you use a Lambda-Proxy integration you should be fine.



          Full details of the event can be found here, however critically the headers Host, origin and Referer all point the the API Gateway uri:



          "headers": {
          "Host": "j3ap25j034.execute-api.eu-west-2.amazonaws.com",
          "origin": "https://j3ap25j034.execute-api.eu-west-2.amazonaws.com",
          "Referer": "https://j3ap25j034.execute-api.eu-west-2.amazonaws.com/dev/"
          }


          Therefore it should be fairly trivial for you to branch/look-up your database behaviour from this information.






          share|improve this answer





















          • Thank you for your response. What would you use to make this lookup then? Where would you store the map between the URL of the API gateway and databases names?
            – Hammerbot
            Nov 8 at 13:36






          • 1




            I suspect that depends a bit on how much you're looking to update it. If it's relatively infrequent update you might just store it in code. If not S3 (you can have a JSON file you read and cache), or Dynamo (key is execute-api id) are both contenters. Dynamo has an edge in that it's a bit faster (precise lookup) and presents a reasonable UI for editing in the AWS console.
            – thomasmichaelwallace
            Nov 8 at 13:54










          • Hi, just in case someone gets on this question some day: aws.amazon.com/blogs/compute/… stage variables allow passing variables from api gateway to lambda. It looks much more clean to me :) Credit to a redditer for sending me this link
            – Hammerbot
            Nov 9 at 10:28













          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53208525%2fmultiple-api-gateway-instances-one-lambda-function%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          2
          down vote



          accepted










          If you use a Lambda-Proxy integration you should be fine.



          Full details of the event can be found here, however critically the headers Host, origin and Referer all point the the API Gateway uri:



          "headers": {
          "Host": "j3ap25j034.execute-api.eu-west-2.amazonaws.com",
          "origin": "https://j3ap25j034.execute-api.eu-west-2.amazonaws.com",
          "Referer": "https://j3ap25j034.execute-api.eu-west-2.amazonaws.com/dev/"
          }


          Therefore it should be fairly trivial for you to branch/look-up your database behaviour from this information.






          share|improve this answer





















          • Thank you for your response. What would you use to make this lookup then? Where would you store the map between the URL of the API gateway and databases names?
            – Hammerbot
            Nov 8 at 13:36






          • 1




            I suspect that depends a bit on how much you're looking to update it. If it's relatively infrequent update you might just store it in code. If not S3 (you can have a JSON file you read and cache), or Dynamo (key is execute-api id) are both contenters. Dynamo has an edge in that it's a bit faster (precise lookup) and presents a reasonable UI for editing in the AWS console.
            – thomasmichaelwallace
            Nov 8 at 13:54










          • Hi, just in case someone gets on this question some day: aws.amazon.com/blogs/compute/… stage variables allow passing variables from api gateway to lambda. It looks much more clean to me :) Credit to a redditer for sending me this link
            – Hammerbot
            Nov 9 at 10:28

















          up vote
          2
          down vote



          accepted










          If you use a Lambda-Proxy integration you should be fine.



          Full details of the event can be found here, however critically the headers Host, origin and Referer all point the the API Gateway uri:



          "headers": {
          "Host": "j3ap25j034.execute-api.eu-west-2.amazonaws.com",
          "origin": "https://j3ap25j034.execute-api.eu-west-2.amazonaws.com",
          "Referer": "https://j3ap25j034.execute-api.eu-west-2.amazonaws.com/dev/"
          }


          Therefore it should be fairly trivial for you to branch/look-up your database behaviour from this information.






          share|improve this answer





















          • Thank you for your response. What would you use to make this lookup then? Where would you store the map between the URL of the API gateway and databases names?
            – Hammerbot
            Nov 8 at 13:36






          • 1




            I suspect that depends a bit on how much you're looking to update it. If it's relatively infrequent update you might just store it in code. If not S3 (you can have a JSON file you read and cache), or Dynamo (key is execute-api id) are both contenters. Dynamo has an edge in that it's a bit faster (precise lookup) and presents a reasonable UI for editing in the AWS console.
            – thomasmichaelwallace
            Nov 8 at 13:54










          • Hi, just in case someone gets on this question some day: aws.amazon.com/blogs/compute/… stage variables allow passing variables from api gateway to lambda. It looks much more clean to me :) Credit to a redditer for sending me this link
            – Hammerbot
            Nov 9 at 10:28















          up vote
          2
          down vote



          accepted







          up vote
          2
          down vote



          accepted






          If you use a Lambda-Proxy integration you should be fine.



          Full details of the event can be found here, however critically the headers Host, origin and Referer all point the the API Gateway uri:



          "headers": {
          "Host": "j3ap25j034.execute-api.eu-west-2.amazonaws.com",
          "origin": "https://j3ap25j034.execute-api.eu-west-2.amazonaws.com",
          "Referer": "https://j3ap25j034.execute-api.eu-west-2.amazonaws.com/dev/"
          }


          Therefore it should be fairly trivial for you to branch/look-up your database behaviour from this information.






          share|improve this answer












          If you use a Lambda-Proxy integration you should be fine.



          Full details of the event can be found here, however critically the headers Host, origin and Referer all point the the API Gateway uri:



          "headers": {
          "Host": "j3ap25j034.execute-api.eu-west-2.amazonaws.com",
          "origin": "https://j3ap25j034.execute-api.eu-west-2.amazonaws.com",
          "Referer": "https://j3ap25j034.execute-api.eu-west-2.amazonaws.com/dev/"
          }


          Therefore it should be fairly trivial for you to branch/look-up your database behaviour from this information.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 8 at 13:30









          thomasmichaelwallace

          2,0851816




          2,0851816












          • Thank you for your response. What would you use to make this lookup then? Where would you store the map between the URL of the API gateway and databases names?
            – Hammerbot
            Nov 8 at 13:36






          • 1




            I suspect that depends a bit on how much you're looking to update it. If it's relatively infrequent update you might just store it in code. If not S3 (you can have a JSON file you read and cache), or Dynamo (key is execute-api id) are both contenters. Dynamo has an edge in that it's a bit faster (precise lookup) and presents a reasonable UI for editing in the AWS console.
            – thomasmichaelwallace
            Nov 8 at 13:54










          • Hi, just in case someone gets on this question some day: aws.amazon.com/blogs/compute/… stage variables allow passing variables from api gateway to lambda. It looks much more clean to me :) Credit to a redditer for sending me this link
            – Hammerbot
            Nov 9 at 10:28




















          • Thank you for your response. What would you use to make this lookup then? Where would you store the map between the URL of the API gateway and databases names?
            – Hammerbot
            Nov 8 at 13:36






          • 1




            I suspect that depends a bit on how much you're looking to update it. If it's relatively infrequent update you might just store it in code. If not S3 (you can have a JSON file you read and cache), or Dynamo (key is execute-api id) are both contenters. Dynamo has an edge in that it's a bit faster (precise lookup) and presents a reasonable UI for editing in the AWS console.
            – thomasmichaelwallace
            Nov 8 at 13:54










          • Hi, just in case someone gets on this question some day: aws.amazon.com/blogs/compute/… stage variables allow passing variables from api gateway to lambda. It looks much more clean to me :) Credit to a redditer for sending me this link
            – Hammerbot
            Nov 9 at 10:28


















          Thank you for your response. What would you use to make this lookup then? Where would you store the map between the URL of the API gateway and databases names?
          – Hammerbot
          Nov 8 at 13:36




          Thank you for your response. What would you use to make this lookup then? Where would you store the map between the URL of the API gateway and databases names?
          – Hammerbot
          Nov 8 at 13:36




          1




          1




          I suspect that depends a bit on how much you're looking to update it. If it's relatively infrequent update you might just store it in code. If not S3 (you can have a JSON file you read and cache), or Dynamo (key is execute-api id) are both contenters. Dynamo has an edge in that it's a bit faster (precise lookup) and presents a reasonable UI for editing in the AWS console.
          – thomasmichaelwallace
          Nov 8 at 13:54




          I suspect that depends a bit on how much you're looking to update it. If it's relatively infrequent update you might just store it in code. If not S3 (you can have a JSON file you read and cache), or Dynamo (key is execute-api id) are both contenters. Dynamo has an edge in that it's a bit faster (precise lookup) and presents a reasonable UI for editing in the AWS console.
          – thomasmichaelwallace
          Nov 8 at 13:54












          Hi, just in case someone gets on this question some day: aws.amazon.com/blogs/compute/… stage variables allow passing variables from api gateway to lambda. It looks much more clean to me :) Credit to a redditer for sending me this link
          – Hammerbot
          Nov 9 at 10:28






          Hi, just in case someone gets on this question some day: aws.amazon.com/blogs/compute/… stage variables allow passing variables from api gateway to lambda. It looks much more clean to me :) Credit to a redditer for sending me this link
          – Hammerbot
          Nov 9 at 10:28




















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53208525%2fmultiple-api-gateway-instances-one-lambda-function%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Schultheiß

          Verwaltungsgliederung Dänemarks

          Liste der Kulturdenkmale in Wilsdruff