Serverless GraphQL server on AWS Lambda
Serverless environments such as AWS Lambda allows you to run your server-side code without obtaining/maintaining/paying a dedicated server. You don’t have to care about OS/software updates and you can focus on your application code. Also, you pay as you go!
In this article we will see how to run a GraphQL server on AWS Lambda step by step.
We are going to use:
- NodeJS
- Lambda & API Gateway
- AWS cli
- CloudFormation
- S3 bucket
Setup the NodeJs project
At the time of this writing, the latest NodeJs version that is supported in the AWS Lambda environment is nodejs12.x
. So, make sure that you have installed a v12.X
version in your system. We will use nvm
for this.
nvm install v12.7.0
nvm use v12.7.0
The Lambda function
We will use the NodeJS library apollo-server-lambda
which is the AWS Lambda integration of GraphQL Server.
npm init
npm i apollo-server-lambda
graphql.js
CloudFormation
CloudFormation allows you to define AWS resources in code. In the following template we define a Lambda function and an API Gateway by using the AWS::Serverless::Function
resource type:
template.yml
AWS cli
In the next steps we will use the AWS cli to deploy our app. Make sure that you have configured the AWS cli
first by running aws configure
.
S3 Bucket
We need to create a S3 bucket to host the code of the Lambda function. Replace {your-bucket-name}
with a universal unique S3 bucket name of your choice.
~ $ aws s3 mb s3://{your-bucket-name} --region eu-central-1
Package your code
The below command zips & uploads your code in your S3 bucket. Also, it exports the template-output.yml
file which will be used in the next step.
~ $ aws cloudformation package \
--template-file template.yml \
--output-template-file template-output.yml \
--s3-bucket {your-bucket-name}
Deploy your code
The following command will deploy our code to our Lambda function. You can replace the serverless-graphql
with your CloudFormation stack name of your choice.
~ $ aws cloudformation deploy \
--template-file template-output.yml \
--stack-name serverless-graphql \
--capabilities CAPABILITY_IAM \
--no-fail-on-empty-changeset
waiting for changeset to be created..
Waiting for stack create/update to complete
Successfully created/updated stack - serverless-graphql
Export the URL of the GraphQL server
The final step is to get the exported GraphQL URL:
~ $ aws cloudformation list-exports \
--query "Exports[?Name=='GraphQLApi'].Value"
[
"https://k6hl3sdw0b.execute-api.eu-central-1.amazonaws.com/Prod/graphql"
]
We are live!
We are live at 🚀 https://k6hl3sdw0b.execute-api.eu-central-1.amazonaws.com/Prod/graphql 🚀.
At this link we can play with the GraphQL Playground which is a graphical, interactive, in-browser GraphQL IDE that allows you to explore/test visually the avaialble GraphQL queries, mutations and subcriptions.
Also, let’s test it from the command line:
$ curl \
-X POST \
-H "Content-Type: application/json" \
--data '{ "query": "{ me { id name } } "}' \
https://k6hl3sdw0b.execute-api.eu-central-1.amazonaws.com/Prod/graphql
{"data":{"me":{"id":"1","name":"John"}}}
Nice! It works as expected!
The complete code of this post is at my Github repo!
For more related posts follow me on Twitter.
Thanks for reading! If you have questions/suggestions please leave a comment below!
PS: Original post here: https://dusaitis.com/posts/serverless-graphql-server-on-aws-lambda