All Products
Search
Document Center

ApsaraDB RDS:Install Terraform on your computer and configure Terraform

Last Updated:Nov 10, 2023

Before you can use the template syntax of Terraform to define, preview, and deploy the cloud resources, you must install Terraform and configure it.

Procedure

  1. Install Terraform.

    1. Download the Terraform installation package that suits your operating system from the Terraform official website.

    2. Decompress the Terraform installation package.

      • For a Linux or macOS operating system, decompress the package to the /usr/local/bin directory. If you decompress the package to a different directory, you must configure environment variables. For more information, see How to permanently set $PATH on Linux/Unix?

      • For a Windows operating system, decompress the package to any directory, such as D:\terraform\, and add the directory to the value of the path environment variable. For more information, see Where can I set path to make.exe on Windows?

    3. Run the terraform command to verify the installation of Terraform.

      If Terraform is installed, a command output similar to the following one is returned and contains available Terraform options:

      Usage: terraform [global options] <subcommand> [args]
      
      The available commands for execution are listed below.
      The primary workflow commands are given first, followed by
      less common or more advanced commands.
      
      Main commands:
        init          Prepare your working directory for other commands
        validate      Check whether the configuration is valid
        plan          Show changes required by the current configuration
        apply         Create or update infrastructure
        destroy       Destroy previously-created infrastructure
      
      All other commands:
        console       Try Terraform expressions at an interactive command prompt
        fmt           Reformat your configuration in the standard style
        force-unlock  Release a stuck lock on the current workspace
        get           Install or upgrade remote Terraform modules
        graph         Generate a Graphviz graph of the steps in an operation
        import        Associate existing infrastructure with a Terraform resource
        login         Obtain and save credentials for a remote host
        logout        Remove locally-stored credentials for a remote host
        output        Show output values from your root module
        providers     Show the providers required for this configuration
        refresh       Update the state to match remote systems
        show          Show the current state or a saved plan
        state         Advanced state management
        taint         Mark a resource instance as not fully functional
        test          Experimental support for module integration testing
        untaint       Remove the 'tainted' state from a resource instance
        version       Show the current Terraform version
        workspace     Workspace management
      
      Global options (use these before the subcommand, if any):
        -chdir=DIR    Switch to a different working directory before executing the
                      given subcommand.
        -help         Show this help output, or the help for a specified subcommand.
        -version      An alias for the "version" subcommand.
  2. For higher flexibility and security in permission management, we recommend that you create a Resource Access Management (RAM) user and grant the required permissions to the RAM user.

    1. Log on to the RAM console.

    2. Create a RAM user named Terraform and create an AccessKey pair for the user. For more information, see Create a RAM user and Obtain an Accesskey pair.

    3. Grant permissions to the RAM user. In this example, attach the AliyunRDSFullAccess, AliyunVPCFullAccess, and AliyunRAMFullAccess policies to the Terraform user. For more information, see Grant permissions to RAM users.

  3. Add environment variables to store authentication information.

    • For Linux or macOS operating systems, run the following commands:

      export ALICLOUD_ACCESS_KEY="*****"
      export ALICLOUD_SECRET_KEY="*****"
      export ALICLOUD_REGION="cn-hangzhou"
      Note

      The ALICLOUD_REGION environment variable specifies the region in which you want to manage your instance. The value of this environment variable must be the same as the region in which the instance resides. You can modify the value based on your business requirements.

    • For Windows operating systems, add the ALICLOUD_ACCESS_KEY, ALICLOUD_SECRET_KEY, and ALICLOUD_REGION environment variables.添加环境变量

  4. Create an execution directory and a Terraform template named terraform.tf.

    1. Create an execution directory and access the directory.

      Note

      You must create an execution directory for each Terraform project.

      • For Linux or macOS operating systems, run the following command:

        sudo mkdir /usr/local/terraform
        cd /usr/local/terraform
        Important

        If you use a non-root user, you must grant permissions on the terraform directory. Run the sudo chown -R <Current username >:<User group name> /usr/local/terraform command to change the owner of the terraform folder to the current user.

      • For Windows operating systems, create a folder such as rdspg in drive D and access the rdspg folder.

    2. In the execution directory, create a Terraform template named terraform.tf.

      • For Linux or macOS operating systems, run the following command:

        touch terraform.tf
      • For Windows operating systems, manually create the terraform.tf file.

  5. In this example, edit the terraform.tf file to query zone information.

    • For Linux or macOS operating systems, run the following command:

      vim terraform.tf
    • For Windows operating systems, manually edit the terraform.tf file.

    In this example, add the following information to the file:

    data "alicloud_db_zones" "queryzones" {
      instance_charge_type= "PostPaid"
      engine = "PostgreSQL"
      db_instance_storage_type = "cloud_essd"
    }
    Note

    After the vim command is run, you must press Esc and enter :wq to save the configuration and exit.

  6. Run the terraform init command in the execution directory to initialize the configuration.

    In this example, a Windows client is used, and the following result is returned:

    PS D:\rdspg> terraform init
    
    Initializing the backend...
    
    Initializing provider plugins...
    - Reusing previous version of hashicorp/alicloud from the dependency lock file
    - Using previously-installed hashicorp/alicloud v1.186.0
    
    ╷
    │ Warning: Additional provider information from registry
    │
    │ The remote registry returned warnings for registry.terraform.io/hashicorp/alicloud:
    │ - For users on Terraform 0.13 or greater, this provider has moved to aliyun/alicloud. Please update your source in
    │ required_providers.
    ╵
    
    Terraform has been successfully initialized!
    
    You may now begin working with Terraform. Try running "terraform plan" to see
    any changes that are required for your infrastructure. All Terraform commands
    should now work.
    
    If you ever set or change modules or backend configuration for Terraform,
    rerun this command to reinitialize your working directory. If you forget, other
    commands will detect it and remind you to do so if necessary.
  7. Run the terraform plan command to preview configurations.

    In this example, a Windows client is used, and the following result is returned:

    PS D:\rdspg> terraform plan
    data.alicloud_db_zones.queryzones: Reading...
    data.alicloud_db_zones.queryzones: Read complete after 4s [id=491248936]
    
    No changes. Your infrastructure matches the configuration.
    
    Terraform has compared your real infrastructure against your configuration and found no differences, so no changes are
    needed.
  8. Run the terraform apply command to apply the configuration.

    In this example, a Windows client is used, and the following result is returned:

    PS D:\rdspg> terraform apply
    data.alicloud_db_zones.queryzones: Reading...
    data.alicloud_db_zones.queryzones: Read complete after 0s [id=491248936]
    
    No changes. Your infrastructure matches the configuration.
    
    Terraform has compared your real infrastructure against your configuration and found no differences, so no changes are
    needed.
    
    Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
  9. Run the terraform show command to query the result.

    In this example, a Windows client is used, and the following result is returned:

    PS D:\rdspg> terraform show
    # data.alicloud_db_zones.queryzones:
    data "alicloud_db_zones" "queryzones" {
        db_instance_storage_type = "cloud_essd"
        engine                   = "PostgreSQL"
        id                       = "491248936"
        ids                      = [
            "cn-hangzhou-g",
            "cn-hangzhou-h",
            "cn-hangzhou-i",
            "cn-hangzhou-j",
            "cn-hangzhou-k",
        ]
        instance_charge_type     = "PostPaid"
        multi                    = false
        multi_zone               = false
        zones                    = [
            {
                id             = "cn-hangzhou-g"
                multi_zone_ids = []
            },
            {
                id             = "cn-hangzhou-h"
                multi_zone_ids = []
            },
            {
                id             = "cn-hangzhou-i"
                multi_zone_ids = []
            },
            {
                id             = "cn-hangzhou-j"
                multi_zone_ids = []
            },
            {
                id             = "cn-hangzhou-k"
                multi_zone_ids = []
            },
        ]
    }