How To Deploy GraphQL API Using AWS Lambda And AWS API Gateway ?
GraphQL is known for its flexibility and efficiency. AWS Lambda, on the other hand, provides a serverless computing service that allows developers to run code without managing servers, and we pay for what we use. We can combine the power of GraphQL with the scalability and ease of AWS Lambda to serve our GraphQL application easily with minimal steps & efforts possible.
In this article will take you on a step-by-step guide to deploy your own GraphQL API using AWS Lambda and AWS API Gateway, we'll guide you how to define schema, write resolvers for the queries & mutations, configue urls, and deploying the API, testing.
What Is GraphQL?
GraphQL is a query language and runtime for APIs (Application Programming Interfaces). It provides a more efficient, powerful, and flexible alternative to the traditional REST API (Representational State Transfer), It was developed by Facebook and it's an open-source technology.
What Is AWS Lambda?
AWS Lambda is a serverless computing service provided by Amazon Web Services (AWS). we can create functions and self-contained applications.
With AWS Lambda functions we can perform any kind of computing task, from serving web pages to building backend APIs, and we can integrate lambda with other AWS services as well, here in this article, we are integrating lambda with API Gateway.
AWS API Gateway
Amazon API Gateway is a fully managed service facilitating effortless creation, publication, monitoring, and securing of APIs. Serving as the "front door" for applications, it supports access to backend services via RESTful and WebSocket APIs, providing to real-time communication needs. With features like request management, authorization, and monitoring.
API Gateway provides integration with various services like AWS Lambda, DynamoDB, etc and also you integrate your HTTP endpoint, and you know AWS provides free 1 million API calls per month for 12 months with the AWS Free Tier.
Why We Choose AWS Lambda?
Also, there are several reasons why we choose AWS Lambda over other choices like AWS EC2, Elastic Bean Stalk, etc for deploying our Django application:
- Cost-Effective: AWS Lambda is a cost-effective option when compared to other services we've.
- Integration With Other AWS Services: AWS Lambda has support for various other AWS services, we can integrate them easily such as API Gateway, Event bridge, etc, which makes it easy to deploy.
- No Server Management: With AWS Lambda, we don't have to worry about managing the infrastructure, as AWS takes care of it for us. and we going to use Zappa for deploying our Django application to AWS Lambda.
Deploying GraphQL Using AWS Lambda And AWS API Gateway: A Step-By-Step Guide
There are many ways to deploy GraphQL API, In this article we're going to deploy Django Based GraphQL API using AWS Lamda and API Gateway, as part of this we'll be using third party python packages as well.
Graphene-Django is an open-source library that provides seamless integration between Django, a high-level Python web framework, and Graphene, a library for building GraphQL APIs. The library allows developers to create GraphQL APIs in Django quickly and we can easily integrate this with AWS Lambda without any external dependencies.
Step 1: Install graphene-django
- Run the below command to install graphene-django.
pip install graphene-django

Step 2: Configure Django Settings
- After installing, add 'graphene_django' to your Django project's INSTALLED_APPS list, and also configure the graphql schema path for your app using GRAPHENE variable.
INSTALLED_APPS = [
...
'graphene_django',
]
GRAPHENE = {
'SCHEMA': 'myboringapp.schema.schema'
}
Note: Replace your app name in the place of myboringapp.
Step 3: Configure Urls
- Configure the url path /graphql to access your apis, (the below config is useless without configuring GRAPHENE in django settings)
from django.urls import path
from graphene_django.views import GraphQLView
from . import schema
urlpatterns = [
...
path('graphql/', GraphQLView.as_view(graphiql=True)),
]
Step 4: Define Schema
- In the myboringapp/schema.py, define the schema for your APIs, here is the sample API schema:
Here we've defined a schema with one query get_hello, resolver will return "Hello, GeeksForGeeks" in response for that query, resolvers are responsible for populating the data for each field, here you can consider get_hello as a field for easy understanding, when get_query is triggered via API, the resolver will return the data defined in it or by processsing the business logic.
import graphene
from graphene_django import DjangoObjectType
class Query(graphene.ObjectType):
get_hello = graphene.String(description='A simple greeting')
def resolve_get_hello(self, info):
return 'Hello, GeeksForGeeks!'
schema = graphene.Schema(query=Query)
Step 5: Deploy Application
Note: For more details on Deployment, visit How to Deploy Django Application in AWS Lambda?
- we are all set for deployment, we configured our app with the required things, and now we can deploy our application to AWS Lambda.
- Here the below command will bundle your application along with your project requirements and deploy it in AWS Lambda.
zappa deploy <stage>
- The above command will create a Lambda function with our Django application code and integrates it with API Gateway so that we can invoke the Django API.
-(1)-768.png)
Testing
We can use the below query to test our deployment, the below will ask API to return the response for the `getHello` query as we've defined it as string response with "Hello, GeeksForGeeks!", we'll get that in response.
To test your graphql query, you can use apollo sandbox, just provide your deployed endpoint there, and use the below query and click on GetHelloFromGFG, below is the reference.

Conclusion
We've deployed a Serverless Django GraphQL API using AWS Lambda & API Gateway, we've learnt how to define schema and expose API endpoints using graphene django, you can also try adding more queries, mutations, and deploy a complex APIs as well using this method.