Looking for Senior AWS Serverless Architects & Engineers?
Let's TalkTencent is one of China’s major players in the cloud computing business. Like every global cloud provider, Tencent also has its own serverless product implementation, Serverless Cloud Functions (SCF). We’ll explore a short tutorial on setting up a cloud function with the Serverless framework.
We will look at using an HTTP API with query string parameters that will be the trigger to invoke the serverless function. The function on invocation takes those parameters and creates a JSON file uploaded to a Cloud Object Service (COS) bucket.
Overview of Steps
- Account Setup
- Installing the Serverless Framework
- Function Code Setup
- Deploying to Function Service
- Testing
Account Setup
Sign up on their website. Follow their on-screen instructions and everything should be smooth sailing from thereon.
Post-sign-up, the first and foremost thing to do is get your real name verification done (this could take a day or two to complete). You can start the verification by navigating to the ‘Account Information’ from the top right dropdown. Though this step is for users in the Mainland China region, when you deploy via the Serverless (SLS) framework, Tencent throws an error requiring this verification to be done.
After being verified, do the following.
- Go to SCF and COS dashboards via the console to “activate” them for use. A service role for SCF will be created for you automatically.
- Navigate to Cloud Access Management (CAM) to create a user and it’s access keys.
- For now, you can provide administrator access to the created user, but the best practice is to limit access to only those resources that the user is supposed to work with.
- We will also need a bucket for this tutorial, so head from the created user login to the console and make your way to the COS dashboard. Create a bucket, and note the name of the bucket and the region.
Installing the Serverless Framework
Ensure that you have node 6.x or later installed. Then run the following command:
Now you can use the serverless framework to create a project from one of the pre-configured templates that the framework provides.
Now we can cd into tencentsample directory and run npm install. This command is used to install the dependencies of the project.
Next run npm install cos-nodejs-sdk-v5. This dependency allows us to perform operations on the bucket which is needed for this tutorial.
Let’s take a quick look into the directory will show two files serverless.yml and index.js.
serverless.yml
- Declare a Serverless service
- Define one or more functions in the service
- Define the provider the service will be deployed to (and the runtime if provided)
- Define any custom plugins to be used
- Define events that trigger each function to execute (e.g. HTTP requests)
- Allow events listed in the events section to automatically create the resources required for the event upon deployment
index.js
The index.js is the file that contains the code that is to be executed by the function service.
The framework needs access to the cloud for deploying your code. To do this we need to provide a credentials file (name of the file can be anything).
With the following structure:
Store the credentials file in the root of the project directory.
Code Setup for the Function
With the framework and our base directory structure in place, all that is left to do is get our code into index.js, and configure the serverless.yml file to deploy our http trigger for the function. Let’s get to it by altering the files accordingly.
Within the index.js file replace its contents with the following code.
Your serverless.yml file will look something like this.
Deployment
Run serverless deploy or sls deploy from within the project directory. The Serverless framework will deploy the function and set up the http trigger for you. Also, if there is a need to redeploy edited code, run serverless deploy function --function <your function name> as opposed to running sls deploy because we only want to update the code and not instantiate full-blown infrastructure provisioning.
On visiting the SCF dashboard you should see the function setup and ready to run.
Testing
Copy the URL (the API trigger) from the terminal where you ran the deploy command and paste into the browser. Add the query parameters like this:
After hitting the API you should see a file named name.json being created in your bucket with the query parameters inside it.
We have now seen how the Serverless framework makes the process of developing Serverless functions on Tencent Cloud simple and straightforward. Your time is better utilized by focussing on developing the function, without having to worry about what’s going on in the background of the platform.