Search code examples
amazon-web-servicesaws-cloudformationaws-api-gatewayserverless-frameworkaws-sdk-nodejs

How to get API gateway url in Environment variable in Serverless


I am working on a serverless framework(cloud formation), I have created two stacks in the same AWS region, and each serverless.yml(stack1, stack2) having their own APIs. How can I get the stack-1(yml) API URL in stack-2 yml.

Stack-1(serverless1.yml)

functions:
  tGateWay:
    handler: src/handlers/tGateWay.handler
    name: ${self:provider.stage}-tGateWay
    environment:
      first: ${self:provider.stage}-firstlambda
    events:
      - http:
          method: any
          path: /tGateway
          private: true

Stack-2(serverless2.yml)

functions:
  tModule:
    handler: src/handlers/tModule.handler
    name: ${self:provider.stage}-tModule
    environment:
      find: ${self:provider.stage}-find
      T_GATEWAY: https://xxxxxxxxx.execute-api.xxxxxxx-1.amazonaws.com/dev/tGateway

In that T_GATEWAY I want to access stack-1 API gateway URL

Thanks in advance!


Solution

  • You can use SecretsManager and you can import it from there into your function with something like this:

    iamRoleStatements:
        - Effect: Allow
          Action:
            - secretsmanager:GetSecretValue
    
    functions:
      handler.js
      myHandler:
        handler: myHandler
        environment:
          yourSecretKey: "{{resolve:secretsmanager:secretYouCreated:SecretString:yourSecretKey}}"