Terraform是一种开源工具,用于安全高效地创建和管理云资源。本文为您演示如何通过Terraform管理CSG。
前提条件
为了降低信息安全风险,建议使用最小权限的RAM用户完成此教程的操作。更多信息,请参见RAM鉴权。
已具备Terraform运行环境。您可以选择以下任一方式来使用Terraform。本文采用在本地Linux系统中安装和配置Terraform的方式。
Explorer:阿里云提供了Terraform的在线运行环境,您无需安装Terraform,登录后即可在线使用和体验Terraform。适用于零成本、快速、便捷地体验和调试Terraform的场景。
Cloud Shell:阿里云Cloud Shell中预装了Terraform的组件,并已配置好身份凭证,您可直接在Cloud Shell中运行Terraform的命令。适用于低成本、快速、便捷地访问和使用Terraform的场景。
在本地安装和配置Terraform:适用于网络连接较差或需要自定义开发环境的场景。
创建网关集群
创建一个工作目录,并在该工作目录中创建名为main.tf的配置文件,然后将以下代码复制到main.tf中。
variable "region"{ # 资源将要部署的地域 default = "cn-hangzhou" } variable "access_key"{ # AccessKey Id default = "LTAIUrZCw3********" } variable "secret_key"{ # AccessKey Secret default = "zfwwWAMWIAiooj14GQ2*************" } provider "alicloud" { access_key = var.access_key secret_key = var.secret_key region = var.region } terraform { required_providers { alicloud = { source = "aliyun/alicloud" version = "1.240.0" } } } resource "alicloud_cloud_storage_gateway_storage_bundle" "default" { storage_bundle_name = "tf_storage_bundle" description = "tf" }
执行如下命令,初始化Terraform运行环境。
terraform init
返回信息如下,则Terraform初始化成功。
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.
执行如下命令,开始执行代码。
terraform apply
在执行过程中,根据提示输入
yes
并按下Enter键,等待命令执行完成,若出现以下信息,则表示网关集群创建完成。You can apply this plan to save these new output values to the Terraform state, without changing any real infrastructure. 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 Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
验证结果。
您可以在工作目录中,使用以下命令查询网关集群的详细信息:
terraform show
创建网关
创建一个新的工作目录,并在该工作目录中创建名为main.tf的配置文件,然后将以下代码复制到main.tf中。
variable "region"{ # 资源将要部署的地域 default = "cn-hangzhou" } variable "access_key"{ # AccessKey Id default = "LTAIUrZCw3********" } variable "secret_key"{ # AccessKey Secret default = "zfwwWAMWIAiooj14GQ2*************" } provider "alicloud" { access_key = var.access_key secret_key = var.secret_key region = var.region } terraform { required_providers { alicloud = { source = "aliyun/alicloud" version = "1.240.0" } } } resource "alicloud_cloud_storage_gateway_gateway" "default" { release_after_expiration = false # 网关规格,请按需更改 gateway_class = "Basic" # 网关名称,请按需更改 gateway_name = "tf-terraform-file" location = "Cloud" payment_type = "PayAsYouGo" reason_type = "File" # 请替换为您的网关集群ID storage_bundle_id = "sb-00095gtq2442k******" type = "File" # 请替换为您的交换机ID vswitch_id = "vsw-wz9sd6vfwnh*******" # 公网带宽,请按需更改 public_network_bandwidth = 5 }
执行如下命令,初始化Terraform运行环境。
terraform init
返回信息如下,则Terraform初始化成功。
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.
执行如下命令,开始执行代码。
terraform apply
在执行过程中,根据提示输入
yes
并按下Enter键,等待命令执行完成,若出现以下信息,则表示文件网关创建成功。You can apply this plan to save these new output values to the Terraform state, without changing any real infrastructure. 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 Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
验证结果。
您可以在工作目录中,使用以下命令查询文件网关的详细信息:
terraform show
创建一个新的工作目录,并在该工作目录中创建名为main.tf的配置文件,然后将以下代码复制到main.tf中。
variable "region"{ # 资源将要部署的地域 default = "cn-hangzhou" } variable "access_key"{ # AccessKey Id default = "LTAIUrZCw3********" } variable "secret_key"{ # AccessKey Secret default = "zfwwWAMWIAiooj14GQ2*************" } provider "alicloud" { access_key = var.access_key secret_key = var.secret_key region = var.region } terraform { required_providers { alicloud = { source = "aliyun/alicloud" version = "1.240.0" } } } resource "alicloud_cloud_storage_gateway_gateway" "default" { release_after_expiration = false # 网关规格,请按需更改 gateway_class = "Basic" # 网关名称,请按需更改 gateway_name = "tf-terraform-block" location = "Cloud" payment_type = "PayAsYouGo" reason_type = "File" # 请替换为您的网关集群ID storage_bundle_id = "sb-00095gtq2442k******" type = "Iscsi" # 请替换为您的交换机ID vswitch_id = "vsw-wz9sd6vfwnh*******" # 公网带宽,请按需更改 public_network_bandwidth = 5 }
执行如下命令,初始化Terraform运行环境。
terraform init
返回信息如下,则Terraform初始化成功。
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.
执行如下命令,开始执行代码。
terraform apply
在执行过程中,根据提示输入
yes
并按下Enter键,等待命令执行完成,若出现以下信息,则表示块网关创建成功。You can apply this plan to save these new output values to the Terraform state, without changing any real infrastructure. 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 Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
验证结果。
您可以在工作目录中,使用以下命令查询块网关的详细信息:
terraform show
创建缓存
创建一个新的工作目录,并在该工作目录中创建名为main.tf的配置文件,然后将以下代码复制到main.tf中。
variable "region"{ # 资源将要部署的地域 default = "cn-hangzhou" } variable "access_key"{ # AccessKey Id default = "LTAIUrZCw3********" } variable "secret_key"{ # AccessKey Secret default = "zfwwWAMWIAiooj14GQ2*************" } provider "alicloud" { access_key = var.access_key secret_key = var.secret_key region = var.region } terraform { required_providers { alicloud = { source = "aliyun/alicloud" version = "1.240.0" } } } resource "alicloud_cloud_storage_gateway_gateway_cache_disk" "default" { # 请更改为您的网关ID gateway_id = "gw-0009t9hkx******" # 缓存盘类型,请按需更改 cache_disk_category = "cloud_essd" # 缓存盘容量,请按需更改 cache_disk_size_in_gb = "100" # 性能级别,仅当盘类型为ESSD云盘才需要设置 performance_level = "PL1" }
执行如下命令,初始化Terraform运行环境。
terraform init
返回信息如下,则Terraform初始化成功。
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.
执行如下命令,开始执行代码。
terraform apply
在执行过程中,根据提示输入
yes
并按下Enter键,等待命令执行完成,若出现以下信息,则表示缓存盘创建成功。You can apply this plan to save these new output values to the Terraform state, without changing any real infrastructure. 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 Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
验证结果。
您可以在工作目录中,使用以下命令查询缓存盘的详细信息:
terraform show
创建共享或卷
创建一个新的工作目录,并在该工作目录中创建名为main.tf的配置文件,然后将以下代码复制到main.tf中。
variable "region"{ # 资源将要部署的地域 default = "cn-hangzhou" } variable "access_key"{ # AccessKey Id default = "LTAIUrZCw3********" } variable "secret_key"{ # AccessKey Secret default = "zfwwWAMWIAiooj14GQ2*************" } provider "alicloud" { access_key = var.access_key secret_key = var.secret_key region = var.region } terraform { required_providers { alicloud = { source = "aliyun/alicloud" version = "1.240.0" } } } resource "alicloud_cloud_storage_gateway_gateway_file_share" "default" { # 共享名称,请按需更改 gateway_file_share_name = "tf-share-terraform" # 请替换为您的网关ID gateway_id = "gw-000e9g71y8o******" # 请替换为您创建的缓存盘ID local_path = "/dev/vdb" # 请替换为您的OSS Bucket oss_bucket_name = "test_bucket" # 请替换为您的OSS Endpoint oss_endpoint = "oss-cn-hangzhou-internal.aliyuncs.com" # 共享协议,NFS或者SMB protocol = "NFS" }
执行如下命令,初始化Terraform运行环境。
terraform init
返回信息如下,则Terraform初始化成功。
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.
执行如下命令,开始执行代码。
terraform apply
在执行过程中,根据提示输入
yes
并按下Enter键,等待命令执行完成,若出现以下信息,则表示共享创建成功。You can apply this plan to save these new output values to the Terraform state, without changing any real infrastructure. 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 Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
验证结果。
您可以在工作目录中,使用以下命令查询共享的详细信息:
terraform show
创建一个新的工作目录,并在该工作目录中创建名为main.tf的配置文件,然后将以下代码复制到main.tf中。
variable "region"{ # 资源将要部署的地域 default = "cn-hangzhou" } variable "access_key"{ # AccessKey Id default = "LTAIUrZCw3********" } variable "secret_key"{ # AccessKey Secret default = "zfwwWAMWIAiooj14GQ2*************" } provider "alicloud" { access_key = var.access_key secret_key = var.secret_key region = var.region } terraform { required_providers { alicloud = { source = "aliyun/alicloud" version = "1.240.0" } } } resource "alicloud_cloud_storage_gateway_gateway_block_volume" "default" { # 卷名称,请按需更改 gateway_block_volume_name = "tfvolume1" # 请替换为您的网关ID gateway_id = "gw-000e9g71y8o******" # 卷工作模式,缓存模式或者写透模式 cache_mode = "Cache" # 请替换为您创建的缓存盘ID local_path = "/dev/vdb" # 请替换为您的OSS Bucket oss_bucket_name = "test_bucket" # 请替换为您的OSS Endpoint oss_endpoint = "oss-cn-hangzhou-internal.aliyuncs.com" # 块存储协议 protocol = "iSCSI" #存储分配单元,8K/16K/32K/64K/128K chunk_size = "8192" # 容量 size = "100" }
执行如下命令,初始化Terraform运行环境。
terraform init
返回信息如下,则Terraform初始化成功。
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.
执行如下命令,开始执行代码。
terraform apply
在执行过程中,根据提示输入
yes
并按下Enter键,等待命令执行完成,若出现以下信息,则表示卷创建成功。You can apply this plan to save these new output values to the Terraform state, without changing any real infrastructure. 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 Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
验证结果。
您可以在工作目录中,使用以下命令查询卷的详细信息:
terraform show
清理资源
当您不再需要上述通过Terraform创建或管理的资源时,请运行以下命令以释放资源。关于terraform destroy
的更多信息,请参见Terraform常用命令。
terraform destroy
相关文档
Terrafrom介绍,请参见Terraform产品介绍。
当您遇到由于网络延迟等原因造成的 terraform init 超时,导致无法正常下载 Provider 等情况时,请参见Terraform Init 加速方案配置。
- 本页导读 (1)
- 前提条件
- 创建网关集群
- 创建网关
- 创建缓存
- 创建共享或卷
- 清理资源
- 相关文档