Using Serverless Devs to run a “Hello World” with Alibaba Cloud

- 5 mins read

As you know, I’ve been talking and developing production-ready solutions in a Serverless fashion since 2018, which sounds “forever” in this IT world. In the beginning, I used the amazing tool called fun developed by Alibaba to deploy them, and this made my life easier since then.

Apparently, Alibaba deprecated fun in favour of another tool called serverless-devs, which has a more “multi-cloud” approach and is gaining a lot of attention in GitHub.

Using Serverless Devs to run a “Hello World” with Alibaba Cloud

According to them, Serverless Devs is an open source serverless platform aimed to provide developers with a powerful framework. Through this platform, developers can not only experience multi-cloud serverless products with one click, deploy serverless projects at an extreme speed, but also manage the entire lifecycle of serverless projects. Also, combining Serverless Devs with other tools is very simple and quick and helps further improve R&D, operation and maintenance efficiency.

Using Serverless Devs to run a “Hello World” with Alibaba Cloud

The tool itself follows the principles of SDM (Serverless Devs Model), which is a vector-agnostic, serverless architecture. It defines common usage standards, allowing developers to focus more on business logic and improving serverless application development, deployment, operation and maintenance. Through this model, developers can use different cloud vendors and open source serverless products in a more flexible and general way, and then implement serverless application management more efficiently, concisely, and conveniently.

Supported Resources by Serverless Devs

At this moment, the FaaS platforms/products currently supported by the Serverless Devs project are:

  • Alibaba Cloud Function Compute (FC)
  • AWS Lambda
  • Baidu Cloud Function Compute (CFC)
  • HUAWEI CLOUD Function Graph (FG)
  • Tencent Serverless Cloud Function (SCF)

Ok, lets go with the “Hello World”

This is supposed to be a “Hello World”, so I’ll try to make this example as little as possible. Remember, this is available in my GitHub for free, so you can download it.

The folder structure is:

serverless-devs-helloworld
├── code
│   ├── index.py
└── s.yml

As you can see, there is a main s.yml file with the definition of the project. Then, a folder called code holds the source code of our example in a file named index.py because our example will be made in Python.

File “s.yml”

edition: 1.0.0
name: sdevs-helloworld
access: default

services:
  helloworld-service:
    component: fc
    props:
      region: cn-shanghai
      service:
        name: helloworld-service
        description: 'Python Hello World Service'
      function:
        name: helloworld-function
        description: 'Python Hello World Function'
        codeUri: './code'
        timeout: 60
        memorySize: 256
        runtime: python3
      triggers:
        - name: httpTrigger
          type: http
          config:
            authType: anonymous
            methods:
              - GET

File “code/index.py”

import logging
HELLO_WORLD = b'Hola Mundo!\n'

def handler(environ, start_response):
    context = environ['fc.context']
    request_uri = environ['fc.request_uri']
    for k, v in environ.items():
      if k.startswith('HTTP_'):
        pass
    status = '200 OK'
    response_headers = [('Content-type', 'text/html')]
    start_response(status, response_headers)
    return [HELLO_WORLD]

Installing Serverless Devs

Once we have the above project structure ready, follow the next steps:

  • Install Node.js (>=10.8.0) and NPM package management tools
  • Install Serverless Devs by running npm install @serverless-devs/s -g
  • Check if Serverless Devs is working properly by running s -v

After installing, you should run s config add to start the setup, choose “Alibaba Cloud (alibaba)” and follow the instructions to put your Alibaba Cloud credentials.

Deployment

Once we have everything ready, let’s rock. This, as the last step, is the easiest of all of them.

We just need to run s deploy and focus on the output, as we will have all the information we need in order to test if our code is running successfully.

Thank you for reading all the way to this point, enjoy serverlessing!


Share: Link copied to clipboard

Tags:

Previous: Thailand 4.0
Next: Alibaba Cloud Elasticsearch Tutorial & Optimisation

Where: Home > Technical > Using Serverless Devs to run a “Hello World” with Alibaba Cloud