13
votes

AWS noob here. This is a bit of a 'best practice development question' - but I'll try to be specific:

I know I can test Lambdas in node locally with a json file for input, but I'm not sure the best way to 'integration test' my web app, which will run in S3 and call the Lambdas in response to API Gateway endpoint calls.

It would be wonderful to use my actual API Gateway configuration files to enable me to call the same methods locally and execute my Lambdas locally. This article suggests that's not possible, yet as there's no local API gateway test environment: http://nickmchardy.com/blog/2015/09/my-thoughts-about-aws-api-gateway-working-with-aws-lambda

Perhaps this isn't clear, an example: if I have a users/ method in API Gateway, with GET and POST, which are connected to listUsers.js and createUser.js Lambdas respectively in AWS; in development I'd like to call: http://localhost:0000/users GET/POST and have it run the Lambdas and give the correct responses, so I can run my whole architecture locally.

Obviously calling a thing at a url isn't particularly complicated, so my question is more: what's the best practice for doing this considering I will have my (Cloudfront!? JAWS!?) configuration for how these things link together in AWS, can I make use of it for local testing at all?

Apologies if this is either not clear or very obvious!

7

7 Answers

16
votes

Oh. The answer is apparently 'Serverless' (formerly JAWS): https://github.com/serverless/serverless . Needed to keep Googling, apologies! Not sure whether to delete this question, now?! I guess it could be helpful for others? Especially as Serverless is new?

6
votes

Currently, AWS API Gateway and AWS Lambda don't have local testing version, but we will consider as a feature request.

4
votes

Our suite (Bespoken Tools) will allow you to do exactly this. This tutorial explains how: http://docs.bespoken.tools/en/latest/tutorials/tutorial_lambda_local/

The short summary is:
1) Install bespoken tools
npm install bespoken-tools -g
2) Start a proxy with your Lambda
bst proxy lamdba index.js
3) Take the URL that it prints out and use that in the API Gateway
Configure the API Gateway as an HTTP integration, and use the URL that is printed out when you start the proxy in Step 2.

Hope that helps! Once you have it setup, calls to your API Gateway will come directly to the Lambda running locally on your laptop.

2
votes

Recently, I've been using ClaudiaJS to help me on AWS Lambda development. The creator also build this library called claudia-api-builder which will help you aorund build AWS API Gateway that trigger lambda event. Within this framework you can test the endpoint in your test class or else by creating something like

var apiRequest = {
  context: {
    path: '/task',
    method: 'GET'
  },
  queryString: {
    name: 'mike'
  }
};
// This method will simulate your api gateway endpoint
api.router(apiRequest, {done: function(err, data){
  console.log(data);
}});

Eventhough this way not really easy to understand, at least it helps me for unit testing my lambda api through api gateway.

2
votes

You can also use aws-sam-local First you have to install docker. Next, follow these steps:

$ npm install -g aws-sam-local
$ cd path/to/your/code
$ sam local start-api

The CLI will map your API function to a localhost URL. Now you can use cURL, Postman, or your browser to hit the API end-point.

1
votes

There is no way to run the API gateway locally, unfortunately.

I'm not sure if there is a best practice, but what we do is have multiple copies of the same API in the API gateway: a dev, staging and live API. We did not choose to use stages as the endpoints would be connected to the same lambda function and versioning was really difficult (it is somewhat better now). Be creating separate APIs and separate Lambda functions it is quite easy to have three setups ready. Using a few shell scripts we built we can deploy to any stage.

The drawback is that you can only develop while being online, but for us this is not really a downside.

1
votes

I suggest you do the following: 1.When you write a lambda make sure that it doesn't know what called it (http for example) but rather it knows all it needs to know from the event json.

2.If you have done "1" then use "grunt" :https://www.npmjs.com/package/grunt-aws-lambda for testing your Lambda with custom "event" files. This will allow you to unitest your Lambda separated from the aws api gateway.

3.use npm test to build an E2E test with http request's to check that your body manipulation is done properly .