Nick Chisiu 16aa6581ab fix #1854 - update npm devDependencies packages (#1879)
* fix #1854 - added latest version of eslint-config-airbnb-base package

* fix #1854 - added latest npm shrinkwrap scheme

* fix #1854 - added latest npm coveralls package

* fix #1854 - added latest npm eslint package

* fix #1854 - added latest npm eslint-config-airbnb package

* fix #1854 - added latest npm eslint-plugin-import package

* fix #1854 - added latest npm eslint-plugin-jsx-a11y package

* fix #1854 - added latest npm eslint-plugin-react package

* fix #1854 - fixed estlint new-parens errors for integration test

* fix #1854 - fixed estlint new-parens errors for yaml parsers tests

* fix #1854 - fixed estlint max-len errors for util tests

* fix #1854 - disabled no-extraneous-dependencies on eslintrc for NodeJS 4 incapability on this feature

* fix #1854 - fixed eslint new-parens errors for Service tests

* fix #1854 - fixed eslint new-parens errors for Serverless tests

* fix #1854 - fixed eslint new-parens errors for plugin manager tests

* fix #1854 - fixed eslint new-parens errors for plugin tracking tests

* fix #1854 - fixed eslint new-parens errors for plugin package zipService lib

* fix #1854 - fixed eslint new-parens errors for plugin package zipService tests

* fix #1854 - fixed eslint trailing spaces errors for plugin package zipService lib

* fix #1854 - fixed eslint new-parens errors for plugin package cleanup  tests

* fix #1854 - fixed eslint new-parens errors for plugin create tests

* fix #1854 - fixed eslint dot same line expectation error on plugin aws logs index

* fix #1854 - fixed eslint operator assignment error on plugin aws logs index

* fix #1854 - fixed eslint dot location error on plugin aws invoke tests

* fix #1854 - fixed eslint new-parens error on plugin aws invoke tests

* fix #1854 - fixed eslint new-parens error on plugin aws deployFunction tests

* fix #1854 - fixed eslint new-parens error on plugin aws deploy uploadDeploymentPackage tests

* fix #1854 - fixed eslint new-parens error on plugin aws deploy updateStack tests

* fix #1854 - fixed eslint new-parens error on plugin aws deploy createStack tests

* fix #1854 - fixed eslint new-parens error on plugin aws deploy apiGateway lib deployment

* fix #1854 - fixed eslint unary typeof whitespace req error on Serverless Service class

* fix #1854 - fixed eslint unary typeof whitespace req error on Serverless Service class ( second fix )

* fix #1854 - fixed eslint no-lonely-if req error on Serverless Service class

* fix #1854 - disabled react/require-extension on eslintrc because it's deprecated

* fix #1854 - AwsCompileApigEvents #constructor() should resolve if no functions are given:
                 Error: Resolution method is overspecified. Specify a callback *or* return a Promise; not both.

* fix #1854 - createStack #postCreate() should resolve:
                 Error: Resolution method is overspecified. Specify a callback *or* return a Promise; not both.

* fix #1854 - emptyS3Bucket #deleteObjects() should resolve if objectsInBucket is empty:
                 Error: Resolution method is overspecified. Specify a callback *or* return a Promise; not both.

* fix #1854 - AwsInvoke #extendedValidate() should resolve if path is not given:
                 Error: Resolution method is overspecified. Specify a callback *or* return a Promise; not both.

* fix #1854 - #cleanup() should resolve if the .serverless directory is not present:
                 Error: Resolution method is overspecified. Specify a callback *or* return a Promise; not both.

* fix #1854 -#validate() should resolve if servicePath is given:
                Error: Resolution method is overspecified. Specify a callback *or* return a Promise; not both.

* fix #1854 -  Service #load() should resolve if no servicePath is found:
                 Error: Resolution method is overspecified. Specify a callback *or* return a Promise; not both.

* fix #1854 - added latest mocha package

* fix #1854 - added latest sinon npm package

* fix #1854 - serverless/lib/plugins/aws/deploy/tests/createStack.js 136:48  error  Missing semicolon  semi

* fix #1854 - serverless/lib/plugins/package/tests/cleanup.js 35:7  error  Missing semicolon  semi

* fix #1854 - serverless/lib/plugins/package/tests/validate.js 22:49  error  Missing semicolon  semi

* fix #1854 - added latest npm shrinkwrap

* fix #1854 - fixed no-extra-boolean-cast eslint error on aws deploy apiGateway methods

* fix #1854 - fixed  new-parens eslint error on serverless tests for Service class
2016-08-18 11:51:09 +02:00
..
2016-08-05 13:30:35 +02:00

Compile Api Gateway Events

This plugins compiles the functions HTTP endpoint definitions to valid API Gateway CloudFormation resources.

How it works

Compile Api Gateway Events hooks into the deploy:compileEvents lifecycle.

It loops over all functions which are defined in serverless.yml. For each function that has a http event defined, an API Gateway REST API will be created.

Furthermore a lambda permission for the current function is created which makes is possible to invoke the function when the endpoint is accessed.

Take a look at the Event syntax examples below to see how you can setup HTTP events.

Those resources are then merged into the compiled CloudFormation template.

Universal JSON request template

The API Gateway plugin implements a request template which provides {body, method, principalId, stage, headers, query, path, identity, stageVariables} = event as JavaScript objects. This way you don't have to define the template on your own but can use this default template to access the necessary variables in your code.

Event syntax examples

Simple http setup

This setup specifies that the index function should be run when someone accesses the API gateway at users/index via a GET request.

# serverless.yml
functions:
  index:
    handler: users.index
    events:
      - http: GET users/index

Http setup with extended event options

Here we've defined an POST endpoint for the path posts/create.

# serverless.yml
functions:
  create:
    handler: posts.create
    events:
      - http:
          path: posts/create
          method: post

Http setup with custom authorizer

You can enable custom authorizers for your HTTP endpoint by setting the authorizer in your http event to another function in the same service, as shown in the following example

# serverless.yml
functions:
  create:
    handler: posts.create
    events:
      - http:
          path: posts/create
          method: post
          authorizer: authorizerFunc
  authorizerFunc:
    handler: handlers.authorizerFunc

Or, if you want to configure the authorizer with more options, you can turn the authorizer property into an object as shown in the following example:

# serverless.yml
functions:
  create:
    handler: posts.create
    events:
      - http:
          path: posts/create
          method: post
          authorizer:
            name: authorizerFunc
            resultTtlInSeconds: 0
            identitySource: method.request.header.Auth
            identityValidationExpression: someRegex
  authorizerFunc:
    handler: handlers.authorizerFunc

Http setup with custom authorizer (via ARN)

If the authorizer function does not exist in your service but exists in AWS, you can provide the ARN of the Lambda function instead of the function name, as shown in the following example:

# serverless.yml
functions:
  create:
    handler: posts.create
    events:
      - http:
          path: posts/create
          method: post
          authorizer: xxx:xxx:Lambda-Name

Or, if you want to configure the authorizer with more options, you can turn the authorizer property into an object as shown in the following example:

# serverless.yml
functions:
  create:
    handler: posts.create
    events:
      - http:
          path: posts/create
          method: post
          authorizer:
            arn: xxx:xxx:Lambda-Name
            resultTtlInSeconds: 0
            identitySource: method.request.header.Auth
            identityValidationExpression: someRegex

Setting API keys for your Rest API

You can specify a list of API keys to be used by your service Rest API by adding an apiKeys array property to the provider object in serverless.yml. You'll also need to explicitly specify which endpoints are private and require one of the api keys to be included in the request by adding a private boolean property to the http event object you want to set as private.

Here's an example configuration for setting API keys for your service Rest API:

service: my-service
provider:
  name: aws
  apiKeys:
    - myFirstKey
    - ${mySecondSecretKey} # you can hide it in a serverless variable
functions:
  hello:
  events:
    - http:
        path: user/create
        method: get
        private: true

Clients connecting to this Rest API will then need to set any of these API keys in the x-api-key header of their request. That wouldn't be required if you hadn't set the private property to true.

Setting an HTTP proxy on API Gateway

Setting an API Gateway proxy can easily be done by adding two custom CloudFormation resource templates to your serverless.yml file. Check this guide for more info on how to set up a proxy using custom CloudFormation resources in serverless.yml.

Enabling CORS for your endpoints

To set CORS configurations for your HTTP endpoints, simply modify your event configurations as follows:

functions:
  hello:
    handler: handler.hello
    events:
      - http:
          path: user/create
          method: get
          cors: true

You can equally set your own attributes:

functions:
  hello:
    handler: handler.hello
    events:
      - http:
          path: user/create
          method: get
          cors:
            origins:
              - '*'
            headers:
              - Content-Type
              - X-Amz-Date
              - Authorization
              - X-Api-Key
              - X-Amz-Security-Token

This example is the default setting and is exactly the same as the previous example. The Access-Control-Allow-Methods header is set automatically, based on the endpoints specified in your service configuration with CORS enabled.