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:
The entry of image processing example:
-
reszie
- path:/resize/[width]/[height]/[pic_name]
- rezise example
-
rotate:
- path:/rotate/[angle]/[pic_name]
- rotate example
-
crop:
- path:/crop/[left]/[top]/[right]/[bottom]/[pic_name]
- crop example
-
flip:
- path: /flip/[pic_name]
- flip example
-
flop:
- path: /flip/[pic_name]
- flop example
-
facedetect
- path: /face-detect/[pic_name]
- face-detect example
-
composite(default demo):
- path: /
- example defualt
Effect Picture:
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
andfc.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:
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)
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:
- Create log resources: logproject:
image-demo-pro
, logstore:fc-log
- Create service:
image-service
and function:image-proc
, configurating function with a trigger named http-trigger. - 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:
FC Console setup
Create service/function, and configurate http trigger
- Create a new service, configurate service with the role of oss permission
- Create function, and configurate Http trigger
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