Example 3 – the Serverless deployment tool 

Serverless is widely used by enterprises working in this domain. We have provided examples of both Apex and Serverless for your reference. Make sure Serverless is installed, and then install the Serverless command-line. We can then call it using Serverless or using the alias name of the sls command.

There are two important files that you have to configure while working with the Serverless command:

  • serverless.yml: This contains details about how to deploy the serverless function. This file is used by the sls/serverless command. 
  • handler.py: This file contains the actual function code. Your function will be defined here. 

The sls command can be used as a shortcut for serverless as shown here:

$ sls

The following steps demonstrate how to create and deploy a service using the Serverless tool:

  1. Create a service: This command is used to create a service template so that we can write our functionality. With the sls create command, we can specify aws-python, among other available templates. In this example, we are going to use aws-python with the  --template or shorthand -t  flag. The --path or shorthand -p is used to update the path where the template service file will be created:
# sls create --template aws-python --path my-service
  1. Deploy the function: The following code will deploy the function to AWS Lambda based on the configuration that you created in the serverless.yml file:
# sls deploy
  1. Invoke the deployed function: The sls  invoke command is used run the function. The -- function or the shorthand -f can be used to mention the function name:
# sls invoke -f hello

In your Terminal window, you should see the following response from AWS Lambda:

{    
"status Code": 200,
"body": "{"message":"Go Serverless v1.0! Your function executed successfully!","input":{}}"
}

Congratulations! You have now deployed and run your first Hello World function!

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

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