Serverless deployment

Now that we have an AWS Lambda function, we will need to attach it to an AWS resource, for which we need to specify the stage, and path properties. Serverless has created a serverless.yml file that contains this required information. Let's take a look at this file, as follows:

service: serverless-sample  
 
provider: 
  name: aws 
  runtime: nodejs8.10 
 
functions: 
  hello: 
    handler: handler.hello 

Here, our .yml, or YAML file, has a number of properties. Note that YAML uses indentation to arrange properties, so that the top-level properties are all at the same indentation, and child properties are indented. This means that the service, provider, and functions properties are all at the same level, and that the provider property has two child properties named name and runtime. Following this indentation pattern, the functions property has a child property named hello, which in turn has a child property named handler.

The properties in this file specify that the service name will be serverless-sample, and that the service provider will be AWS. Also, the runtime engine that will be used for the AWS Lambda function will be NodeJs version 8.10. The functions property provides a name for the Lambda, and finally the handler property specifies the JavaScript file to use, which in this case is named handler, followed by a dot, and then the name of the function that is exported, which is hello.

There are a number of things that seem to be missing from this YAML file, however. We still need to specify the REST endpoint path, as well as the HTML method, which should be GET. We also need to specify the stage and AWS region that this Lambda should be placed in. The documentation for Serverless explains where these properties should be placed, and is well described at docs.serverless.com. Let's go ahead and modify this file as follows:

service: serverless-sample  
 
provider: 
  name: aws 
  runtime: nodejs8.10 
  stage: dev 
  region: us-east-1 
 
functions: 
  hello: 
    handler: handler.hello 
    events: 
      - http: 
          path: test/hello 
          method: get 

Here, we have made two modifications. Firstly, we have added the stage and region properties that will deploy our Lambda function to the dev stage, and the us-east-1 region. We have also added an events property at the same level as the existing handler property, and added an http property with a path property of test/hello, and a method property specifying get. Note how the http property has a leading dash ( - ).

Once these minor changes have been made, we can deploy our Lambda function by executing the following on the command line:

serverless deploy  

Serverless will then package our Lambda function, and deploy it to our AWS account using the user that we configured earlier. If all goes well, Serverless will log to the console the full URL for our newly created Lambda function, as shown in the following screenshot:

Our Lambda function has been deployed successfully, and Serverless has reported the full URL in the console. If we copy and paste this URL value into a browser, we will see that our AWS Lambda function has responded to our GET request, and returned a rather large JSON object, as shown in the following screenshot:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.119.17