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

相关实践学习
【玩转ComfyUI】基于函数计算一键部署AI生图平台ComfyUI
本次实验将带大家通过使用阿里云产品函数计算FC,快速使用ComfyUI实现更高质量的图像生成。
从 0 入门函数计算
在函数计算的架构中,开发者只需要编写业务代码,并监控业务运行情况就可以了。这将开发者从繁重的运维工作中解放出来,将精力投入到更有意义的开发任务上。
目录
相关文章
|
存储 算法 安全
密钥密码学(一)(2)
密钥密码学(一)
785 1
|
敏捷开发 编解码 安全
测试面试题集锦(一)| 软件测试常见必考问题与流程篇(附答案)
本系列文章总结归纳了一些软件测试工程师常见的面试题,主要来源于个人面试遇到的、网络搜集(完善)、工作日常讨论等,分为以下十个部分,供大家参考。如有错误的地方,欢迎指正。有更多的面试题或面试中遇到的坑,也欢迎补充分享。希望大家都能找到满意的工作,共勉之!
测试面试题集锦(一)| 软件测试常见必考问题与流程篇(附答案)
|
弹性计算 运维 容灾
从人工到自动,泛微云上自动化部署实践
泛微借助阿里云的底层优势,将部分客户的服务迁移到云上,从硬件和网络上保证用户能够高效访问 OA 系统
从人工到自动,泛微云上自动化部署实践
|
缓存 Linux Perl
CentOS 8解决yum不能更新源问题
问题描述:执行yum makecache出现以下错误,搜索网上帖子换成各种源均是不能进行基础库更新。问题根源:造成此问题的原因在于repo源的路径问题。解决方式1.执行cd /etc/yum.repos.d/进入目录;2.执行sed -i 's/$releasever/8/g' *命令;3.执行yum clean all清理当前缓存;4.执行yum makecache命令进行基础库缓存。
10419 0
CentOS 8解决yum不能更新源问题
|
监控 NoSQL 算法
阿里本地生活EMonitor根因分析大揭秘
阿里集团针对故障处理提出了“**1/5/10**”的目标-- 1 分钟发现、5 分钟定位、10 分钟恢复,这对我们的定位能力提出了更高的要求。
4921 115
阿里本地生活EMonitor根因分析大揭秘
|
数据可视化 IDE Java
PlanUML和Mermaid哪个好?
PlanUML和Mermaid哪个好?
5362 0
|
编解码 计算机视觉 C++
OpenCV 打开双目摄像头(python版)
本文主要介绍在OpenCV用使用双目摄像头,包括:打开单目摄像头、设置摄像头参数、拍照、录制视频。
1580 0
|
存储 SQL 分布式计算
ClickHouse深度揭秘
ClickHouse是近年来备受关注的开源列式数据库,主要用于数据分析(OLAP)领域。目前国内社区火热,各个大厂纷纷跟进大规模使用
7228 0
|
传感器 人工智能 城市大脑
阿里云AI | 畜牧养殖业综合解决方案
本文介绍了阿里云AI | 畜牧养殖业综合解决方案的方案概述以及业务价值。
阿里云AI | 畜牧养殖业综合解决方案
|
存储 数据采集 运维
智能数据构建与管理平台Dataphin的前世今生:缘起
阿里巴巴提出的OneData方法论帮助企业捋清了数据全生命周期的管理思路,更将其植入到产品Dataphin(智能数据构建与管理)中,通过阿里云为企业提供服务。
智能数据构建与管理平台Dataphin的前世今生:缘起

热门文章

最新文章