开始使用 Terraform

演示如何安装 Redis Cloud 提供商并创建订阅。

在这里,您将学习如何使用 Redis Cloud Terraform Provider 创建订阅和数据库。

先决条件

  1. 安装 Terraform

  2. 如果您还没有 Redis Cloud 帐户,请创建一个

  3. 启用 Redis Cloud API

  4. 获取您的 Redis Cloud API 密钥。将它们设置为以下环境变量:

    • 设置REDISCLOUD_ACCESS_KEY添加到您的 API 账户密钥中。
    • 设置REDISCLOUD_SECRET_KEY添加到您的 API 用户密钥中。
  5. 设置付款方式

安装 Redis Cloud 提供程序

  1. 创建一个文件以包含名为main.tf.

  2. 转到 Redis Cloud Terraform 注册表

  3. 选择 Use Provider 并复制位于其中的 Terraform 代码。将代码粘贴到main.tf并保存文件。

    provider "rediscloud" {
    }
    
    # Example resource configuration
    resource "rediscloud_subscription" "example" {
       # ...
    }
    
  4. Run terraform init.

Create a Redis Cloud subscription with Terraform

In your Terraform configuration file, you can add resources and data sources to plan and create subscriptions and databases. See the Redis Cloud Terraform Registry documentation for more info about the resources and data sources you can use as part of the Redis Cloud provider.

The steps in this section show you how to plan and create a Redis Cloud Pro subscription with one database.

  1. Use the rediscloud_payment_method data source to get the payment method ID.

    # Get credit card details
    data "rediscloud_payment_method" "card" {
        card_type = "<Card type>"
        last_four_numbers = "<Last four numbers on the card>"
    }
    

    Example:

    data "rediscloud_payment_method" "card" {
       card_type = "Visa"
       last_four_numbers = "5625"
    }
    
  2. Define a rediscloud_subscription resource to create the subscription.

    # Create a subscription
    resource "rediscloud_subscription" "subscription-resource" {
            name = "subscription-name"
            payment_method_id = data.rediscloud_payment_method.card.id # If you want to pay with a marketplace account, replace this line with payment_method = 'marketplace'.
            memory_storage = "ram"
    
            # Specify the cloud provider information here
            cloud_provider {
                    provider = "<Cloud provider>"
                    region {
                            region = "<region>"
                            networking_deployment_cidr = "<CIDR>"
                    }
            }
    
            #Define the average database specification for databases in the subscription
            creation_plan {
                    memory_limit_in_gb = 2
                    quantity = 1
                    replication = true
                    throughput_measurement_by = "operations-per-second"
                    throughput_measurement_value = 20000
            }
    }
    

    Example:

    resource "rediscloud_subscription" "subscription-resource" {
         name = "redis-docs-sub"
         payment_method_id = data.rediscloud_payment_method.card.id # If you want to pay with a marketplace account, replace this line with payment_method = 'marketplace'.
         memory_storage = "ram"
    
         cloud_provider {
                 provider = "GCP"
                 region {
                         region = "us-west1"
                         networking_deployment_cidr = "192.168.0.0/24"
                 }
         }
    
         creation_plan {
                 memory_limit_in_gb = 2
                 quantity = 1
                 replication = true
                 throughput_measurement_by = "operations-per-second"
                 throughput_measurement_value = 20000
                 modules = ["RedisJSON"]
         }
    }
    
  3. Define a rediscloud_subscription_database resource to create a database.

    # Create a Database
    resource "rediscloud_subscription_database" "database-resource" {
        subscription_id = rediscloud_subscription.subscription-resource.id
        name = "database-name"
        memory_limit_in_gb = 2
        data_persistence = "aof-every-write"
        throughput_measurement_by = "operations-per-second"
        throughput_measurement_value = 20000
        replication = true
    
        alert {
        name = "dataset-size"
        value = 40
        }
        depends_on = [rediscloud_subscription.subscription-resource]
    
    }
    

    Example:

    resource "rediscloud_subscription_database" "database-resource" {
        subscription_id = rediscloud_subscription.subscription-resource.id
        name = "redis-docs-db"
        memory_limit_in_gb = 2
        data_persistence = "aof-every-write"
        throughput_measurement_by = "operations-per-second"
        throughput_measurement_value = 20000
        replication = true
    
        modules = [
        {
             name = "RedisJSON"
        }
        ]
    
        alert {
        name = "dataset-size"
        value = 40
        }
       depends_on = [rediscloud_subscription.subscription-resource]
    
    }
    
  4. Run terraform plan to check for any syntax errors.

    $ terraform plan
    data.rediscloud_payment_method.card: Reading...
    data.rediscloud_payment_method.card: Read complete after 1s [id=8859]
    
    Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following
    symbols:
    + create
    
    Terraform will perform the following actions:
    
        # rediscloud_subscription.subscription-resource will be created
        + resource "rediscloud_subscription" "subscription-resource" {
            [...]
        }
    
        # rediscloud_subscription_database.database-resource will be created
        + resource "rediscloud_subscription_database" "database-resource" {
            [...]
        }
    
    Plan: 2 to add, 0 to change, 0 to destroy.
    
  5. Run terraform apply to apply the changes and enter yes to confirm when prompted.

    This will take some time. You will see messages in your terminal while the subscription and database are being created:

    rediscloud_subscription.subscription-resource: Creating...
    rediscloud_subscription.subscription-resource: Still creating... [10s elapsed]
    rediscloud_subscription.subscription-resource: Still creating... [20s elapsed]
    rediscloud_subscription.subscription-resource: Still creating... [30s elapsed]
    

    When provisioning is complete, you will see a message in your terminal:

    Apply complete! Resources: 2 added, 0 changed, 0 destroyed.
    

    View the Redis Cloud console to verify your subscription and database creation.

  6. If you want to remove these sample resources, run terraform destroy.

More info

RATE THIS PAGE
Back to top ↑