Running a GraphQL Endpoint with Serverless — Using Python
In this article, we’ll demonstrate how to set up a GraphQL endpoint with Serverless on AWS Lambda, using Python. We’ll also show how to deploy the endpoint using Continuous Integration and Deployment (CI/CD) pipelines with GitHub Actions.
Setting up the Apollo Server
To set up the Apollo Server, we’ll use the graphene-python
library, which provides a simple way to create GraphQL APIs in Python. First, create a new directory for the project and navigate into it:
mkdir graphql-serverless
cd graphql-serverless
Next, create a new virtual environment and activate it:
python -m venv venv
source venv/bin/activate
Install the required dependencies by running the following command:
pip install graphene Flask
This installs the graphene
and Flask
libraries.
Next, create a new file named handler.py
in the graphql-serverless
directory with the following code:
import graphene
import flask
from flask import Flask
from flask_graphql import GraphQLView
class Query(graphene.ObjectType):
hello = graphene.String(description='A simple greeting')
def resolve_hello(self, info):
return 'Hello, Serverless!'
app = Flask(__name__)
app.add_url_rule(
'/graphql',
view_func=GraphQLView.as_view(
'graphql',
schema=graphene.Schema(query=Query),
graphiql=True
)
)
def graphql_handler(event, context):
return flask.Response(app(flask.request.environ, context.respond))
This sets up the Apollo Server with a single query named hello
that returns the string 'Hello, Serverless!'
.
Setting up the Serverless Function
Next, we’ll set up the Serverless function to host the Apollo Server. First, install the serverless
framework by running the following command:
npm install -g serverless
Create a new Serverless service by running the following command:
serverless create --template aws-python3
This creates a new Serverless service with the required configuration files.
Replace the contents of serverless.yml
with the following code:
service: graphql-serverless
framework:
name: flask
provider:
name: aws
runtime: python3.8
functions:
graphql:
handler: handler.graphql_handler
events:
- http:
path: graphql
method: post
plugins:
- serverless-flask-binary
This configures a single Serverless function named graphql
that will be triggered by a HTTP POST request to the /graphql
endpoint. It also includes the serverless-flask-binary
plugin, which is required for Flask to work on AWS Lambda.
Deploying the Serverless
To deploy the Serverless endpoint, simply run the following command:
serverless deploy
This will compile the Serverless function and deploy it to AWS Lambda. Once the deployment is complete, the command will output the endpoint URL, which you can use to access the GraphQL API.
Setting up the CI/CD Pipeline
Finally, we’ll set up a CI/CD pipeline to automate the deployment process using GitHub Actions. First, create a new repository on GitHub for the project and push the code to it.
Next, navigate to the Actions tab in the repository and create a new workflow. Replace the contents of main.yml
with the following code:
name: Deploy
on:
push:
branches:
- main
env:
AWS_REGION: us-west-2
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Set up Python environment
uses: actions/setup-python@v2
with:
python-version: 3.8
- name: Install dependencies
run: |
python -m venv venv
source venv/bin/activate
pip install -r requirements.txt
- name: Deploy to AWS Lambda
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ env.AWS_REGION }}
- name: Deploy with Serverless
run: |
npx serverless deploy --aws-profile default
This workflow is triggered whenever a push is made to the main
branch. It sets up the Python environment, installs the required dependencies, and deploys the Serverless endpoint using the serverless
framework.
You’ll also need to add your AWS credentials as secrets in the repository. To do this, navigate to the repository’s Settings tab, select Secrets, and add two new secrets named AWS_ACCESS_KEY_ID
and AWS_SECRET_ACCESS_KEY
with your AWS credentials.
With the CI/CD pipeline set up, every time you make changes to the code and push them to the main
branch, the pipeline will automatically deploy the changes to AWS Lambda.
Conclusion
In this article, we demonstrated how to run a GraphQL endpoint with Serverless on AWS Lambda using Python. We covered the basics of setting up an Apollo Server, deploying it as a Serverless function, and automating the deployment process using a CI/CD pipeline with GitHub Actions.
The combination of GraphQL and Serverless provides a powerful platform for building and deploying modern web applications, allowing you to take advantage of the scalability and cost-effectiveness of AWS Lambda while keeping the development process simple and streamlined.