Setting Up an Apollo Server (GraphQL) on AWS with Serverless Framework

Matías Salinas
3 min readMar 15, 2023

--

Introduction

We will to see how to setting up an Apollo Server (GraphQL) on AWS using the Serverless Framework and SLS CLI. This will enable you to create a scalable GraphQL API that takes advantage of serverless architecture. We will cover the necessary steps, including initializing a Serverless project, creating a simple GraphQL schema, configuring the Serverless Framework, and deploying your Apollo Server on AWS.

Prerequisites

To follow along with this tutorial, you should have:

  1. Basic knowledge of GraphQL, Apollo Server, AWS, and Node.js
  2. Node.js and npm installed on your local machine
  3. An AWS account and the AWS CLI installed and configured
  4. The Serverless Framework installed on your local machine

Step 1: Initialize a new Serverless project

First, install the Serverless Framework as a global npm package:

$ npm install -g serverless

Next, create a new Serverless project using the SLS CLI:

$ sls create --template aws-nodejs --path apollo-server-graphql

This command will create a new directory named apollo-server-graphql with an AWS Node.js template. Navigate to the newly created directory:

$ cd apollo-server-graphql

Step 2: Initialize a new Node.js project

Initialize a new Node.js project by running the following command:

$ npm init -y

This command will create a package.json file with default settings.

Step 3: Install required dependencies

Install the required dependencies for your project:

$ npm install apollo-server-lambda graphql

These packages include the necessary components for running an Apollo Server on AWS Lambda.

Step 4: Create a simple GraphQL schema

Create a new file named schema.js and add the following code to define a simple GraphQL schema:

const { gql } = require('apollo-server-lambda');
const typeDefs = gql`
type Query {
hello: String
}
`;
const resolvers = {
Query: {
hello: () => 'Hello, world!',
},
};
module.exports = { typeDefs, resolvers };

This schema defines a single query, hello, which returns a string.

Step 5: Set up the Apollo Server

Create a new file named server.js and add the following code to set up the Apollo Server:

const { ApolloServer } = require('apollo-server-lambda');
const { typeDefs, resolvers } = require('./schema');
const server = new ApolloServer({
typeDefs,
resolvers,
});
exports.graphqlHandler = server.createHandler();

This code initializes the Apollo Server with the provided schema and exports the GraphQL handler function.

Step 6: Configure the Serverless Framework

Create a serverless.yml file in your project's root directory to configure the Serverless Framework:

service: apollo-server-graphql
provider:
name: aws
runtime: nodejs14.x
stage: dev
region: us-east-1
memorySize: 256
timeout: 10
functions:
graphql:
handler: server.graphqlHandler
events:
- http:
path: graphql
method: any
cors: true

This configuration file defines a single AWS Lambda function named graphql using the Apollo Server handler from the server.js file. It also sets up an HTTP event trigger with CORS enabled.

Step 7: Deploy the Apollo Server

Deploy your Apollo Server to AWS by running the following command:

$ sls deploy

The Serverless Framework will package and deploy your application to AWS. Once the deployment is complete, you will receive an endpoint URL for your GraphQL API in the output. It will look like this:

endpoints:
ANY - https://<unique_id>.execute-api.<region>.amazonaws.com/dev/graphql

Make a note of this URL, as you will need it to interact with your GraphQL API.

Conclusion

We saw how to set up an Apollo Server (GraphQL) on AWS using the Serverless Framework and SLS CLI. This approach enables you to create a scalable GraphQL API that leverages serverless architecture. With your GraphQL API now deployed, you can start building clients that consume the API, iterate on your schema and resolvers, and add additional features to your server as needed.

--

--

Matías Salinas
Matías Salinas

No responses yet