×
Community Blog Building Serverless Applications with Alibaba Cloud Function Compute

Building Serverless Applications with Alibaba Cloud Function Compute

This guide walks you through Function Compute, your first function, triggers, integrations, and production optimization.

Introduction

Serverless computing has revolutionized how developers think about deploying and scaling applications. With Alibaba Cloud Function Compute, you write code as small, event-driven functions and let Alibaba handle all the infrastructure—no server setup, patching, or worrying about scalability. This guide will walk you through the essentials: what Function Compute is, how to create your first function, connect triggers, integrate with other cloud resources, and optimize your architecture for production.

1

What is Function Compute?

Function Compute lets you execute code in response to events:

● HTTP requests (APIs)

● OSS bucket uploads or deletes

● Timer/Cron schedules (every hour, every day, etc.)

● Message queue/events

You upload code (Python, Node.js, Java, and more), configure triggers, and Alibaba Cloud automatically handles provisioning, scaling, and monitoring. You only pay for the milliseconds your code actually runs.

Step 1: Setting Up Your Environment

Install Serverless Devs CLI

npm install -g @serverless-devs/s
s config add  # Add your Alibaba Cloud credentials

Initialize a New Function Project

s init devsapp/start-fc-http-python3
cd start-fc-http-python3

Step 2: Writing Your Function

Basic HTTP API Example

def handler(event, context):
    return {
        'statusCode': 200,
        'headers': {'Content-Type': 'application/json'},
        'body': '{"message": "Hello from Alibaba Cloud Function Compute!"}'
    }

This simple function responds to any HTTP request with a JSON greeting.

Step 3: Connect Triggers

OSS Trigger Example

Have your function respond when files are uploaded to OSS:

def handler(event, context):
    # For each file event
    for ev in event['events']:
        bucket = ev['oss']['bucket']['name']
        key = ev['oss']['object']['key']
        print(f"Received file {key} in bucket {bucket}")

Great for auto-processing images, PDFs, or backups.

Timer Trigger Example

Run code on a schedule (e.g., analytics every hour):

def handler(event, context):
    import datetime
    print("Function triggered at", datetime.datetime.now())

Step 4: Integrate with Other Alibaba Cloud Services

Database Write Example (RDS/MySQL)

import pymysql
def save(data):
    conn = pymysql.connect(host='host', user='user', password='pwd', db='mydb')
    with conn.cursor() as cursor:
        cursor.execute("INSERT INTO customers (name,email) VALUES (%s,%s)", (data['name'], data['email']))
    conn.commit()

Use environment variables for connection settings and secrets:

import os
db_host = os.environ.get('DB_HOST')

Step 5: Logging and Error Handling

Good logging improves debugging and monitoring.

import logging
def handler(event, context):
    logger = logging.getLogger()
    try:
        # Main logic
        pass
    except Exception as e:
        logger.error("Error: " + str(e))
        return {'statusCode': 500, 'body': '{"error":"Internal error"}'}

Step 6: Deploy, Monitor, and Optimize

Deploy from CLI

s deploy

Monitor invocations, latency, and errors in the Function Compute console.

Key Tips:

● Size functions (memory/time) for your workload, not too big or small.

● Use environment variables for configuration.

● Add alerting for errors or high latency.

● Secure triggers—e.g., API authentication.

Real-World Example

A company wants to process images uploaded by users:

  1. Actor uploads a photo to OSS.
  2. OSS triggers Function Compute, which creates a thumbnail and saves it back to OSS.
  3. Another function runs on a timer each night to clean up unused files or generate reports.

Serverless makes such orchestration easy, massively scalable, and affordable—because you only pay when your code runs.

Conclusion

Alibaba Cloud Function Compute lets you quickly build APIs, data pipelines, scheduled jobs, and integrations that scale to millions of events per day without managing infrastructure.


Disclaimer: The views expressed herein are for reference only and don't necessarily represent the official views of Alibaba Cloud.

0 1 0
Share on

Farah Abdou

17 posts | 0 followers

You may also like

Comments

Farah Abdou

17 posts | 0 followers

Related Products