Search code examples
amazon-web-servicesaws-lambdaserverless-frameworkaws-appsync

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


I am using serverless to deploy an Appsync API and a few lambda functions. This plugin https://github.com/sid88in/serverless-appsync-plugin is used to deploy Appsync. The lambda and Appsync are defined in the same serverless.yml file and they are deployed altogether.

My lambda needs to know the Appsync API endpoint which is created during sls deploy. How can I reference the Appsync endpoint as environment variable to my lambda?

my 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}
    ...


Solution

  • AppSync plugin exposes a variable GraphQlApi which you can tap into for your environment variables which are automatically accessible in your lambdas.

    In your config:

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

    In your lambda:

    process.env.API_URL
    

    Or lambda config directly:

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