Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix lambdas #58

Merged
merged 17 commits into from
Jun 10, 2018
Merged

Fix lambdas #58

merged 17 commits into from
Jun 10, 2018

Conversation

oliverzheng
Copy link
Owner

There were a few things done here to make lambdas happy:

  • Call disconnect() from mongo before returning results. Turns out each function cannot leave lingering connections.
  • Added environment variables to netlify.toml, since that's the only place environment variables are pulled from at runtime. But we don't want to hardcode secrets into the file and commit them, so there's a script to get the build-time environment variables and replaces the placeholders in netlify.toml with actual values. BUT running lambdas in dev also uses those, which means the "PLACEHOLDER" strings actually override dev env variables, so a separate set of environment variables are used for prod.

Other small things:

  • Move S3StorageConfig into env to centralize where env fetching was happening.
  • Added a herro test graphql query to make testing easier.

Go to https://oliverzheng-lambdass--lgtmeme.netlify.com/.netlify/functions/graphql?query={node(id:%20%22bWVtZTo1YjE3MzEzN2Y0NjE5MjBkMDdhYjA5ZTA6NWIxOWNhYWVhNDNiMTU1YzVmZjU3Zjlj%22){id,...on%20Meme{macro}}} and see

{
  "data": {
    "node": {
      "id": "bWVtZTo1YjE3MzEzN2Y0NjE5MjBkMDdhYjA5ZTA6NWIxOWNhYWVhNDNiMTU1YzVmZjU3Zjlj",
      "macro": "wat"
    }
  }
}

@oliverzheng
Copy link
Owner Author

Deploy preview for lgtmeme ready!

Built with commit 8c2b3f1

https://deploy-preview-58--lgtmeme.netlify.com

@oliverzheng
Copy link
Owner Author

The S3 domain issue, I think, is from us using the s3 package, which depends on an older version of the aws-sdk-js package. According to aws/aws-sdk-js#603, 2.6.0 fixed it, but s3 still depends on 2.4.9.

@@ -4,6 +4,9 @@

# The query root for LGTMeme.
type Query {
# A simple ping/pong used for testing
herro: String!
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Running part of an introspection query would achieve pretty much the same thing. Being able to type herro though is pretty easy

{
  __schema {
    types {
      name
    }
  }
}

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

neat_spider-man

path.resolve(process.cwd(), 'src', 'schema.graphql'),
'utf8',
);
// $FlowFixMe
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cool i couldn't figure out how to get that to work

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yeah turns out Netlify compiles one giant JS file, and copies the file to some virtualized environment to run. Nothing else is copied over, so we have to import everything we need at build time.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thats what i thinking we had to do though this is much more elegant than what i had in mind

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm curious what did you have in mind? I tried copying stuff over but Netlify's build process just doesn't care about anything in the directory. It has to be in the JS file. Or I just didn't know what special thing to invoke to make that work.


const connection = await connect();
console.log(`Connected to MongoDB at ${urlWithoutPassword}`); // eslint-disable-line no-console

createApp(connection, s3Storage).listen(
SERVER_PORT,
() => console.log(`Listening on ${SERVER_PORT}.`), // eslint-disable-line no-console
nullthrows(DEBUG_SERVER_PORT),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i thought we had invariant for that

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

invariant doesn't return what you pass into it.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

subtle


// Must disconnect, otherwise a lingering outbound connection makes lambda
// think we aren't done, and not return the request.
await disconnect();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

something to try out later that reuses connections apparently http://mongoosejs.com/docs/lambda.html

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

homersimpson26

@oliverzheng oliverzheng merged commit c9ac356 into master Jun 10, 2018
@oliverzheng oliverzheng deleted the oliverzheng/lambdass branch June 10, 2018 16:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants