The hello world serverless function

Write the following code and save it to hello-serverless.py

# The framework is all setup to take in a 
# user defined handler function (hello in this case)
# and pass data along with the event array
def hello(event, context):
print event return event['data']

Now, to deploy it, run the following code:

kubectl create ns serverless
kubeless function deploy hello --runtime python2.7 --from-file hello-serverless.py --handler hello-serverless.hello -n serverless

Let's see what we have done in the preceding code:

  • Line one: This creates a new namespace for the serverless functions.
  • Line two: This deploys a serverless function named hello by using the hello-serverless.py file, with runtime specified as python 2.7 and the handler as the hello function in the serverless namespace.

You can check whether the function is ready by running the following command:

kubeless function ls hello  -n serverless

Now, let's call it by running the following command:

kubeless function call hello --data 'Hello world!' -n serverless

You should get the following output:

Hello world!

You have successfully run your first serverless function.

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

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