You need to enable JavaScript to run this app.
文档中心
缓存数据库 Redis 版

缓存数据库 Redis 版

复制全文
下载 pdf
Terraform 参考
Terraform 集成示例
复制全文
下载 pdf
Terraform 集成示例
本文以通过 Terraform 创建 Redis 实例为例,介绍如何安装使用 Terraform。​
注意事项​
  • 火山引擎主账号具有资源的所有权限,一旦发生泄露将面临重大风险。建议您使用子用户(IAM 用户),并为该子用户创建单独的 AK/SK 来使用 Redis 的相关资源。详细操作步骤,请参见创建子用户和 API 访问密钥管理。​
  • 为子用户授权 Redis 云服相关权限,例如 RedisFullAccess(缓存数据库 Redis 版的全部管理权限)、VPCFullAccess(私有网络(VPC)全部管理权限)等权限。更多详情,请参见通过子用户使用 Redis 服务。​
  • 本文示例包含的部分资源会产生一定费用(如新建 Redis 实例的费用 ),如不再需要通过 Terraform 管理资源时,请及时通过 terraform destroy 命令释放资源。​
操作步骤​
步骤一:安装 Terraform 并配置环境变量​
  1. 从 Terraform 官网下载安装所需的版本。具体操作,请参见 Terraform 官方文档。​
说明​
确保下载安装的 Terraform 版本不低于 v0.13。您可以在安装成功后通过 terraform version 命令查看版本信息。​
  1. 在 Terraform 项目的文件目录下,创建一个名为 main.tf 的配置文件,并在文件中配置 Provider(服务提供商)的信息,包括 Provider 的源和版本。​
说明​
您可以从 Terraform-volcengine 页面获取 version 信息。关于 Provider 的更多信息,请参见 Provider。​
  • ​
    terraform {​
    required_providers {​
    volcengine = {​
    source = "volcengine/volcengine"​
    version = "0.0.176"​
    }​
    }​
    }​
    ​
  1. 在命令行中执行如下命令创建环境变量,存放身份认证信息。​
  • 使用 export 命令配置的临时环境变量仅当前会话有效,当会话退出之后所设置的环境变量将会丢失。若需长期保留环境变量,可将 export 命令配置到对应操作系统的启动配置文件中。​
  • ​
    export VOLCENGINE_ACCESS_KEY="AK************"​
    export VOLCENGINE_SECRET_KEY="************"​
    export VOLCENGINE_REGION="cn-beijing"​
    ​
  • 参数说明如下表所示。​
  • ​
    参数​
    说明​
    VOLCENGINE_ACCESS_KEY​
    您火山引擎账号的 AccessKey ID(AK)。获取方式,请参见访问密钥使用指南。​
    VOLCENGINE_SECRET_KEY​
    您火山引擎账号的 Secret Access Key(SK)。获取方式,请参见访问密钥使用指南。​
    VOLCENGINE_REGION​
    需要使用 Redis 资源的地域。缓存数据库 Redis 版已开通服务的地域详情,请参见服务地址。​
    ​
步骤二:编写模板​
进入 Terraform 项目的文件目录并打开步骤一中创建的 main.tf文件进行编辑,在文件中追加如下内容,用于创建一个拥有如下配置的 Redis 实例:​
  • 数据库版本号: 5.0。​
  • 可用内存:8 GiB(即 8192 MiB)。​
  • 分片数:2。​
  • 每个分片中的节点数:3。​
  • 在华北 2(北京)地域下使用多可用区部署,其中主节点部署在可用区 A,2 个从节点均在可用区 B。​
  • 绑定 department:game 和 owner:Mary 两个标签。​
代码示例如下。​
说明​
如下代码中使用的资源类型的配置详情,请参见 volcengine_redis_instance。​
​
# query available zones in current region​
data "volcengine_zones" "foo" {​
}​
​
# create vpc​
resource "volcengine_vpc" "foo" {​
vpc_name = "tf-test-vpc-mary"​
cidr_block = "172.16.0.0/16"​
dns_servers = ["114.114.114.114", "8.8.8.8"]​
project_name = "default"​
}​
​
# create subnet​
resource "volcengine_subnet" "foo" {​
subnet_name = "tf-test-subnet-mary"​
cidr_block = "172.16.0.0/24"​
zone_id = data.volcengine_zones.foo.zones[0].id​
vpc_id = volcengine_vpc.foo.id​
}​
​
# create redis instance​
resource "volcengine_redis_instance" "foo" {​
instance_name = "tf-test-redis-mary"​
sharded_cluster = 1​
password = "Test@pWd"​
node_number = 3​
shard_capacity = 8192​
shard_number = 2​
engine_version = "5.0"​
subnet_id = volcengine_subnet.foo.id​
deletion_protection = "disabled"​
charge_type = "PostPaid"​
port = 6391​
project_name = "default"​
tags {​
key = "department"​
value = "game"​
}​
tags {​
key = "owner"​
value = "Mary"​
}​
multi_az = "enabled"​
configure_nodes {​
az = "cn-beijing-a"​
}​
configure_nodes {​
az = "cn-beijing-b"​
}​
configure_nodes {​
az = "cn-beijing-b"​
}​
}​
​
步骤三:运行模板​
  1. 在 main.tf 文件所在的目录下,执行如下命令来初始化和加载模块。​
  • ​
    terraform init​
    ​
  • 预计返回结果示例如下。​
  • ​
    Initializing the backend...​
    Initializing provider plugins...​
    - Finding volcengine/volcengine versions matching "0.0.176"...​
    - Installing volcengine/volcengine v0.0.176...​
    - Installed volcengine/volcengine v0.0.176 (self-signed, key ID ****)​
    Partner and community providers are signed by their developers.​
    ....​
    ​
    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.​
    ​
  1. 执行如下命令,验证模板语法是否正确。​
  • ​
    terraform validate​
    ​
  • 预计返回结果示例如下。​
  • ​
    Success! The configuration is valid.​
    ​
  1. 执行如下命令创建执行计划。​
  • ​
    terraform plan​
    ​
  • 预计返回结果示例如下。​
  • ​
    data.volcengine_zones.foo: Reading...​
    data.volcengine_zones.foo: Read complete after 0s [id=****]​
    ​
    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:​
    ​
    # volcengine_redis_instance.foo will be created​
    + resource "volcengine_redis_instance" "foo" {​
    + additional_bandwidth = (known after apply)​
    + addr_type = (known after apply)​
    + apply_immediately = true​
    + backup_active = true​
    + backup_hour = 6​
    + backup_period = [​
    + 1,​
    + 2,​
    + 3,​
    ]​
    + backup_point_name = (known after apply)​
    + charge_type = "PostPaid"​
    + create_backup = false​
    + deletion_protection = "disabled"​
    + engine_version = "5.0"​
    + id = (known after apply)​
    + instance_name = "tf-test-redis-mary"​
    + max_connections = (known after apply)​
    + multi_az = "enabled"​
    + new_address_prefix = (known after apply)​
    + new_port = (known after apply)​
    + node_number = 3​
    + password = (sensitive value)​
    + port = 6391​
    + project_name = "default"​
    + shard_capacity = 8192​
    + shard_number = 2​
    + sharded_cluster = 1​
    + subnet_id = (known after apply)​
    + time_scope = (known after apply)​
    + upgrade_region_domain = (known after apply)​
    + vpc_auth_mode = "close"​
    ​
    + configure_nodes {​
    + az = "cn-beijing-a"​
    }​
    + configure_nodes {​
    + az = "cn-beijing-b"​
    }​
    + configure_nodes {​
    + az = "cn-beijing-b"​
    }​
    ​
    + tags {​
    + key = "department"​
    + value = "game"​
    }​
    + tags {​
    + key = "owner"​
    + value = "Mary"​
    }​
    }​
    ​
    # volcengine_subnet.foo will be created​
    + resource "volcengine_subnet" "foo" {​
    + cidr_block = "172.16.0.0/24"​
    + creation_time = (known after apply)​
    + enable_ipv6 = (known after apply)​
    + id = (known after apply)​
    + ipv6_cidr_block = (known after apply)​
    + status = (known after apply)​
    + subnet_name = "tf-test-subnet-mary"​
    + vpc_id = (known after apply)​
    + zone_id = "cn-beijing-a"​
    }​
    ​
    # volcengine_vpc.foo will be created​
    + resource "volcengine_vpc" "foo" {​
    + account_id = (known after apply)​
    + associate_cens = (known after apply)​
    + auxiliary_cidr_blocks = (known after apply)​
    + cidr_block = "172.16.0.0/16"​
    + creation_time = (known after apply)​
    + dns_servers = [​
    + "114.114.114.114",​
    + "8.8.8.8",​
    ]​
    + enable_ipv6 = (known after apply)​
    + id = (known after apply)​
    + ipv6_cidr_block = (known after apply)​
    + nat_gateway_ids = (known after apply)​
    + project_name = "default"​
    + route_table_ids = (known after apply)​
    + secondary_cidr_blocks = (known after apply)​
    + security_group_ids = (known after apply)​
    + status = (known after apply)​
    + subnet_ids = (known after apply)​
    + update_time = (known after apply)​
    + vpc_id = (known after apply)​
    + vpc_name = "tf-test-vpc-mary"​
    }​
    ​
    Plan: 3 to add, 0 to change, 0 to destroy.​
    ​
    ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────​
    ​
    Note: You didn't use the -out option to save this plan, so Terraform can't guarantee to take exactly these actions if you run "terraform​
    apply" now. ​
    ​
  1. 执行如下命令部署模板。​
  • ​
    terraform apply​
    ​
  • 部分返回结果示例如下。​
  • ​
    Plan: 3 to add, 0 to change, 0 to destroy.​
    ​
    Do you want to perform these actions?​
    Terraform will perform the actions described above.​
    Only 'yes' will be accepted to approve.​
    ​
    Enter a value: ​
    ​
  • 此时,您需要输入 yes 即可变更资源。​
  • 输入 yes 后返回的结果示例如下。​
  • ​
    volcengine_vpc.foo: Creating...​
    volcengine_vpc.foo: Creation complete after 2s [id=vpc-2f8f8xx7t5pmo4f4pzz3c****]​
    volcengine_subnet.foo: Creating...​
    volcengine_subnet.foo: Creation complete after 2s [id=subnet-2f8f92quj2jgg4f4pzymb****]​
    volcengine_redis_instance.foo: Creating...​
    ...​
    volcengine_redis_instance.foo: Still creating... [14m11s elapsed]​
    volcengine_redis_instance.foo: Still creating... [14m21s elapsed]​
    volcengine_redis_instance.foo: Creation complete after 14m29s [id=redis-cnlfde3q47ffh****]​
    Apply complete! Resources: 3 added, 0 changed, 0 destroyed.​
    ​
  1. 创建完成后,您可以通过 OpenAPI 或者 Redis 控制台查看新创建的实例。具体操作步骤,请参见 DescribeDBInstanceDetail 和查看实例信息。​
相关文档​
​
最近更新时间:2026.10.29 20:32:22
这个页面对您有帮助吗?
有用
有用
无用
无用