user12688644
user12688644

Reputation:

Is it possible to deploy apollo-server-express to lambda?

I have a backend project implemented with apollo-server-express. I researched how to deploy this to lambda and found that I need to use apollo-server-lambda for this.

I am not familiar with GraphQL and AWS lambda. I don't know the differences between these 2 packages - apollo-server-express and apollo-server-lambda.

Is there any easy way to convert apollo-server-express to apollo-server-lambda?

Or Is it possible to deploy this implementation directly to AWS lambda?

I tried to deploy this project with serverless as it is a simple express app but it fails with 6 seconds timeout.

Upvotes: 6

Views: 977

Answers (1)

aadibajpai
aadibajpai

Reputation: 356

I faced the same problem since I had written a graphql server using apollo-server-express which I had to deploy it to aws lambda so needed to make it use apollo-server-lambda.

Ever since Apollo 3, apollo-server-lambda uses apollo-server-express under the hood.

So instead of writing a graphql server from scratch using apollo-server-lambda, you can just import ApolloServer from it and then supply your existing express app to it with all the middleware as detailed here: https://www.apollographql.com/docs/apollo-server/deployment/lambda/#customizing-http-serving

Then you'll need to modify the context to read the req value from the express parameter, instead of directly reading req as it was in apollo-server-express. You can follow this for it: https://www.apollographql.com/docs/apollo-server/deployment/lambda/#getting-request-info

The last thing that you would need to do in case you were using some database is to make it reuse the connection in aws lambda, but that's not strictly related to apollo-server.

In case it helps, you can look at how I did it here, all the code from express is commented out so the difference is visible.

Upvotes: 2

Related Questions