From 05ec3e809280093596ee38808298caa16dd98c22 Mon Sep 17 00:00:00 2001 From: Philipp Muens Date: Tue, 11 Oct 2016 19:26:42 -0700 Subject: [PATCH] Update handler files for better onboarding --- docs/01-guide/05-event-sources.md | 34 ------------------- .../create/templates/aws-nodejs/handler.js | 22 ++++-------- .../create/templates/aws-python/handler.py | 17 ++++------ tests/integration_test.js | 4 ++- 4 files changed, 17 insertions(+), 60 deletions(-) diff --git a/docs/01-guide/05-event-sources.md b/docs/01-guide/05-event-sources.md index 62574781a..1832f7c7f 100644 --- a/docs/01-guide/05-event-sources.md +++ b/docs/01-guide/05-event-sources.md @@ -40,40 +40,6 @@ functions: That's it. There's nothing more to do to setup a `http` event. Let's (re)deploy our service so that Serverless will translate this event definition to provider specific syntax and sets it up for us. -## Updating our code - -The `http` event we just added uses the [`LAMBDA-PROXY` integration type](../02-providers/aws/events/01-apigateway.md) -which means that we need to define the `response` we want to send in our functions code. - -Serverless has you covered here and ships with a (yet commented out) callback you can use to send this required response -back to the client. - -Open up the `handler.js` file and remove the callback at the top. Next up comment out the code for response sending. - -Your `handler.js` file should now look like this: - -```javascript -// Your first function handler -module.exports.hello = (event, context, callback) => { - const body = { - message: 'Go Serverless v1.0! Your function executed successfully!', - input: event, - }; - - const response = { - statusCode: 200, - headers: { - 'custom-header': 'Custom header value', - }, - body: JSON.stringify(body), - }; - - callback(null, response); -}; -``` - -Great we're all set for a (re)deployment to update our service. - ## (Re)deploying We can redeploy our updated service by simply running `serverless deploy` again. diff --git a/lib/plugins/create/templates/aws-nodejs/handler.js b/lib/plugins/create/templates/aws-nodejs/handler.js index b5e131d35..770e6415d 100644 --- a/lib/plugins/create/templates/aws-nodejs/handler.js +++ b/lib/plugins/create/templates/aws-nodejs/handler.js @@ -1,24 +1,16 @@ 'use strict'; -// Your first function handler module.exports.hello = (event, context, callback) => { - callback(null, { message: 'Go Serverless v1.0! Your function executed successfully!', event }); - - // Use this code if you're using the HTTP LAMBDA-PROXY integration - /* - const body = { - message: 'Go Serverless v1.0! Your function executed successfully!', - input: event, - }; - const response = { statusCode: 200, - headers: { - 'custom-header': 'Custom header value', - }, - body: JSON.stringify(body), + body: JSON.stringify({ + message: 'Go Serverless v1.0! Your function executed successfully!', + input: event, + }), }; callback(null, response); - */ + + // Use this code if you don't use the http event with the LAMBDA-PROXY integration + // callback(null, { message: 'Go Serverless v1.0! Your function executed successfully!', event }); }; diff --git a/lib/plugins/create/templates/aws-python/handler.py b/lib/plugins/create/templates/aws-python/handler.py index 7df25a919..ea831b279 100644 --- a/lib/plugins/create/templates/aws-python/handler.py +++ b/lib/plugins/create/templates/aws-python/handler.py @@ -1,13 +1,6 @@ import json def hello(event, context): - return { - "message": "Go Serverless v1.0! Your function executed successfully!", - "event": event - } - - # Use this code if you're using the HTTP LAMBDA-PROXY integration - """ body = { "message": "Go Serverless v1.0! Your function executed successfully!", "input": event @@ -15,11 +8,15 @@ def hello(event, context): response = { "statusCode": 200, - "headers": { - "custom-header": "Custom header value" - }, "body": json.dumps(body) }; return response + + # Use this code if you don't use the http event with the LAMBDA-PROXY integration + """ + return { + "message": "Go Serverless v1.0! Your function executed successfully!", + "event": event + } """ diff --git a/tests/integration_test.js b/tests/integration_test.js index 3365c4941..a83c8d27d 100644 --- a/tests/integration_test.js +++ b/tests/integration_test.js @@ -47,7 +47,9 @@ describe('Service Lifecyle Integration Test', () => { this.timeout(0); const invoked = execSync(`${serverlessExec} invoke --function hello --noGreeting true`); const result = JSON.parse(new Buffer(invoked, 'base64').toString()); - expect(result.message).to.be.equal('Go Serverless v1.0! Your function executed successfully!'); + // parse it once again because the body is stringified to be LAMBDA-PROXY ready + const message = JSON.parse(result.body).message; + expect(message).to.be.equal('Go Serverless v1.0! Your function executed successfully!'); }); it('should deploy updated service to aws', function () {