The Wayback Machine - https://web.archive.org/web/20240826230325/https://www.geeksforgeeks.org/how-to-deploy-graphql-api-using-aws-lambda-and-aws-api-gateway/
Open In App

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

Last Updated : 26 Mar, 2024
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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
5 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 spee
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
5 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
Build an API Gateway REST API with HTTP Integration
Pre-requisite: AWS We can use either the HTTP proxy integration or the HTTP custom integration to build an API using HTTP integration. When feasible, leverage HTTP proxy integration for faster API setup while delivering varied and strong functionality. If it is essential to change client request data for the backend or backend response data for the
4 min read
Create a REST API as an Amazon Kinesis Proxy in API Gateway
Pre-Requisite: AWS Amazon Kinesis is a managed, scalable, cloud-based service that enables real-time processing of enormous amounts of data per second in real-time. It is intended for real-time applications and lets developers collect an unlimited amount of data from many sources, scaling up and down that can be run on EC2 instances. It is used to
4 min read
Create a REST API as an Amazon S3 Proxy in API Gateway Automation
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 gonna build and Deploy a REST API with API Gateway which acts as a proxy to S3 and can be used to perform Read/Write on S3 without any intermediate service, here we gonna use an open-
6 min read
How To Build Real-Time Chat Application With AWS AppSync And GraphQL
AWS Live Chat is a service provider that provides a serverless, scalable, and secure chat application. AWS Lambda resolves the tasks by performing the necessary data transformations before sending the data to the client. Alternatively, It also gives a reliable query language for APIs that helps in enabling clients to request specific data. AWS AppS
10 min read
Amazon Web Services (AWS) Transit Gateway VS VPC Peering
Are you looking forward to enhancing the connectivity of your Cloud Infrastructure using AWS? If it is so, you have landed in the right place. This article covers detailed AWS Transit Gateway and VPC Peering, their advantages, use cases, and differences. By the end of this article, you will be easily able to decide which AWS networking solution bes
8 min read
How To Configure GateWay Load Balancer In AWS ?
In the advanced landscape of cloud computing, managing incoming internet traffic efficiently is fundamental for ensuring the accessibility, scalability, and reliability of applications. AWS offers a set of services to address these necessities, and one such help is the Gateway Load Balancer (GWLB). Gateway Load Balancer is an incredible tool intend
7 min read
AWS Transit Gateway
In the powerful world of cloud computing, managing network availability between different parts can immediately become complex and challenging. Amazon Web Services (AWS) perceived this need and presented AWS Transit Gateway as a solution to work on network architecture and improve networks inside the AWS Cloud environment. AWS Transit Gateway is a
7 min read
Deploy a Website Using AWS Amplify
Pre-requisite: AWS Amplify Amplify is a suite of tools and frameworks developed by Amazon Web Services (AWS) that are used for managing hosting services for web apps as well as for launching software applications on AWS. On AWS, two well-known services include Amplify Studio and Hosting. Amplify Studio: It is a platform for development that is typi
2 min read
How to Deploy a Flask Web Server in Docker Container using AWS?
In the current digitized era, web servers are essential as they stand for various online applications and sites. Web servers run stealthily the scenes to give the devices needed information and functional facilities irrespective of whether surfing on the internet, using a mobile app, or cloud facilities. In this article, we will guide you through h
10 min read
Deploy Java Microservices on Amazon ECS using AWS Fargate
In recent, Microservices architecture has gained huge popularity. It provides an effective way to develop and deploy applications. The Architecture of Microservices works on dividing a Monolithic big application into smaller sub-applications as independent services that can be easily developed, deployed, and scalable separately. This type of approa
10 min read
Difference Between Lambda and Amplify in AWS
Pre-requisite: AWS By using the computing service offered by Amazon Lambda, you may run code without installing or managing servers. Almost any type of backend service or application may be executed with Lambda. Providing Lambda with your code in one of the languages it supports is all that is necessary. Lambda manages all compute resource manageme
3 min read
Serverless Image Processing with AWS Lambda and S3
AWS S3 (Simple Storage Service) is a cloud data storage service. It is one of the most popular services of AWS. It has high scalability, availability, security and is cost effective. S3 has different storage tiers depending on the use case. Some common use cases of AWS S3 are: Storage: It can be used for storing large amounts of data.Backup and Arc
11 min read
AWS Lambda and Amazon DynamoDB for Serverless Authentication
Serverless Authentication is a method of authenticating users in a cloud-based application without the need for traditional server management. It leverages services like AWS Lambda and Amazon DynamoDB to handle user authentication processes in a cost-effective and efficient manner. By using these serverless components, developers can create secure
14 min read
Securing AWS Lambda Functions With IAM Roles And Policies
AWS Lambda is a serverless computing service that helps in executing code without any management of servers while AWS IAM is an essential security component that allows authorized individuals or services to have access of other AWS resources. These two AWS services are very important on the AWS cloud platform. Here in this guide, I have first discu
5 min read
Serverless Computing With AWS Lambda And Docker: Running Custom Containers
AWS Lambda is a serverless computing service that runs the code without any management of servers by the user. On the other hand, Docker is a tool that helps to encapsulate applications with its dependency into docker containers. AWS Lambda and Docker are some of the most important services used by organizations. Here in this, I will first explain
5 min read
How To Use AWS Lambda Layers for Code Reuse and Organization?
AWS lambda is an AWS service that works with Serverless Architecture, The infrastructure management is automatically managed by the AWS. It facilitates the developers to focus on the Code development completely. AWS Lambda Layers extends the core functionalities of the AWS Lambda functions through an efficient approach for managing dependencies and
12 min read
Blue/Green Deployment in AWS Lambda
Blue Green Deployment is just like we deploy two versions of our application, one is the stable version, and another is a new feature or bug fix let's say, forwarding a certain percentage of traffic to the second version as well in production to ensure that everything is working fine. The Blue environment represents the currently active version of
7 min read
How to Pass the Query String Parameters to AWS Lambda Function or HTTP Endpoint?
In this article, we will explore the process of passing query string parameters from a client to the Backend Lambda or HTTP endpoint API gateway. As we know there are two types of integrations for Lambda and HTTP endpoints. The first kind is Proxy integration and second kind is Non-proxy integration. While proxy implementation is the recommended ap
4 min read
How to Configure AWS Lambda?
AWS Lambda is a responsive cloud service that examines the steps followed within any application and responds to them by compiling the codes that have been defined by the users, also known as functions. The service automatically computes and manages the resources across multiple availability zones and functions through them whenever new actions are
4 min read
AWS Lambda - Copy Object Among S3 Based on Events
In this article, we will make AWS Lambda function to copy files from one s3 bucket to another s3 bucket. The lambda function will get triggered upon receiving the file in the source bucket. We will make use of Amazon S3 Events. Every file when uploaded to the source bucket will be an event, this needs to trigger a Lambda function which can then pro
3 min read