Getting Started with Alibaba Cloud(Aliyun) Function Compute to make TypeScript-API and use fun-cli
This is about Function Compute Alibaba Cloud. Function Compute that is an event-driven, serverless computing platform corresponds to AWS lambda or GCP Cloud Functions.
It seems that Alibaba Cloud has the advantage to expand our business in Mainland China.
Function Computer is almost same as lambda and Cloud Function. In this article, I tried to create a nodejs10-typescript project with Docker, and deploy to Alibaba from localhost.
Docker environment and Install “fun"
First, create Dockerfile. Install aliyun-cli too.
FROM node:10.14.2-stretch ENV ALIYUNCLI_VERSION 3.0.16 WORKDIR /usr/local/bin RUN apt-get update && apt-get install -y curl && curl -L -o aliyun-cli-linux-amd64.tgz https://github.com/aliyun/aliyun-cli/releases/download/v${ALIYUNCLI_VERSION}/aliyun-cli-linux-${ALIYUNCLI_VERSION}-amd64.tgz && tar zxvf aliyun-cli-linux-amd64.tgz RUN npm install @alicloud/fun tsc typescript -g
And docker-compose.yml is the following.
version: '3' services: aliyun-fc: build: . container_name: aliyun-fc volumes: - ./src:/src - ./config.yaml:/root/.fcli/config.yaml working_dir: /src tty: true
“config.yaml" is needed to authenticate in Aliyun.
Build Docker image and attache as the following.
docker exec -it aliyun-fc /bin/bash # The following command ask accountid, accessKeyId, accessKeySecret. fun config
This command create config.yaml to “/root/.fcli/". And it mount config.yaml that is saved in host machine into docker container. Refer to docker-compose.yml.
Fundamental of the local infrastructure is completed with this.
Init node and TypeScript
Attach the Docker Container, then init respectively on Container.
docker exec -it aliyun-fc /bin/bash # It is asked which program language and which environment. select http-trigger-nodejs10. fun init # Init npm. (I pressed "Enter" for all asked.) npm init # Init tsc tsc --init
Install required libraries and rename index.js to index.ts to create typescript file. Set config of tsconfig.json whatever you want.
npm install -D @types/node mv index.js index.ts
Tentatively, deploy to alibaba cloud.
tsc && fun deploy
You can see created functions in Console.
And you can check a function that is deployed with the provided URL.
Modify Typescript
Update index.ts as the following. This modification makes a function that just return request body as it.
import * as getRawBody from 'raw-body' module.exports.handler = function(req, resp, context) { getRawBody(req, function(err, body) { resp.setHeader('content-type', 'application/json'); resp.send(body); }); }
Then create a new file “head.ts" in the same directory. That’s a function that just return request header as it.
module.exports.handler = function(req, resp, context) { resp.setHeader('content-type', 'application/json'); resp.send(JSON.stringify(req.headers)); }
Finally, define these functions in the template.yml.
ROSTemplateFormatVersion: '2015-09-01' Transform: 'Aliyun::Serverless-2018-04-03' Resources: api: Type: 'Aliyun::Serverless::Service' Properties: Description: 'helloworld' index: Type: 'Aliyun::Serverless::Function' Properties: Handler: index.handler Runtime: nodejs10 CodeUri: './' Events: httpTrigger: Type: HTTP Properties: AuthType: ANONYMOUS Methods: ['POST'] head: Type: 'Aliyun::Serverless::Function' Properties: Handler: head.handler Runtime: nodejs10 CodeUri: './' Events: httpTrigger: Type: HTTP Properties: AuthType: ANONYMOUS Methods: ['GET']
It can be defined two functions that are triggered by GET method or POST method. And you can see the two functions and endpoints after deploying to Aliyun. But I don’t still know how to make functions as REST-API.
Conclusion about Alibaba Cloud(Aliyun) function compute
I felt that Alibaba Cloud function compute is almost the same as lambda and Cloud Function. Maybe it can be more complexity construction with template.yaml.
It seems that the Start-up time of Function Compute is faster than one of lambda and CloudFunctions. In addition, the service is very low price.
The bad thing about it is that the official document is not enough. The official document includes something wrong or older. Of course, it has so much information in the Chinese language. And it is difficult for us to search for information about Alibaba cloud service because its service names are similar to AWS or GCP. For example “Cloud CDN", “RDS", “Function *", “Elastic *".
Discussion
New Comments
No comments yet. Be the first one!