tencent cloud

Feedback

Application Management

Last updated: 2024-12-02 10:48:10

    Overview

    Every time sls deploy is run, a serverless application will be deployed, which consists of one or multiple component instances, and each component corresponds to an instance.
    Each instance involves a serverless.yml file, which defines certain parameters of the component. Such parameters are used to generate the instance information during deployment; for example, region defines the resource region.
    The differences between the project organizations of a single-instance application and a multi-instance application are as shown below:
    
    

    Single-Instance application

    In the project of a single-instance application, only one component is imported, and only one component instance will be generated during deployment.
    Generally, you do not need to manually name a single-instance application. If you want to customize the name, you can directly enter a name in serverless.yml of the component.

    Multi-Instance application

    In the project of a multi-instance application, multiple components are imported, and multiple component instances will be generated during deployment.
    You need to enter a custom name for the multi-instance application to ensure that all components are managed under the same application. Generally, the application name is defined in serverless.yml in the project directory, so that all components can use the same application name.

    serverless.yml file

    The serverless.yml file defines the application organization parameters and the component's inputs parameters. During each deployment, resources will be created, updated, and orchestrated according to the configuration information in the serverless.yml file.
    The following is a simple serverless.yml file:
    # serverless.yml
    
    # Application information
    app: expressDemoApp # Application name, which is the component instance name by default
    stage: ${env:STAGE} # Parameter used to isolate the development environment, which is `dev` by default and optional
    # Component information
    component: express # Name of the imported component, which is required. The `express-tencent` component is used in this example
    name: expressDemo # Name of the instance created by the component, which is required
    
    # Component configuration
    inputs:
    src:
    src: ./
    exclude:
    - .env
    region: ap-guangzhou
    runtime: Nodejs10.15
    functionName: ${name}-${stage}-${app} # Function name
    apigatewayConf:
    protocols:
    - http
    - https
    environment: release
    Configuration information in the .yml file:

    Application information

    Parameter
    Description
    org
    Organization information, which is the APPID of your Tencent Cloud account by default. It is a reserved field and is not recommended to be used.
    app
    Application name, which is the same as the instance name in the component information by default. A single-instance application and a multi-instance application have different definitions of this parameter. For more information, please see Application Management.
    stage
    Environment information, which is dev by default. You can define different stage values to provide independent runtime environments for development, testing, and release of the serverless application, respectively. For more information, please see Project Development.

    Component information

    Parameter
    Description
    component
    Name of the imported component. You can run sls registry to query components available for import.
    name
    Name of the created instance. An instance will be created when each component is deployed.

    Parameter information

    The parameters under inputs are configuration parameters of the corresponding component. Different components have different parameters. To guarantee environment isolation and resource uniqueness, the component resource names are in the ${name}-${stage}-${app} format by default.

    Application Deployment

    Single-Instance application

    Do not set the application name (app) in the serverless.yml file, and an application with the same name as that of the instance (name) will be generated by default during deployment.
    For example, if you create an SCF project, and the project directory is as shown below:
    scfDemo
    |- index.js
    └── serverless.yml
    Here, the serverless.yml file is configured as follows:
    component: scf
    name: myscf
    
    inputs:
    src: ./
    runtime: CustomRuntime
    region: ap-guangzhou
    functionName: ${name}-${stage}-${app} # Function name
    
    events:
    - apigw:
    parameters:
    endpoints:
    - path: /
    method: GET
    Run sls deploy in the scfDemo directory for deployment, and an application whose app is myscf will be generated by default, and the application will contain an SCF instance named myscf.
    Generally, you can use the default application name for a single-instance application project. If you want to customize the name, you can directly enter a name in serverless.yml as follows:
    app: scfApp # Set `app` to `scfApp`
    
    component: scf
    name: myscf
    
    inputs:
    src: ./
    runtime: CustomRuntime
    region: ap-guangzhou
    
    events:
    - apigw:
    parameters:
    endpoints:
    - path: /
    method: GET
    Run sls deploy in the scfDemo directory for deployment, and an application whose app is scfApp will be generated, and the application will contain an SCF instance named myscf.

    Multi-Instance application

    As the project contains multiple components, you need to unify the application name for all components. Generally, you should define a serverless.yml file in the root directory of the project to name the application.
    For example, if you deploy a Vue.js + Express.js + PostgreSQL full-stack website, and the project directories are as shown below:
    fullstack
    |- api
    | |- sls.js
    | |- ...
    | └── serverless.yml
    |- db
    | └── serverless.yml
    |- frontend
    | |- ...
    | └── serverless.yml
    |- vpc
    | └── serverless.yml
    |- scripts
    └── serverless.yml
    app is set in the serverless.yml file in the fullstack directory of the project:
    # Project application information
    
    app: fullstack
    The component and parameter information is configured in the serverless.yml file in each component directory, such as the api directory:
    # `api` configuration information
    
    component: express
    name: fullstack-api
    
    inputs:
    src:
    src: ./
    exclude:
    - .env
    functionName: ${name}-${stage}-${app}
    region: ${env:REGION}
    runtime: Nodejs10.15
    functionConf:
    timeout: 30
    vpcConfig:
    vpcId: ${output:${stage}:${app}:fullstack-vpc.vpcId}
    subnetId: ${output:${stage}:${app}:fullstack-vpc.subnetId}
    environment:
    variables:
    PG_CONNECT_STRING: ${output:${stage}:${app}:fullstack-db.private.connectionString}
    apigatewayConf:
    enableCORS: true
    protocols:
    - http
    - https
    Note:
    In the demo on the legacy version, the application name (app) is written into all components, which requires you to ensure that all components under the project have the same application name. We recommend you not use this method in the new version.
    Contact Us

    Contact our sales team or business advisors to help your business.

    Technical Support

    Open a ticket if you're looking for further assistance. Our Ticket is 7x24 avaliable.

    7x24 Phone Support