By Du Wan (Yixian)
First, let's take a quick look at several key concepts mentioned in this article:
template.yml
file. For more information about Fun, see here.Note: The operations in this article are applicable to Funcraft version 3.0.6 and all later versions.
This project development is based on MacOS. However, the tools involved are platform-independent and also applicable to the Linux and Windows operating systems. Before proceeding with the example, make sure that the following tools are correctly installed, updated to the latest version, and properly configured.
Fun is based on Docker to simulate the local environment.
MacOS users may use homebrew to install these tools:
brew cask install docker
brew tap vangie/formula
brew install fun
Windows and Linux users must read this article to successfully install these tools.
After the installation, first run the fun config
command to initialize the configuration.
Note: Make sure to use a Funcraft version 3.0.6 or any later version.
$ fun --version
3.0.6
Run the fun init
command to locally initialize a project according to the specified template.
$ fun init vangie/libreoffice-example
Modify the template.yml
file, and modify OSS_REGION
and OSS_BUCKET
to be the same as the OSS configuration under the specific account.
This template project is updated to use a NAS network disk to store LibreOffice binary files. Compared to fc-libreoffice, the initialization time is shortened by using the initializer + OSS method. Moreover, the size of LibreOffice files is no longer restricted, which simplifies the difficulty of upgrading LibreOffice in the future and reduces complicated compilation and modification.
Refer to the following code snippet to install necessary dependencies.
$ fun install
using template: template.yml
start installing function dependencies without docker
building libreoffice/word2pdf-nodejs8
Funfile exist, Fun will use container to build forcely
Step 1/7 : FROM registry.cn-beijing.aliyuncs.com/aliyunfc/runtime-nodejs8:build-1.6.6
---> d38b47af4e9e
Step 2/7 : RUN fun-install apt-get install libnss3
---> Using cache
---> 71b4a0d6f676
Step 3/7 : RUN mkdir -p /mnt/auto/
---> Using cache
---> 3fbbfc5d7ef8
Step 4/7 : WORKDIR /mnt/auto
---> Using cache
---> f4b65a179d06
Step 5/7 : RUN curl https://fc-demo-public.oss-cn-hangzhou.aliyuncs.com/fun/examples/lo.tar.gz -o lo.tar.gz
---> Using cache
---> d1ba9fd1e188
Step 6/7 : RUN tar -xzf lo.tar.gz
---> Using cache
---> ab7011d1b370
Step 7/7 : RUN rm lo.tar.gz
---> Using cache
---> 34700b58a0e8
sha256:34700b58a0e8ab04898f8cae93f85ace5beb67fd7bb35e689395f9987101089a
Successfully built 34700b58a0e8
Successfully tagged fun-cache-cb5ac96f-00f8-4c05-b4d9-5e36874ee3b2:latest
copying function artifact to /Users/vangie/Workspace/libreoffice-example/{{ projectName }}/nodejs8
copy from container /mnt/auto/. to localNasDir
build function using image: fun-cache-cb5ac96f-00f8-4c05-b4d9-5e36874ee3b2
running task flow NpmTaskFlow
running task: CopySource
running task: NpmInstall
building libreoffice/word2pdf-python3
Funfile exist, Fun will use container to build forcely
Step 1/7 : FROM registry.cn-beijing.aliyuncs.com/aliyunfc/runtime-python3.6:build-1.6.6
---> 40ba07f7bda9
Step 2/7 : RUN fun-install apt-get install libnss3
---> Using cache
---> 4e4764b5c980
Step 3/7 : RUN mkdir -p /mnt/auto/
---> Using cache
---> 8009dc11bf7e
Step 4/7 : WORKDIR /mnt/auto
---> Using cache
---> 6e889ba3a676
Step 5/7 : RUN curl https://fc-demo-public.oss-cn-hangzhou.aliyuncs.com/fun/examples/lo.tar.gz -o lo.tar.gz
---> Using cache
---> 064cdb7fa85a
Step 6/7 : RUN tar -xzf lo.tar.gz
---> Using cache
---> 7030b6dde611
Step 7/7 : RUN rm lo.tar.gz
---> Using cache
---> 83f8fb18c2c9
sha256:83f8fb18c2c99b17ed5bbefcbc334a9a2e5955ac0257be00e88703ab21492052
Successfully built 83f8fb18c2c9
Successfully tagged fun-cache-6c286205-f96f-44f9-b27b-e2bfc4fb6f8e:latest
copying function artifact to /Users/vangie/Workspace/libreoffice-example/{{ projectName }}/python3
copy from container /mnt/auto/. to localNasDir
Install Success
Tips for next step
======================
* Invoke Event Function: fun local invoke
* Invoke Http Function: fun local start
* Build Http Function: fun build
* Deploy Resources: fun deploy
Synchronize files in the local NAS directory .fun/nas/auto-default/libreoffice/
to the NAS service.
$ fun nas sync
using template: template.yml
start fun nas init...
checking if _FUN_NAS_libreoffice needs to be deployed...
Waiting for service _FUN_NAS_libreoffice to be deployed...
make sure role 'aliyunfcgeneratedrole-cn-hongkong--FUN-NAS-libreoffice' is exist
role 'aliyunfcgeneratedrole-cn-hongkong--FUN-NAS-libreoffice' is already exist
attaching police 'AliyunECSNetworkInterfaceManagementAccess' to role: aliyunfcgeneratedrole-cn-hongkong--FUN-NAS-libreoffice
attached police 'AliyunECSNetworkInterfaceManagementAccess' to role: aliyunfcgeneratedrole-cn-hongkong--FUN-NAS-libreoffice
using 'VpcConfig: Auto', Fun will try to generate related vpc resources automatically
vpc already generated, vpcId is: vpc-j6c2caegv9uuavx2p4kdk
vswitch already generated, vswitchId is: vsw-j6c6cqwh3x079caqc6xut
security group already generated, security group is: sg-j6c8qsgt8wg0j99ospmt
generated auto VpcConfig done: {"vpcId":"vpc-j6c2caegv9uuavx2p4kdk","vswitchIds":["vsw-j6c6cqwh3x079caqc6xut"],"securityGroupId":"sg-j6c8qsgt8wg0j99ospmt"}
using 'NasConfig: Auto', Fun will try to generate related nas file system automatically
nas file system already generated, fileSystemId is: 127fe949c0d
nas file system mount target is already created, mountTargetDomain is: 127fe949c0d-klt69.cn-hongkong.nas.aliyuncs.com
generated auto NasConfig done: {"UserId":10003,"GroupId":10003,"MountPoints":[{"ServerAddr":"127fe949c0d-klt69.cn-hongkong.nas.aliyuncs.com:/libreoffice","MountDir":"/mnt/auto"}]}
Checking if nas directories /libreoffice exists, if not, it will be created automatically
Checking nas directories done ["/libreoffice"]
Waiting for function fun-nas-function to be deployed...
Waiting for packaging function fun-nas-function code...
The function fun-nas-function has been packaged.
Waiting for HTTP trigger httpTrigger to be deployed...
methods: POST,GET
url: https://1751705494334733.cn-hongkong.fc.aliyuncs.com/2016-08-15/proxy/_FUN_NAS_libreoffice/fun-nas-function/
function httpTrigger deploy success
function fun-nas-function deploy success
service _FUN_NAS_libreoffice deploy success
Create local NAS directory of service libreoffice:
/Users/vangie/Workspace/libreoffice-example/{{ projectName }}/.fun/nas/auto-default/libreoffice
fun nas init Success
starting upload /Users/vangie/Workspace/libreoffice-example/{{ projectName }}/.fun/nas/auto-default/libreoffice to nas://libreoffice/mnt/auto/
NAS path checking...
zipping /Users/vangie/Workspace/libreoffice-example/{{ projectName }}/.fun/nas/auto-default/libreoffice
/Users/vangie/Workspace/libreoffice-example/{{ projectName }}/.fun/tmp/nas/sync/Users/vangie/Workspace/libreoffice-example/{{ projectName }}/.fun/nas/auto-default/.fun-nas-generated-libreoffice.zip - zipped
checking NAS tmp dir
check done
Creating 124975363 bytes size file: /mnt/auto/.fun_nas_tmp/.fun-nas-generated-libreoffice.zip
create done
upload done
checking uploaded NAS zip file hash
hash unchanged
unzipping file
unzip done
cleaning
clean done
upload completed!
Tips for next step
======================
$ fun nas info # Show NAS info
$ fun nas ls # List NAS files
$ fun nas sync # Synchronize files to nas
$ fun deploy # Deploy Resources
After the test, we observed that LibreOffice cannot run in the local Docker environment under macOS, which may depend on the Linux kernel version. Therefore, this example skips the the running of the fun local invoke command. The function is directly deployed on the cloud.
$ fun deploy
using template: template.yml
using region: cn-hongkong
using accountId: ***********4733
using accessKeyId: ***********EUz3
using timeout: 600
Waiting for service libreoffice to be deployed...
make sure role 'aliyunfcgeneratedrole-cn-hongkong-libreoffice' is exist
role 'aliyunfcgeneratedrole-cn-hongkong-libreoffice' is already exist
attaching policies AliyunOSSFullAccess to role: aliyunfcgeneratedrole-cn-hongkong-libreoffice
attached policies AliyunOSSFullAccess to role: aliyunfcgeneratedrole-cn-hongkong-libreoffice
attaching police 'AliyunECSNetworkInterfaceManagementAccess' to role: aliyunfcgeneratedrole-cn-hongkong-libreoffice
attached police 'AliyunECSNetworkInterfaceManagementAccess' to role: aliyunfcgeneratedrole-cn-hongkong-libreoffice
using 'VpcConfig: Auto', Fun will try to generate related vpc resources automatically
vpc already generated, vpcId is: vpc-j6c2caegv9uuavx2p4kdk
vswitch already generated, vswitchId is: vsw-j6c6cqwh3x079caqc6xut
security group already generated, security group is: sg-j6c8qsgt8wg0j99ospmt
generated auto VpcConfig done: {"vpcId":"vpc-j6c2caegv9uuavx2p4kdk","vswitchIds":["vsw-j6c6cqwh3x079caqc6xut"],"securityGroupId":"sg-j6c8qsgt8wg0j99ospmt"}
using 'NasConfig: Auto', Fun will try to generate related nas file system automatically
nas file system already generated, fileSystemId is: 127fe949c0d
nas file system mount target is already created, mountTargetDomain is: 127fe949c0d-klt69.cn-hongkong.nas.aliyuncs.com
generated auto NasConfig done: {"UserId":10003,"GroupId":10003,"MountPoints":[{"ServerAddr":"127fe949c0d-klt69.cn-hongkong.nas.aliyuncs.com:/libreoffice","MountDir":"/mnt/auto"}]}
Checking if nas directories /libreoffice exists, if not, it will be created automatically
Checking nas directories done ["/libreoffice"]
Waiting for function word2pdf-nodejs8 to be deployed...
Waiting for packaging function word2pdf-nodejs8 code...
The function word2pdf-nodejs8 has been packaged. A total of 29 files files were compressed and the final size was 1.63 MB
function word2pdf-nodejs8 deploy success
Waiting for function word2pdf-python3 to be deployed...
Waiting for packaging function word2pdf-python3 code...
The function word2pdf-python3 has been packaged. A total of 27 files files were compressed and the final size was 1.63 MB
function word2pdf-python3 deploy success
service libreoffice deploy success
Now, call the function in nodejs8 runtime as shown below.
$ fun invoke word2pdf-nodejs8
using template: template.yml
========= FC invoke Logs begin =========
FC Invoke Start RequestId: 9f68ec8b-a1ca-4fab-a605-e5414a878c7e
load code for handler:index.handler
2019-10-14T08:20:52.559Z 9f68ec8b-a1ca-4fab-a605-e5414a878c7e [verbose] convert /tmp/example.docx -> /tmp/example.pdf using filter : writer_pdf_Export
FC Invoke End RequestId: 9f68ec8b-a1ca-4fab-a605-e5414a878c7e
Duration: 3397.91 ms, Billed Duration: 3400 ms, Memory Size: 640 MB, Max Memory Used: 203.46 MB
========= FC invoke Logs end =========
FC Invoke Result:
http://vangie-test.oss-cn-shanghai.aliyuncs.com/example.pdf
Call the function in Python 3 runtime.
$ fun invoke word2pdf-python3
using template: template.yml
========= FC invoke Logs begin =========
FC Invoke Start RequestId: 4bbb507d-1f57-4ca1-a877-4ce4d9096d4d
convert /tmp/example.docx -> /tmp/example.pdf using filter : writer_pdf_Export
FC Invoke End RequestId: 4bbb507d-1f57-4ca1-a877-4ce4d9096d4d
Duration: 1855.37 ms, Billed Duration: 1900 ms, Memory Size: 640 MB, Max Memory Used: 67.22 MB
========= FC invoke Logs end =========
FC Invoke Result:
upload to oss success, the url is http://vangie-test.oss-cn-shanghai.aliyuncs.com/example.pdf?security-token=CAISmwJ1q6Ft5B2yfSjIr4%2BHJvjBgI9I%2F6CEM1D1oEMFf7dD16D61Tz2IHpFfnFsBukftvU3nW5U5%2FYYlqZdVplOWU3Da%2BB364xK7Q757QoRDHjwv9I%2Bk5SANTW5KXyShb3%2FAYjQSNfaZY3eCTTtnTNyxr3XbCirW0ffX7SClZ9gaKZ8PGD6F00kYu1bPQx%2FssQXGGLMPPK2SH7Qj3HXEVBjt3gX6wo9y9zmnZDFtUKD0AymkbRJ%2BN%2BqGPX%2BMZkwZqUYesyuwel7epDG1CNt8BVQ%2FM909vceqG2f4o7EWgEAu0zYb7uEqMcqJQt4d7U8FaVLof7xj%2FRkt%2BDJkID6jh1LeOFSVSvcQ4avhc%2FFEvmkMdg3dL32K8pAU1cDq3ieGoABAiBcEZ5ZzqlsXeIfiEKFAsIlNr3yYTflfBp%2FOr%2BktvB54GISQyX%2BzAlhBeq1IkBl3pudBcz%2FSsluxyR9kySjvx07UU4Zdh5dS%2BaNrDimZVvKxxYZaMtA%2FFqetiO1NZ6iE6GPOBe0lb5Hg%2FRHUrSaVK5JgQR%2B0JSvy%2BEUcw%2B44jE%3D&OSSAccessKeyId=STS.NL2mBumPiHbo1vDDCPs8o8eQ7&Expires=1571041909&Signature=kfDwm6JRZ2odtXU5IML3ETQjHds%3D
Access the PDF file through the OSS's temporary URL. The temporary URL is valid for 10 minutes.
How to Develop Function Compute - Using Ghostscript to Convert PDF Files to JPG Files
How to Develop Function Compute - Installing Dependencies in Interactive Mode
99 posts | 7 followers
FollowAlibaba Cloud Serverless - March 19, 2019
Alibaba Cloud Serverless - March 19, 2019
Alibaba Clouder - January 18, 2021
Alibaba Cloud Serverless - April 7, 2020
Alibaba Cloud Serverless - April 7, 2020
Alibaba Clouder - January 30, 2019
99 posts | 7 followers
FollowAlibaba Cloud Function Compute is a fully-managed event-driven compute service. It allows you to focus on writing and uploading code without the need to manage infrastructure such as servers.
Learn MoreVisualization, O&M-free orchestration, and Coordination of Stateful Application Scenarios
Learn MoreServerless Application Engine (SAE) is the world's first application-oriented serverless PaaS, providing a cost-effective and highly efficient one-stop application hosting solution.
Learn MoreAlibaba Cloud (in partnership with Whale Cloud) helps telcos build an all-in-one telecommunication and digital lifestyle platform based on DingTalk.
Learn MoreMore Posts by Alibaba Cloud Serverless