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

How to Name Handlers #70

Open
freeformflow opened this issue Apr 27, 2017 · 18 comments
Open

How to Name Handlers #70

freeformflow opened this issue Apr 27, 2017 · 18 comments

Comments

@freeformflow
Copy link
Contributor

freeformflow commented Apr 27, 2017

Right now, we rely on a convention for Lambda names:

appName-environment-resource-HttpVerb

Sky assumes this pattern when it creates and names Lambdas, but that's not enforced in the actual code that constitutes Lambdas. So, within the code, we are relying on convention.

We should solidify our design and then enforce it, both in the CloudFormation template and the Lambda code.

@automatthew
Copy link
Contributor

With the request Mapping Template now transforming the Lambda events into Panda Request Representation, we now have access to the HTTP Method in the event object passed to the Lambda handler.

This opens up some new possibilities that I need to muse on.

@automatthew
Copy link
Contributor

Consider this as a new approach to the Sky Lambda dispatcher:

exports.handle = (request, context, callback) ->
  resource = Blurb.getResource(context)
  methodName = request.method
  resource[methodName](request, context)
  .then (result) ->
    callback null, result
  .catch (e) ->
    callback e

@automatthew
Copy link
Contributor

resource = Blurb.getResource(context) could be handled in at least a couple of ways.

  1. using conventions in context.functionName as at present
  2. determining the API Gateway "resource path" using a Mapping Template and $context.resource_path. This provides the parameterized resource path, e.g. "/request-mapping-test/{key}"

@automatthew
Copy link
Contributor

Currently in the CloudFormation template:

  BlurbGetLambdaHandler:
    Type: 'AWS::Lambda::Function'
    Properties:
      Description: Handler for APIblurb9
      FunctionName: blurb9-staging-blurb-get

@automatthew
Copy link
Contributor

We could simplify by having one Lambda per resource, instead of per (resource X method).

@automatthew
Copy link
Contributor

In API Gateway, we can determine the deployment environment using Mapping Template's $context.stage. This means we don't necessarily need to put it in the Lambda FunctionName, just to get Sky dispatching working. We may still need it as part of namespacing Lambdas themselves.

@automatthew
Copy link
Contributor

@automatthew
Copy link
Contributor

Implication of the above two links is that we may be able use API Gateway stageVariables instead of putting stage names in our function names.

@automatthew
Copy link
Contributor

semi-helpful discussion: https://forums.aws.amazon.com/thread.jspa?messageID=681736

@automatthew
Copy link
Contributor

I have not found any AWS native way to deal with the application namespacing (e.g. "blurb9") though.

@automatthew
Copy link
Contributor

It's risky to jump straight into trusting all the AWS affordances I've just described. Here's an idea for the next safe step:

exports.handle = (request, context, callback) ->
  [_app, _stage, resource] = context.functionName.split('-')
  resource = Blurb9.getResource(resource)
  methodName = request.method
  resource[methodName](request, context)
  .then (result) ->
    callback null, result
  .catch (e) ->
    callback e

@automatthew
Copy link
Contributor

This moves us one---very important---step closer to isomorphism between the AWS concepts and the Sky API concepts.

@automatthew
Copy link
Contributor

automatthew commented Apr 27, 2017

Blurb9 = Sky.API
  resources:
    Blurbs: require "./resources/blurbs"
    Blurb: require "./resources/blurb"

@dyoder
Copy link
Member

dyoder commented Apr 27, 2017

  • One lambda per resources makes sense, given that we're providing helper methods to unpack this anyway.

  • We can also make the callback go away with a helper.

  • Why is the request not part of the context?

@automatthew
Copy link
Contributor

We can also make the callback go away with a helper.

Yeah, in a variety of ways. My code example is just a riff on the current helper.

Why is the request not part of the context?

Because AWS has them as separate (confusing) arguments. In these examples, I'm trying to cleave close to our existing work and the AWS apis, to make it easier to see what's going on.

@automatthew
Copy link
Contributor

Both "API Gateway Mapping Templates" and "Lambda handler functions" use the term context, and it means totally different things. Which is unfortunate and confusing, but eh.

@automatthew
Copy link
Contributor

With the sky.method wrapper I describe elsewhere, we get to provide our own interface to these resource-method functions, so we can easily reorganize the information into a single skyContext if we so desire.

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

No branches or pull requests

3 participants