Lambda function runthrough

In this section, we will edit the Lambda function to return a different result.

But first, let’s go and look at the Lambda function.

  1. In Cloud9, open the api/hello-world/app.js file.
  2. Line 17 - exports.lambdaHandler function. This is the function that runs when it is triggered by the API request.
    • The event parameter contains the event information passed by the caller. In this case, because the trigger is API Gateway request, it will contain items like header, body etc.
    • The context parameter contains the information about the lambda function itself. For example, you can get the execution time of the Lambda function, CloudWatch log stream name etc.
  3. Line 32 - returns the response. In this case, because the request is from API Gateway, we will return a message with a HTTP 200 status and a message.

Lambda Function

Now that we know the skeleton of the Lambda function, let’s go and change the return message.

  1. Go to api/hello-world/app.js file, change the return message from hello world to hello serverless friends, press Ctrl+S (or Cmd+S on a Mac) to save the file.
  2. In the terminal tab, run sam build. This will build the serverless application to make it ready for deployment.
  3. Run sam deploy. This deploys the solution into your environment.
  4. Click the link in the output section and select Open from the dropdown menu. This will open a browser with the body {“message”:“hello serverless friends”}.