All Products
Search
Document Center

Serverless App Engine:Use Terraform to manage SAE applications

Last Updated:Jan 30, 2024

Serverless App Engine (SAE) is a serverless Platform as a Service (PaaS) for application management. You can deploy your applications to SAE without the need to manage or maintain clusters and servers. This way, you can focus on application design and development. You can deploy applications to SAE by using the console, API operations, plug-ins, and CI/CD tools. You can also use Terraform to deploy SAE applications. This topic describes how to create and delete an SAE application by using Terraform.

Prerequisites

  • Terraform is installed.
  • Your account information is configured.

    You can select an Alibaba Cloud authentication method to provide the authentication information required by Terraform. The following example shows how to use environment variables to perform authentication:

    export ALICLOUD_ACCESS_KEY="************"
    export ALICLOUD_SECRET_KEY="************"
    export ALICLOUD_REGION="cn-hangzhou"
    Note To ensure data security, we recommend that you grant a RAM user the permissions to manage SAE resources based on your business requirements. For more information, see Grant permissions to a RAM user.

Background information

The alicloud_sae_application resource of Terraform provides parameters for managing SAE applications. This topic describes how to create and delete applications and the parameters associated with these operations. This topic also demonstrates how Terraform can be used to manage cloud resources. For more information, see alicloud_sae_application.

Note

You cannot use Terraform to update applications.

Create an application

You can use an image or a code package to deploy an application to SAE. The code package can be a JAR, WAR, or PHP package as a .zip file. When you create an application, you can use one of the following methods to configure the virtual private cloud (VPC) based on your requirements:

  • Automatic configuration: SAE automatically configures the namespace, VPC, vSwitch, and security group for the application that you want to create. The default namespace is configured.

  • Custom configuration: You need to manually configure the namespace, VPC, vSwitch, and security group for the application that you want to create.

Automatic configuration

This section describes how to use an image to deploy an application and implement automatic application configuration. The China (Hangzhou) region is used in this example.

  1. Create a project folder named terraform for storing Terraform resources.
  2. Run the following command to go to the project directory:
    cd terraform
  3. Create a configuration file named main.tf. Sample code:

    terraform {
      required_providers {
        alicloud = {
          source  = "hashicorp/alicloud"
          version = "1.156.0"
        }
      }
    }
    
    resource "alicloud_sae_application" "auto" {
      count           = 1
      app_name        = var.app_name
      app_description = var.app_description
      auto_config     = true 
      image_url       = var.image_url
      package_type    = var.package_type
      timezone        = "Asia/Beijing"
      replicas        = var.replicas
      cpu             = var.cpu
      memory          = var.memory
    }
    
    # Specify the application name.
    variable "app_name" {
      description = "The name of the application"
      type        = string
    }
    
    # Specify the description of the application.
    variable "app_description" {
      default     = "description created by Terraform"
      description = "The description of the application"
      type        = string
    }
    
    # Specify the deployment method of the application.
    variable "package_type" {
      default     = "Image"
      description = "The package type of the application"
      type        = string
    }
    
    # Specify the CPU specifications of the instance.
    variable "cpu" {
      default     = "500"
      description = "The cpu of the application, in unit of millicore"
      type        = string
    }
    
    # Specify the memory size of the instance.
    variable "memory" {
      default     = "1024"
      description = "The memory of the application, in unit of MB"
      type        = string
    }
    
    # Specify the address of the image.
    variable "image_url" {
      description = "The image url of the application, like `registry.cn-hangzhou.aliyuncs.com/google_containers/nginx-slim:0.9`"
      type        = string
    }
    
    # Specify the number of application instances.
    variable "replicas" {
      default     = "1"
      description = "The replicas of the application"
      type        = string
    }
    
    output "app_id" {
      description = "The id of the application"
      value       = alicloud_sae_application.auto.0.id
    }
    
    output "app_name" {
      description = "The name of the application"
      value       = var.app_name
    }
  4. Run the following command to initialize the configurations:
    terraform init
  5. Perform the following steps in sequence to create an SAE application:

    1. Run the following command to execute the configuration file:

      terraform apply
    2. Enter the following information as prompted:

      • app_name: the name of the application. Enter auto-app-1.

      • image_url: the address of the image. Enter registry.cn-hangzhou.aliyuncs.com/****/****:01.

        You can log on to the Container Registry console and obtain the image address on the Details page of the repository. Format:

        registry.<regionId>.aliyuncs.com/<Namespace name>/<Repository name>:<Image version>

      Expected output:

      ...
      
      Plan: 1 to add, 0 to change, 0 to destroy.
      
      Changes to Outputs:
        + app_id   = (known after apply)
        + app_name = "auto-app-1"
      alicloud_sae_application.auto[0]: Creating...
      ...
      alicloud_sae_application.auto[0]: Creation complete after 59s [id=f8e2f217-8788-41e0-85d1-ce96105b****]
      
      Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
      
      Outputs:
      
      app_id = "f8e2f217-8788-41e0-85d1-ce96105b****"
      app_name = "auto-app-1"

    If the output is returned as expected, the application that is deployed by using the image is created.

Custom configuration: use an image to deploy the application

This section describes how to use an image to deploy an application and manually configure the application. The China (Hangzhou) region is used in this example.

  1. Create a project folder named terraform for storing Terraform resources.
  2. Run the following command to go to the project directory:
    cd terraform
  3. Create a configuration file named main.tf. Sample code:

    terraform {
      required_providers {
        alicloud = {
          source  = "hashicorp/alicloud"
          version = "~> 1.163.0"
        }
      }
    }
    
    # Specify the information of the namespace.
    resource "alicloud_sae_namespace" "default" {
      namespace_description = var.namespace_description
      namespace_id          = var.namespace_id
      namespace_name        = var.namespace_name
    }
    
    # Specify the description of the namespace.
    variable "namespace_description" {
      description = "Namespace Description"
      default     = "a namespace"
    }
    # Specify the name of the namespace.
    variable "namespace_name" {
      description = "Namespace Name"
      type = string
    }
    # Specify the ID of the namespace.
    variable "namespace_id" {
      description = "Namespace ID"
      type = string
    }
    
    output "namespace_id" {
      value = var.namespace_id
      description = "Namespace ID"
    }
    
    # Specify the VPC and security group.
    resource "alicloud_security_group" "sg" {
      name        = var.name
      description = var.description
      vpc_id      = module.vpc.VPC_ID
    }
    
    resource "alicloud_security_group_rule" "sg_rule" {
      type              = "ingress"
      ip_protocol       = "tcp"
      nic_type          = "intranet"
      policy            = "accept"
      port_range        = var.port_range
      priority          = 1
      security_group_id = alicloud_security_group.sg.id
      cidr_ip           = var.cidr_ip
    }
    
    module "vpc" {
      source  = "git::github.com/kubevela-contrib/terraform-modules.git//alibaba/vswitch"
      zone_id = var.zone_id
    }
    
    variable "name" {
      default     = "tf"
      description = "The name of the security group rule"
      type        = string
    }
    
    variable "description" {
      default     = "The description of the security group rule"
      description = "The description of the security group rule"
      type        = string
    }
    
    # Specify the port range.
    variable "port_range" {
      default     = "1/65535"
      description = "The port range of the security group rule"
      type        = string
    }
    
    # Specify the Classless Inter-Domain Routing (CIDR) block.
    variable "cidr_ip" {
      description = "cidr blocks used to create a new security group rule"
      type        = string
      default     = "0.0.0.0/0"
    }
    
    # Specify the zone in the region.
    variable "zone_id" {
      description = "Availability Zone ID"
      type        = string
      default     = "cn-hongkong-b"
    }
    
    resource "alicloud_sae_application" "manual" {
      app_name          = var.app_name
      app_description   = var.app_description
      deploy            = true
      image_url         = var.image_url
      namespace_id      = alicloud_sae_namespace.default.id
      vswitch_id        = module.vpc.VSWITCH_ID
      vpc_id            = module.vpc.VPC_ID
      security_group_id = alicloud_security_group.sg.id
      package_type      = var.package_type
      timezone          = "Asia/Beijing"
      replicas          = var.replicas
      cpu               = var.cpu
      memory            = var.memory
    
    # Specify environment variables.
      envs="[{'name':'envtmp','value':'0'},{'name':'envtmp2','value':'0'}]"
    # Specify custom mappings between hostnames and IP addresses.
      custom_host_alias = "[{hostName:'samplehost',ip:'127.0.0.1'},{'hostName':'example.com','ip':'128.0.X.X'}]"
    # Specify the health check methods for the application.
      liveness = "{'httpGet':{'path':'/','port':80,'scheme':'HTTP'},'initialDelaySeconds':20,'periodSeconds':10,'timeoutSeconds':1}"
      readiness = "{'httpGet':{'path':'/','port':80,'scheme':'HTTP'},'initialDelaySeconds':20,'periodSeconds':10,'timeoutSeconds':1}"
    
    # Specify the script that is executed after the container starts.
      post_start ="{'exec':{'command':['sh','-c','echo hello > /tmp/hello.txt']}}"
    # Specify the script that is executed before the container is stopped.
      pre_stop = "{'exec':{'command':['sh','-c','echo hello']}}"
    # Specify the timeout period for a graceful shutdown.
      termination_grace_period_seconds = 50
    # Specify whether to automatically enable an auto scaling rule for the application.
      auto_enable_application_scaling_rule = true
    # Specify the minimum number of available instances.
      min_ready_instances = 1
    }
    # Configure log collection to Simple Log Service.
    variable "slsConfig" {
      default = "[{"logDir":"","logType":"stdout"},{"logDir":"/home/admin/logs/*.log"}]"
      description = "The config of sls log collect"
      type        = string
    }
    # Specify the application name.
    variable "app_name" {
      description = "The name of the application"
      type        = string
    }
    # Specify the description of the application.
    variable "app_description" {
      default     = "description created by Terraform"
      description = "The description of the application"
      type        = string
    }
    # Specify the deployment method of the application.
    variable "package_type" {
      default     = "Image"
      description = "The package type of the application"
      type        = string
    }
    
    # Specify the CPU specifications of the instance.
    variable "cpu" {
      default     = "500"
      description = "The cpu of the application, in unit of millicore"
      type        = string
    }
    # Specify the memory size of the instance.
    variable "memory" {
      default     = "1024"
      description = "The memory of the application, in unit of MB"
      type        = string
    }
    # Specify the address of the image.
    variable "image_url" {
      description = "The image url of the application, like `registry.cn-hangzhou.aliyuncs.com/google_containers/nginx-slim:0.9`"
      type        = string
    }
    # Specify the number of application instances.
    variable "replicas" {
      default     = "1"
      description = "The replicas of the application"
      type        = string
    }
    
    output "app_id" {
      description = "The id of the application"
      value       = alicloud_sae_application.manual.id
    }
    
    output "app_name" {
      description = "The name of the application"
      value       = var.app_name
    }
  4. Run the following command to initialize the configurations:
    terraform init
  5. Perform the following steps to create an application by using an image:

    1. Run the following command to deploy the application:

      terraform apply
    2. Enter the following information as prompted:

      • app_name: the name of the application. Enter manual-image.

      • namespace_name: the name of the namespace. Enter demo.

      • namespace_id: the ID of the namespace. Enter cn-hangzhou:demo.

      • image_url: the address of the image. Enter registry.cn-hangzhou.aliyuncs.com/****/****:01.

        You can log on to the Container Registry console and obtain the image address on the Details page of the repository. Format:

        registry.<regionId>.aliyuncs.com/<Namespace name>/<Repository name>:<Image version>

      Expected output:

      ...
      
      Plan: 6 to add, 0 to change, 0 to destroy.
      
      Changes to Outputs:
        + app_id       = (known after apply)
        + app_name     = "manual-image"
        + namespace_id = "cn-hangzhou:demo"
      alicloud_sae_namespace.default: Creating...
      ...
      alicloud_sae_application.manual: Creation complete after 1m58s [id=af34c033-fc5a-4147-8baf-87c71d3a****]
      
      Apply complete! Resources: 6 added, 0 changed, 0 destroyed.
      
      Outputs:
      
      app_id = "af34c033-fc5a-4147-8baf-87c71d3a****"
      app_name = "manual-image"
      namespace_id = "cn-hangzhou:demo"

    If the output is returned as expected, the application that is deployed by using the image is created.

Custom configuration: use a JAR package to deploy the application

This section describes how to use a JAR package to deploy an application and manually configure the application. The China (Hangzhou) region is used in this example.

  1. Create a project folder named terraform for storing Terraform resources.
  2. Run the following command to go to the project directory:
    cd terraform
  3. Create a configuration file named main.tf. Sample code:

    terraform {
      required_providers {
        alicloud = {
          source  = "hashicorp/alicloud"
          version = "~> 1.163.0"
        }
      }
    }
    
    # Specify the namespace.
    resource "alicloud_sae_namespace" "default" {
      namespace_description = var.namespace_description
      namespace_id          = var.namespace_id
      namespace_name        = var.namespace_name
    }
    
    # Specify the description of the namespace.
    variable "namespace_description" {
      description = "Namespace Description"
      default     = "a namespace"
    }
    
    # Specify the name of the namespace.
    variable "namespace_name" {
      description = "Namespace Name"
      type = string
    }
    # Specify the ID of the namespace.
    variable "namespace_id" {
      description = "Namespace ID"
      type = string
    }
    
    
    output "namespace_id" {
      value = var.namespace_id
      description = "Namespace ID"
    }
    
    # Specify the VPC and security group.
    resource "alicloud_security_group" "sg" {
      name        = var.name
      description = var.description
      vpc_id      = module.vpc.VPC_ID
    }
    
    resource "alicloud_security_group_rule" "sg_rule" {
      type              = "ingress"
      ip_protocol       = "tcp"
      nic_type          = "intranet"
      policy            = "accept"
      port_range        = var.port_range
      priority          = 1
      security_group_id = alicloud_security_group.sg.id
      cidr_ip           = var.cidr_ip
    }
    
    module "vpc" {
      source  = "git::github.com/kubevela-contrib/terraform-modules.git//alibaba/vswitch"
      zone_id = var.zone_id
    }
    
    variable "name" {
      default     = "tf"
      description = "The name of the security group rule"
      type        = string
    }
    
    variable "description" {
      default     = "The description of the security group rule"
      description = "The description of the security group rule"
      type        = string
    }
    # Specify the port range.
    variable "port_range" {
      default     = "1/65535"
      description = "The port range of the security group rule"
      type        = string
    }
    # Specify the IP address of the security group.
    variable "cidr_ip" {
      description = "cidr blocks used to create a new security group rule"
      type        = string
      default     = "0.0.0.0/0"
    }
    # Specify the zone in the region.
    variable "zone_id" {
      description = "Availability Zone ID"
      type        = string
      default     = "cn-hongkong-b"
    }
    
    resource "alicloud_sae_application" "manual" {
      app_name          = var.app_name
      app_description   = var.app_description
      deploy            = true
    
      package_version = "12132111"
      package_url     = "https://****.oss-ap-southeast-1.aliyuncs.com/javacommon-0.0.1-SNAPSHOT.jar"
      jdk             = "Open JDK 8"
    
      namespace_id      = alicloud_sae_namespace.default.id
      vswitch_id        = module.vpc.VSWITCH_ID
      vpc_id            = module.vpc.VPC_ID
      security_group_id = alicloud_security_group.sg.id
      package_type      = var.package_type
      timezone          = "Asia/Beijing"
      replicas          = var.replicas
      cpu               = var.cpu
      memory            = var.memory
    }
    # Specify the application name.
    variable "app_name" {
      description = "The name of the application"
      type        = string
    }
    # Specify the description of the application.
    variable "app_description" {
      default     = "description created by Terraform"
      description = "The description of the application"
      type        = string
    }
    # Specify the deployment method of the application.
    variable "package_type" {
      default     = "FatJar"
      description = "The package type of the application"
      type        = string
    }
    # Specify the CPU specifications of the instance.
    variable "cpu" {
      default     = "500"
      description = "The cpu of the application, in unit of millicore"
      type        = string
    }
    # Specify the memory size of the instance.
    variable "memory" {
      default     = "1024"
      description = "The memory of the application, in unit of MB"
      type        = string
    }
    
    # Specify the number of application instances.
    variable "replicas" {
      default     = "1"
      description = "The replicas of the application"
      type        = string
    }
    
    output "app_id" {
      description = "The id of the application"
      value       = alicloud_sae_application.manual.id
    }
    
    output "app_name" {
      description = "The name of the application"
      value       = var.app_name
    }
                            
  4. Run the following command to initialize the configurations:
    terraform init
  5. Perform the following steps to create an application by using a JAR package:

    1. Run the following command to deploy the application:

      terraform apply
    2. Enter the following information as prompted:

      • app_name: the name of the application. Enter manual-jar.

      • namespace_name: the name of the namespace. Enter demo.

      • namespace_id: the ID of the namespace. Enter cn-hangzhou:demo.

      Expected output:

      Plan: 6 to add, 0 to change, 0 to destroy.
      
      Changes to Outputs:
        + app_id       = (known after apply)
        + app_name     = "manual-jar"
        + namespace_id = "cn-hangzhou:demo"
      alicloud_sae_namespace.default: Creating...
      ...
      alicloud_sae_application.manual: Creation complete after 1m46s [id=724b681e-e9e4-4891-b426-f16d7b78****]
      
      Apply complete! Resources: 6 added, 0 changed, 0 destroyed.
      
      Outputs:
      
      app_id = "724b681e-e9e4-4891-b426-f16d7b78****"
      app_name = "manual-jar"
      namespace_id = "cn-hangzhou:demo"

    If the output is returned as expected, the application that is deployed by using the JAR package is created.

Delete an application

This section describes how to delete an application. The auto-app-1 application is used in this example. This application was automatically created in the China (Hangzhou) region.

  1. Run the following command in the project directory to execute the configuration file:
    terraform destroy
  2. To delete the application, enter the following information as prompted:

    • app_name: Enter auto-app-1.

    • image_url: Enter registry.cn-hangzhou.aliyuncs.com/****/****:01.

      Format:

      registry.<regionId>.aliyuncs.com/<Namespace name>/<Repository name>:<Image version>

      You can log on to the Container Registry console and obtain the image address on the Details page of the repository.

    Expected output:

    alicloud_sae_application.auto[0]: Refreshing state... [id=599a843b-f11d-456e-b934-dc9fdf99****]
    
    Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
      - destroy
    
    Terraform will perform the following actions:
    
      # alicloud_sae_application.auto[0] will be destroyed
      - resource "alicloud_sae_application" "auto" {
          - app_description                  = "description created by Terraform" -> null
          - app_name                         = "auto-app-1" -> null
    ...
        }
    
    Plan: 0 to add, 0 to change, 1 to destroy.
    
    Changes to Outputs:
      - app_id   = "599a843b-f11d-456e-b934-dc9fdf99****" -> null
      - app_name = "auto-app-1" -> null
    alicloud_sae_application.auto[0]: Destroying... [id=599a843b-f11d-456e-b934-dc9fdf99****]
    alicloud_sae_application.auto[0]: Destruction complete after 5s
    
    Destroy complete! Resources: 1 destroyed.

    If the output is returned as expected, the auto-app-1 application is deleted.

References