Deploy a flexible and highly available image processing service within 10 minutes

简介: Alibaba Cloud Function Compute is an event-driven and fully-managed compute service. With Function Compute, you can quickly build any type of applicat.

Prefaces

Alibaba Cloud Function Compute is an event-driven and fully-managed compute service. With Function Compute, you can quickly build any type of applications or services without considering management or O&M. You can complete a set of backend services for processing multimedia data even in several days. In this tutorial,we will deploy a flexible and highly avaliable image processing service in 10 minutes with FC.

Image processing service example

Take the classic lena in image processing for example:
image

The entry of image processing example:

Effect Picture:

image

image

Function Entry

We use python runtime to complete this tutorial. Let's look at a few concepts before we get started.

common handlers

def my_handler(event, context):
    return 'hello world'
  • Function name

The function name must match the “handler” field provided when a function is created. For example, if handler is specified as main.my_handler, Function Compute obtains the my_handler function defined in main.py.

  • Parameter event

The event parameter is defined when you call a function. In Python 2.7, this parameter is of str type. In Python 3, it is of bytes type. The parameter is an input parameter.

  • Parameter context

The context parameter contains information generated when the function is executed, for example, the request ID and temporary AccessKey credentials. You can use the generated information when coding. This parameter is of the FCContext type.

  • Return value

The return value of a function can be returned to the users as the result of invoke function. It can be any type. Function Compute can return it as str for simple type; and it can be returned as JSON str for complex type.

Handlers with an HTTP trigger

HELLO_WORLD = b"Hello world!\n"
def handler(environ, start_response):
    context = environ['fc.context']
    status = '200 OK'
    response_headers = [('Content-type', 'text/plain')]
    start_response(status, response_headers)
    return [HELLO_WORLD]
  • environ : This parameter represents a Python dictionary that stores all client-related information. For more information, see Parameter environ,Two user-defined keys, fc.context and fc.request_uri, are added to Function Compute.

    • fc.context : functions as the context parameter of common handlers.
    • fc.request_uri : indicates the URL of the request. This parameter is a string.

Note: The HTTP_Variables field of environ contains the request header. For example, if the request header is 'x-Custom-key':'value', the environ parameter will be environ['HTTP_X_CUSTOM_KEY'] ='value'. According to WSGI, the key in the request header is processed as follows: key = "HTTP_" + k.upper().replace("-","_").

  • start_response: The start_response callable is provided in runtime environments of Function Compute. It has two required positional parameters and one optional parameter. For more information, see the-start-response-callable. In the following example, the three parameters are named status, response_headers, and exc_info. You can use other names as needed.

More details can be found at python handler

Specific steps

Assuming that all the operations are finished in Shenzhen(China south 1) region, all the relevant resources can be download in the attachment.

There are two types of deployment:

  1. Use fun tools for automatic deployment
  2. Use fc console for visual deployment

Preparations

  • Preparing pictures to the specified oss bucket

For example, build a bucket named xiamen-ws in Shenzhen(China south 1) region, where store the four pictures(you can casually name the bucket)

image

Use the fun tool for automatic deployment

Fun is a development tool for serverless applications. It can help you to efficiently arrange cloud resources such as Function Compute, API Gateway, Log Service and so on. You can use it to develop,build and deploy FC by describing relative resources in a template.yml file. The most basic serverless application can have only one function.

Take this case as example, the yaml file can be defined as follows:

ROSTemplateFormatVersion: '2015-09-01'
Transform: 'Aliyun::Serverless-2018-04-03'
Resources:
  image-demo-pro:
    Type: 'Aliyun::Serverless::Log'
    Properties:
      Description: 'image process log pro'
    fc-log:
      Type: 'Aliyun::Serverless::Log::Logstore'
      Properties:
        TTL: 362
        ShardCount: 1 
  
  image-service:
    Type: 'Aliyun::Serverless::Service'
    Properties:
      Description: 'image process demo'
      Policies:
        - AliyunOSSFullAccess
      LogConfig:
        Project: 'image-demo-pro'
        Logstore: 'fc-log'
    image-proc:
      Type: 'Aliyun::Serverless::Function'
      Properties:
        Handler: main.handler
        CodeUri: './'
        Description: 'image-process http function'
        Runtime: python2.7
        Timeout: 60
        MemorySize: 512
      Events:
        http-trigger:
          Type: HTTP
          Properties:
            AuthType: ANONYMOUS
            Methods: ['GET', 'POST', 'PUT']

Using the yaml file defined above, you can do the following:

  1. Create log resources: logproject:image-demo-pro, logstore:fc-log
  2. Create service: image-service and function: image-proc, configurating function with a trigger named http-trigger.
  3. Configurate service role and logconfig, role permission is AliyunOSSFullAccess and the authority of function execution log store into fc-log.

Specific deployment operation

  • Install nodejs (version>8)
  • Install fun
  npm install git://github.com/aliyun/fun.git --save -g
  • Modify the name of logproject and the related configuration in template.yml. After fun deploy has been executed successfully, you may see the following resources being crested, screenshot:

image
image

FC Console setup

Create service/function, and configurate http trigger

  • Create a new service, configurate service with the role of oss permission

image

image

  • Create function, and configurate Http trigger

image

image

image

image

Summary

Function compute has advantages as follows:

  • No infrastructure - No need to purchase and manage server
  • Focus on Business - Focusing on the development of business logic can greatly improve development efficiency
  • Traceable - Provide log query, performance monitoring, alarm and other functions to quickly troubleshoot.
  • Stable and Reliable - Stable , Highly availability, millisecond-level elastic scaling to cope up with peak demand pressure.
  • Flexible Pricing - Pay-as-you-go, You only pay for the time your code runs. Function Compute is suitable for high traffic-fluctuation scenarios

The official website of Function Compute

相关实践学习
【AI破次元壁合照】少年白马醉春风,函数计算一键部署AI绘画平台
本次实验基于阿里云函数计算产品能力开发AI绘画平台,可让您实现“破次元壁”与角色合照,为角色换背景效果,用AI绘图技术绘出属于自己的少年江湖。
从 0 入门函数计算
在函数计算的架构中,开发者只需要编写业务代码,并监控业务运行情况就可以了。这将开发者从繁重的运维工作中解放出来,将精力投入到更有意义的开发任务上。
目录
相关文章
|
存储 监控 搜索推荐
在生产环境中部署Elasticsearch:最佳实践和故障排除技巧——安装篇(一)
在生产环境中部署Elasticsearch:最佳实践和故障排除技巧——安装篇(一)
|
负载均衡 Java 微服务
深入理解Spring Cloud中的服务发现与注册
深入理解Spring Cloud中的服务发现与注册
|
人工智能 安全 搜索推荐
智能家居的未来:从自动化到人工智能
本文探讨了智能家居技术的发展趋势,特别是如何从简单的家居自动化向集成人工智能的方向发展。我们将分析当前市场上流行的智能家居产品,并预测人工智能如何塑造家居生活的未来。通过实际案例研究,本文旨在揭示技术革新如何提高居家生活的效率、安全和舒适度。
|
运维 监控 安全
软件研发核心问题之用在需求拆解时明确监控范围与形式的问题如何解决
软件研发核心问题之用在需求拆解时明确监控范围与形式的问题如何解决
|
缓存 测试技术 调度
双十一弹性能力支撑--ECI稳定性建设
本文我们将为大家介绍,ECI这些年在稳定性方面做了哪些工作,以及是如何来为集团双十一保驾护航的。
130862 49
|
数据可视化 IDE Java
PlanUML和Mermaid哪个好?
PlanUML和Mermaid哪个好?
4022 0
|
存储 关系型数据库 数据库
PostgreSQL孤儿文件
与所有其他关系数据库系统一样,PostgreSQL需要通过写入wal日志或在Checkpoint时同步数据到数据文件来持久化数据到磁盘上。对于数据文件,一旦Relation达到SEGMENT_SIZE(默认1GB),PostgreSQL就会创建一个新的数据文件。因此如果Relation持续增长,则该Relation可能会由多个文件组成。在这篇文章中想要考虑的问题是,是否可能存在孤儿文件。
|
Java Spring
Command line is too long. Shorten command line for Application or also for Spring Boot default confi
Command line is too long. Shorten command line for Application or also for Spring Boot default confi
385 1
|
编解码 计算机视觉 C++
OpenCV 打开双目摄像头(python版)
本文主要介绍在OpenCV用使用双目摄像头,包括:打开单目摄像头、设置摄像头参数、拍照、录制视频。
1271 0
|
存储 数据可视化 大数据
LINUX入门篇【一】
LINUX入门篇【一】
139 0