Search code examples
aws-lambdagraphqlserverlessaws-serverlessgrandstack

Issues deploying Apollo with GRAND stack using Serverless


I've been developing an app in the GRAND stack starter and after overcoming many hurdles, I finally wanted to deploy it and see it run. My goal is to do it all on Amazon. I have the front-end serving correctly, and a Neo4j instance running on an EC2 instance.

Since the GRAND stack starter was using Apollo, their website suggested using Serverless. The problem I am running into I believe is a lack of information about the required folder structure. The GRAND stack starter has a src folder where the core files live with the package.json living a directory higher.

Serverless says to make a serverless.yml file in the root, which I have one directory higher than source. It's contents are run-of-the-mill:

# serverless.yml
service: apollo-lambda
provider:
  name: aws
  runtime: nodejs6.10
  region: us-west-2
  stage: production
functions:
  graphql:
    # this is formatted as <FILENAME>.<HANDLER>
    handler: graphql.graphqlHandler
    events:
    - http:
        path: graphql
        method: post
        cors: true

Running serverless deploy does succeed in packaging up the files, pushing them to an S3 bucket and adding it to a Lambda. It weighs in at about 10mb. However when I am in AWS Lambda and I attempt to run a test on the Lambda function created, it says "Cannot find module '/var/task/graphql".

It feels like it can't access my node_modules. Some people have had the packaged zip file accidentally have a folder wrap everything, but the apollo-lambda.zip generated by Serverless is not doing that. I can see my root structure just fine.

I've spent many hours the past two days troubleshooting what I thought were first 502, then 403 errors, trying to see where I forgot to do something, until I got to this point. Any help would be appreciated.

Frankly I am just not sure how my folder structure should look when deploying. If I move the serverless.yml within the src folder, it won't have the package.json or the node_modules folder


Solution

  • So I ended up having to rework a bit of my code. The problem which technically answered my question is that I found the handler option in serverless.yml let me path down so I was able to direct the build to look at my file within src/

    The other problem after that however was I had to rework all of my import statements to conts with require statements instead. I couldn't get imports working on Lambda even with updated nodejs versions. There might be a fix for this but I haven't found that quite yet.