如何在无服务器配置文件中获取 appsync API 端点?

How can I get appsync API endpoint in serverless configure file?

我正在使用 serverless 部署 Appsync API 和一些 lambda 函数。此插件 https://github.com/sid88in/serverless-appsync-plugin 用于部署 Appsync。 lambda 和 Appsync 在同一个 serverless.yml 文件中定义,并且一起部署。

我的 lambda 需要知道在 sls deploy 期间创建的 Appsync API 端点。如何将 Appsync 端点作为环境变量引用到我的 lambda?

我的serverless.yml:

function:
   myHandler:
      handler: src/lambdas.myHandler
      name: myHandler
      environment:
        ENDPOINT: # HERE, how can I get appsync endpoint url?

custom:
  appSync:
    name: ${self:provider.stackName}
    ...

AppSync 插件公开了一个变量 GraphQlApi,您可以利用该变量获取可在 lambda 中自动访问的环境变量。

在您的配置中:

environment:
  API_URL: { Fn::GetAtt: [ GraphQlApi, GraphQLUrl ] }

在你的 lambda 中:

process.env.API_URL

或者直接lambda配置:

  name: myHandler
  environment:
    ENDPOINT: { Fn::GetAtt: [ GraphQlApi, GraphQLUrl ] }