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

本文涉及的产品
Serverless 应用引擎免费试用套餐包,4320000 CU,有效期3个月
函数计算FC,每月15万CU 3个月
简介: 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

相关实践学习
【文生图】一键部署Stable Diffusion基于函数计算
本实验教你如何在函数计算FC上从零开始部署Stable Diffusion来进行AI绘画创作,开启AIGC盲盒。函数计算提供一定的免费额度供用户使用。本实验答疑钉钉群:29290019867
建立 Serverless 思维
本课程包括: Serverless 应用引擎的概念, 为开发者带来的实际价值, 以及让您了解常见的 Serverless 架构模式
目录
相关文章
|
7月前
|
网络协议 jenkins 调度
Docker【部署 06】Swarm实践及Operation not permitted和No chain/target/match by that name问题处理
Docker【部署 06】Swarm实践及Operation not permitted和No chain/target/match by that name问题处理
459 0
Docker【部署 06】Swarm实践及Operation not permitted和No chain/target/match by that name问题处理
|
7月前
|
Kubernetes 容器
k8s-error execution phase upload-config/kubelet: Error writing Crisocket...
k8s-error execution phase upload-config/kubelet: Error writing Crisocket...
208 0
|
Kubernetes 安全 容器
Kubernetes CKS【18】---Supply Chain Security - Static Analysis(OPA)
Kubernetes CKS【18】---Supply Chain Security - Static Analysis(OPA)
Kubernetes CKS【18】---Supply Chain Security - Static Analysis(OPA)
|
Java Shell PHP
Guidelines for Function Compute Development - Use Fun Local for Local Running and Debugging
Preface The following key concepts are involved in this document: Function Compute: an event-driven service that allows you to focus on writing and .
1784 0
|
中间件 Serverless Go
Backend-as-a-Service (BaaS) for Efficient Software Development
The adoption of the Internet and mobile technologies has revolutionized the business ecosystem, with entrepreneurs able to implement ideas quickly by .
1757 0
Backend-as-a-Service (BaaS) for Efficient Software Development
|
弹性计算
Structuring the Backend Service Architecture of a Mobile Card Game
2014 saw the rise of intense action mobile card games, and 2015 ushered in the age of real-time battles.
1448 0
Structuring the Backend Service Architecture of a Mobile Card Game
|
JavaScript 前端开发
Guidelines for Function Compute Development - Troubleshoot Timeout Issues
Endless codes and endless bugs When you write code, you may inadvertently introduce some hidden bugs, even if you test a large proportion of the codes to the maximum extent possible.
1642 0
|
关系型数据库 Shell Docker

热门文章

最新文章