mirror of
https://github.com/serverless/serverless.git
synced 2026-01-25 15:07:39 +00:00
Merge pull request #2326 from serverless/update-handler-files-for-better-onboarding
Update handler files for better onboarding
This commit is contained in:
commit
7a8fe3e9aa
@ -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.
|
||||
|
||||
@ -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 });
|
||||
};
|
||||
|
||||
@ -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
|
||||
}
|
||||
"""
|
||||
|
||||
@ -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 () {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user