Saidy Barry
3min read
How to deploy a Serverless Node.js or Apollo GraphQL Server to AWS Lambda
DevOps has always been a pain in the ass for me personally way before server-less technology came into play, I have always love the idea of not having to worry about application deployment or spinning up a server to host an application. Although no matter how hard you try to run away from it, it’s inevitable you will have to do at some point as a software engineer especially in this era of server-less function which has made deployment so easy with the help of platforms like vercel, AWS, Azure and etc.
Recently our company switched to using Nx workspaces as a monorepo support with all it’s addons such as fast builds, rich plugin ecosystem to name a few, from yarn workspaces. One of the project I was working on is a graphql server and I had to deploy it to AWS lambda. In this tutorial I will show you a step by step guild on how to deploy a apollo graphql server or a nodejs server on a NX workspace to AWS lambda.
Deploying serverless applications has become easier with technologies like AWS Lambda and the Serverless Framework. In this guide, we’ll walk through the steps to deploy a Node.js or Apollo GraphQL server from an Nx workspace to AWS Lambda.
1. Convert to serverless function.
First, convert your Node.js server to a serverless function. For an Apollo GraphQL server, switch to using “apollo-server-lambda” instead of “apollo-server.” This library simplifies the conversion process.
const { ApolloServer } = require('apollo-server-lambda');
export const typeDefs = gql`
type Query {
testMessage: String!
}
`;
export const resolvers = {
Query: {
testMessage: () => 'Hello World!',
},
};
const server = new ApolloServer({
typeDefs,
resolvers,
});
module.exports.graphqlHandler = server.createHandler();
2. Adjust the production build configuration.
By default, Nx workspace’s build configurations omit certain options. To enable AWS Lambda to execute the serverless function, add a configuration to ensure the “package.json” file is included. Set the “optimization” field to false to prevent code minification.
"configurations": {
"production": {
"optimization": false,
"extractLicenses": true,
"inspect": false,
"generatePackageJson": true
}
}
Now build the project with the following command
nx build projectName --prod
3. Install the serverless framework
Install the Serverless Framework globally. It provides tools and components for developing, deploying, and securing serverless applications on AWS and other platforms.
npm install -g serverless
Create a serverless file and name it serverless.
# serverless.yml
service: aws-lambda
plugins:
- serverless-layers
- serverless-dotenv-plugin
- serverless-deployment-bucket
custom:
prune:
automatic: true
number: 3
serverless-layers:
dependenciesPath: buildFolderPath/package.json
dotenv:
path: /.evn
provider:
name: aws
runtime: nodejs14.x
region: eu-central-1
profile: default
stage: production
deploymentBucket:
name: aws-lamda
serverSideEncryption: AES256
package:
individually: true
include:
- buildFolderPath/<build-folder>/**
- buildFolderPath/main.js
exclude:
- '**'
functions:
siminko:
#this is formatted as <FILENAME>.<HANDLER>
handler: buildFolderPath/main.graphqlHandler
name: aws-lambda
events:
- http:
path: /
method: post
cors: true
- http:
path: /
method: get
cors: true
Add these required serverless plugin to make deployment easy.
npm install -S serverless-layers
npm install -S serverless-dotenv-plugin
npm install -S serverless-deployment-bucket
4. Setup the AWS environment to deploy.
Use the AWS CLI to deploy the serverless function. If you haven’t set up the AWS CLI, refer to AWS’s documentation for best practices.
Deploying serverless applications has become easier with technologies like AWS Lambda and the Serverless Framework. In this guide, we’ve walked through the steps to deploy a Node.js or Apollo GraphQL server from an Nx workspace to AWS Lambda.
I hope this tutorial has helped simplify the process for you. Embracing serverless technology means you can focus more on coding and less on managing infrastructure, a true win for any developer.
Now, go ahead and deploy your serverless application with confidence. You’re on your way to harnessing the power of AWS Lambda and the Serverless Framework for your projects. Enjoy the seamless deployment and the scalability it offers.
Happy coding and deploying!