All Documents
Current Document

Content is empty

If you don't find the content you expect, please try another search term

Documentation

Terraform to manage KRDS

Last updated:2023-03-21 10:48:35

This topic describes how to install and configure Terraform and use Terraform to manage KRDS.

Install and configure Terraform

Before you use Terraform, install and configure it by performing the following operations.

Download the Terraform package for your operating system from the Terraform official website

The following example describes how to install Terraform in Linux. Decompress the package to the /usr/local/bin directory. To decompress the executable file to another directory, you must add the path to global variables. Run Terraform to verify the path configuration. If a list of available Terraform options is displayed, the installation is complete.

[root@test bin]#terraform
Usage: terraform [-version] [-help] <command> [args]

Create an IAM user and grant permissions to the user

Log in to the Identity and Access Management (IAM) console. Create an IAM user named Terraform and create an AccessKey for the user. For more information, see Manage IAM users. Grant permissions to the IAM user. You can assign management permissions to the IAM user as required. For more information, see Manage permissions. Note: For security reasons, do not use the AccessKey of your Kingsoft Cloud account to configure Terraform.

Create a test directory

Each Terraform project requires an independent working directory. In this example, create a test directory named terraform-test.

[root@test bin]#mkdir terraform-test

Go to the terraform-test directory.

[root@test bin]#cd terraform-test
[root@test terraform-test]#

Create configuration files

During running, Terraform reads all .tf and .tfvars files from the test directory. You can write configuration information to different files as needed. The following files are some common configuration files:

provider.tf              -providers
terraform.tfvars         -provider variables
variable.tf              -general variables
resource.tf              -resource definitions
data.tf                  -package file definitions
output.tf                -output

For example, you can create the provider.tf file that contains your authentication information in the following format:

provider.tf
provider "ksyun" {
    region           = "cn-beijing-6"
    access_key  = "LTA**********NO2"
    secret_key   = "MOk8x0*********************wwff"
}

Initialize the working directory

[root@test terraform-test]#terraform init

Initializing provider plugins...
- Checking for available provider plugins on https://releases.hashicorp.com...
- Downloading plugin for provider "ksyun" ...

Note: After you create the working directory and configuration files for a Terraform project, you must initialize the working directory. After the preceding operations are completed, you can use Terraform.

Use Terraform to manage KRDS

After Terraform is installed, you can run Terraform commands to manage KRDS. The following commands are commonly used: terraform plan: You can run this command to view the operations to be performed by a configuration file. Assume that you add a configuration file named test.tf that is used to create an KRDS instance:

test.tf
resource "ksyun_krds" "test-ss-1"{
  output_file = "output_file"
  dbinstanceclass= "db.ram.16|db.disk.500"
  dbinstancename = "ksyun_rds_1"
  dbinstancetype = "HRDS"
  engine = "mysql"
  engineversion = "5.7"
  masterusername = "admin"
  masteruserpassword = "123qweASD"
  vpcid = "cb***************************8aae"
  subnetid = "87**************************eb47"
  billtype = "DAY"
}

You can run the terraform plan command to view the operations to be performed by the test.tf configuration file.

[root@test terraform-test]# terraform plan
Refreshing Terraform state in-memory prior to plan...
The refreshed state will be used to calculate this plan, but will not be
persisted to local or remote state storage.

------------------------------------------------------------------------

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create
Terraform will perform the following actions:
....
Plan: 1 to add, 0 to change, 0 to destroy.

terraform apply: You can run this command to run a configuration file in the working directory. For example, you can add a configuration file to create a KRDS instance named test-ss-1. Then, you can run the terraform apply command to run the configuration file.

[root@test terraform-test]#terraform  apply

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create
Terraform will perform the following actions:
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

Note: After the configuration file is run, if the test-ss-1 instance does not exist, a new KRDS instance is created.

To delete the KRDS instance, delete the test-ss-1 resource from the configuration file.

Terraform allows you update KRDS instances.

On this page
Pure ModeNormal Mode

Pure Mode

Click to preview the document content in full screen
Feedback