Complete the steps in this guide to install the Serverless Framework open-source CLI and deploy a sample Service on Tencent Cloud that reports deployment information and operational metrics to the Serverless Framework.
There are a few prerequisites you need to install and configure:
- Install Node.js 6.x or later on your local machine
- Install the Serverless Framework open-source CLI version 1.47.0 or later
If you already have these prerequisites setup you can skip ahead to deploy an example Service.
Install Node.js and NPM
- Follow these installation instructions.
- At the end, you should be able to run
node -vfrom your command line and get a result like this...
$ node -vvx.x.x
- You should also be able to run
npm -vfrom your command line and should see...
$ npm -vx.x.x
Install the Serverless Framework open-source CLI
- Run this command in your terminal
npm install -g serverless
- After install is complete, you should be able to run
serverless -vfrom your command line and get a result like this...
$ serverless -vx.x.x
Create and deploy a serverless Service
Now that you’ve completed your setup, let’s create and deploy a serverless Service.
Create a new Service from a Template
- Use the Serverless Framework open-source CLI to create a new Service with
# Create a new Serverless service/project$ serverless create --template tencent-nodejs --path my-service2. Install the dependencies```sh# Change into the newly created directory$ cd my-service$ npm install
Set up the credentials
Configure your Tencent Cloud account to work with the Serverless Framework.
Set up an endpoint
An Event is anything that can trigger your serverless functions. In this case, you need to define a endpoint in your
serverless.yml that will trigger your serverless function.
service: my-service # service nameprovider: # provider informationname: tencentruntime: Nodejs8.9credentials: ~/credentialsplugins:- serverless-tencent-scffunctions:function_one:handler: index.main_handlerruntime: Nodejs8.9events:- apigw:name: hello_world_apigwparameters:stageName: releaseserviceId:httpMethod: ANY
Deploy the Service
Use this command to deploy your service for the first time and after you make changes to your Functions, Events or Resources in
serverless.yml and want to deploy all changes within your Service at the same time.
More information in deploy command
Test your Service
Replace the URL in the following curl command with your returned endpoint URL, which you can find in the
sls deploy output, to hit your URL endpoint.
$ curl -X POST https://service-xxxx-1300000000.ap-guangzhou.apigateway.myqcloud.com/release/
Invoke your Service's function
Invokes a Function and returns logs.
serverless invoke -f hello
More information in invoke command
Fetch the Function Logs
Open up a separate tab in your console and stream all logs for a specific Function using this command.
serverless logs -f hello -t
Remove your Service
If at any point you no longer need your Service, you can run the following command to remove the Functions, Events and Resources that were created. This will delete the resources you created and ensure that you don't incur any unexpected charges.