Deploying an Apollo GraphQL application as an AWS lambda function through Serverless

CSP
Level Up Coding
Published in
4 min readFeb 22, 2021

--

This article is in continuation of my previous article wherein we built a minimalistic application to pull AWS DynamoDB data from a locally hosted Graphql server.

Now we will modify the code further to run the same code as AWS Lambda function deployed through Serverless.

STEP 4: Make a lambda function

Apollo Server provides binding for lambda function through apollo-server-lambda module.

Change server.js, and rather than running Apollo Server, export an apollo GraphQL lambda function which AWS lambda will invoke based on HTTP event.

const { ApolloServer, gql } = require(‘apollo-server-lambda’);

const handler = server.createHandler({
cors: {
origin: true,
credentials: true,
},
});
exports.graphqlHandler = handler;
Complete code for server.js

STEP 5: Deploy to AWS

Now you need to publish this to AWS. For this, we will leverage Serverless (a framework to ease out developing and deploying serverless applications).

npm install -g serverless

Serverless needs an AWS profile to be able to publish and deploy the code on your behalf. In the last article, we created a profile called serverlessuser .

aws configure --profile serverlessuser

Run below command in CLI to provide access to serverless (replace aws_secret_access_key and aws_access_key_id)

serverless config credentials --provider aws -- key aws_access_key_id -- secret aws_secret_access_key — profile custom-profile

Create a serverless YAML file

Let’s go over this file.

This YAML file instructs to create an AWS Lambda service apollo-lambda and use runtime as node12 .

Now run the below command in CLI

serverless deploy --aws-profile serverlessuser

The CLI will show the location of endpoints (e.g.https://xxxx-east1.amazonaws.com/dev/graphql) to access the deployed GraphiQL interface.

Note: Ensure that the newly created Lambda function has access to DynamoDB by going to the permissions tab and attaching appropriate policies to access the DynamoDB (or all resources for development purpose) to the role.

You can now access the GraphiQL interface and run the query

query {
quotes {
id
value
source
}
}

That’s it!

Few other links to that will help you understand what serverless did for you and debug the application

CloudFormation, API Gateway, and CloudWatch logs,

Bonus: Behind the scene

The below section briefly covers what Serverless did for you.

First, it created a Lambda function. Let us try doing it ourselves.

Go to Lambda console.

Create Function- Author from Scratch > Fill in a name for set runtime to Node.

Also, ensure that lambda function runtime settings. The export function name should be graphql.graphqlHandler

Upload a file from Amazon S3 by providing the link URL of your zip. Leverage the same zip which serverless pushed for you in S3 bucket.

Lets try to run a test and execute a query on this function.

Configure a Test Event.

Enter this as a request:

{
"operationName": null,
"variables": {},
"query": "{\n quotes {\n id\n value\n source\n }\n}\n"
}

Save and hit “Test” and you will see an error! Why? because the function is being exposed to respond to an HTTP request. Here comes the second part of what serverless did for you.

Serverless creates a stackusing AWS Cloudformation which exposes HTTP endpoints through AWS API Gateway.

Check out the stack serverless created for the application apollo-lambda-dev. Go to the tab “template” and view it in the designer. This will show all the resources that were created to run the lambda function.

CloudFormation Designer

You define your resources, properties, and relations in your template and then create your stack from it. As you would see there are multiple resources and if you look closely you will see APIGateway RestAPI and Method (for handling GET, POST, etc) which are integrated with Lambda Function resource to execute on any Rest API call.

There is a lot going on in here which is beyond the scope of this article but hopefully, it would have given a glimpse of how Serverless helps in developing and deploying serverless applications.

If you enjoyed this, please click the 👏 button and follow me for more updates.

--

--

Striving to become my better self. I write on well researched thought provoking topics.