Open In App

How To Deploy GraphQL API Using AWS Lambda And AWS API Gateway ?

Last Updated : 26 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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?

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

Installing 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.

Deploy Django App using Zappa cli

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.

Get API response from the deployed API

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.

Deploy GraphQL API, AWS Lambda And AWS API Gateway – FAQ’s

What Are Other Ways To Deploy A Graphql App In Lambda?

Yes, there are other ways too, such as deploying using Appsync, deploying using the Custom docker image deployment using Lambda, but for most of the scenarios this should be enough.

What Is The Advantage Of Using GraphQL Over Rest APIs?

GraphQL provides a more efficient and flexible alternative to traditional REST APIs by allowing clients to request only the data they need, reducing over-fetching and under-fetching of data.

How Does AWS Lambda Differ From Traditional Server-based Solutions Like EC2?

AWS Lambda is serverless, meaning developers don’t have to manage infrastructure. It scales automatically and charges only for the compute time consumed, making it cost-effective and eliminating the need for server management tasks.

Why Choose AWS Lambda For Deploying Django Applications?

AWS Lambda offers seamless integration with other AWS services, such as API Gateway, making it easy to build and deploy scalable applications. Additionally, it eliminates the need for server management, reducing operational overhead.

What Is Zappa, And How Does It Facilitate Django Deployment On AWS Lambda?

Zappa is a Python library that simplifies the deployment of Django applications on AWS Lambda. It bundles the application code along with project requirements and handles the integration with API Gateway, streamlining the deployment process.



Similar Reads

AWS Lambda - Create a Lambda Function in Python, Integrated with API Gateway
Pre-requisite: AWS We will begin with creating a lambda function in the AWS Lambda console and write our Python code for the same. We will test our function by creating the new event and verifying the response. Next, we will configure the lambda function with the JSON placeholder which is a sample API. We will be using the concept of layers in the
3 min read
Building a Serverless Blog with AWS Lambda and API Gateway
AWS Lambda is a serverless computing service offered by AWS. It enables you to run code in response to events without the need to manage servers. This event-driven service charges you based on actual compute time, making it cost-effective. Lambda supports various programming languages, scales automatically, and seamlessly integrates with other AWS
9 min read
Build an API Gateway REST API with Lambda Integration
Pre-requisite: AWS Amazon Web Services is a leading cloud provider which provides us with plenty of Paas, and Iaas, and services that we can use to build and deploy our applications. we going to build and Deploy a REST API with API Gateway which is integrated with AWS Lambda and expose GET and POST methods, here we going to use an open-source frame
4 min read
How to Deploy Django Application in AWS Lambda?
Pre-requisite: AWS, Python Django is a Python web framework that makes it easy to build web applications quickly and securely. It has a large and helpful community that provides support and contributes to its development. AWS Lambda is a serverless computing platform that runs your code in Docker containers, provides us with the best possible speed
7 min read
Difference Between Internet Gateway and NAT Gateway
Pre-requisite: AWS In the world of networking, the terms "Internet Gateway" and "NAT Gateway" are commonly used, but they can often be confusing. In this article, we will explain the difference between an Internet Gateway and NAT Gateway. We will define each term and explain how they work, their use cases, and the main differences between them. Int
4 min read
AWS DynamoDB - Insert Data Using AWS Lambda
In this article, we will look into the process of inserting data into a DynamoDB table using AWS Lambda. Amazon DynamoDB is a completely owned NoSQL proprietary provider that helps key-value and textual statistics systems and is supplied via way of means of Amazon.com as a part of Amazon Web Services. AWS Lambda is an event-driven, serverless compu
3 min read
AWS Lambda Functions With AWS CLI
Amazon Web Services (AWS) is a comprehensive cloud computing platform offering many services, including storage, computing, databases, and more. 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
6 min read
AWS Lambda Deployments with AWS CloudFormation
AWS CloudFormation allows to create AWS resources with help of templates. It provisions and deploy all the resources required for the entire application described as a Stack. Templates can be updated and replicated as required to reduce the overhead of implementing and provisioning infrastructure. let's learn how we can provision lambda deployments
6 min read
Complete Guide To Create AWS API Gateway With Terraform
AWS Application Programming Interface (API) Gateway is a completely managed service presented by Amazon Web Services (AWS) that empowers developers to create, monitor, deploy, and secure APIs at any scale. It goes about as a gateway for managing and routing HTTP and WebSocket traffic to backend service, including AWS Lambda function, Amazon EC2 ins
7 min read
Amazon Web Services - Resolving 403 Forbidden Error When Connecting to API from VPC through API Gateway
The Amazon API Gateway is used to create scalable APIs. It can perform additional functionalities like publishing the API, monitoring and maintaining the API. These can also be used to develop RESTful APIs and WebSocket APIs which can be used for real-time two-way communication between the client and the server. In simpler words we can say that, th
3 min read