export const meta = { title: `GraphQL Server in Lambda`, description: `How to run an Apollo GraphQL server in a Lambda function`, }; In this guide you will learn how to run a GraphQL server in a Lambda function. In this example you will be using [Apollo Server](https://www.apollographql.com/docs/) and [Apollo Server Lambda](https://github.com/apollographql/apollo-server/tree/master/packages/apollo-server-lambda), but you can use any server implementation you would like. The end goal is to have an API endpoint, like `https://your-api-endpoint.com/graphql`, deployed and integrated with a GraphQL server running in a serverless function. ### Creating the Amplify project To get started, create a new Amplify project. If you already have an Amplify project created, you can jump to the next step - Creating the GraphQL API and function ```sh amplify init # Choose your environment name and default text editor # You can answer the defaults for the rest of the questions and then choose the profile you created when you ran amplify configure ``` ### Creating the GraphQL API and function Next you need to create the API and the Lambda function. Using the `api` category, the CLI will create a serverless function as well as an http endpoint that you can use for your GraphQL server. ```sh $ amplify add api ? Please select from one of the below mentioned services: REST ? Provide a friendly name for your resource to be used as a label for this category in the project: apolloapi ? Provide a path (e.g., /items): /graphql ? Choose a Lambda source: Create a new Lambda function ? Provide a friendly name for your resource to be used as a label for this category in the project: apolloserver ? Provide the AWS Lambda function name: apolloserver ? Choose the function runtime that you want to use: NodeJS ? Choose the function template that you want to use: Hello World ? Do you want to access other resources created in this project from your Lambda function? N ? Do you want to edit the local lambda function now? N ? Restrict API access: N ? Do you want to add another path? N ``` #### Installing the dependencies Change into the folder of the Lambda function and install the following dependencies: ```sh cd amplify/backend/function/apolloserver/src npm install apollo-server-lambda graphql cd ../../../../../ ``` ### Function code Now, let's open the code for the function. Open __amplify/backend/function/apolloserver/src/index.js__. Here, you will see the main function handler. Update the function with the following code: ```javascript const { ApolloServer, gql } = require('apollo-server-lambda'); /* Construct a schema, using GraphQL schema language */ const typeDefs = gql` type Query { hello: String } ` /* Provide resolver functions for your schema fields */ const resolvers = { Query: { hello: () => 'Hello from Apollo!!', }, } const server = new ApolloServer({ typeDefs, resolvers, context: ({ event, context }) => ({ headers: event.headers, functionName: context.functionName, event, context, }), }) exports.handler = server.createHandler({ cors: { origin: '*', credentials: true, } }) ``` Now, you can deploy the function and GraphQL API: ```sh amplify push ``` Now the API is deployed and you should be able to start interacting with it. ### API URL Once the server is up and running, The url is available in the aws-exports.js file. The final GraphQL endpoint will look something like this: ``` https://6dbg37jfw5.execute-api.us-east-1.amazonaws.com//graphql ``` You can also use the GraphQL playground by navigating to the GraphQL endpoint `/graphql` directly in your browser.