安装并配置Terraform
使用Terraform前,您需要按照以下步骤安装并配置Terraform。
前往Terraform官网下载适用于您的操作系统的程序包。
将程序包解压到/usr/local/bin。
如果将可执行文件解压到其他目录,则需要将路径加入到全局变量。
执行以下命令验证是否已成功安装Terraform。
terraform
成功返回示例如下。
Usage: terraform [-version] [-help] <command> [args]
创建RAM用户,并为其授权。
重要 阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户的AccessKey配置Terraform工具。
登录RAM控制台。
创建名为Terraform的RAM用户,并为该用户创建 AccessKey。
为RAM用户授权。
您可以根据实际的情况为Terraform授予合适的管理权限。具体步骤参见为RAM用户授权。
执行以下命令为Terraform项目创建工作目录。
重要 每个Terraform项目都需要1个独立的工作目录。
mkdir terraform-test
执行以下命令进入工作目录terraform-test。
cd terraform-test
Terraform在执行时,会读取该目录空间下所有*.tf和*.tfvars文件。因此,您可以按照实际用途将配置信息写入到不同的文件中。常用配置文件如下。
文件 | 说明 |
provider.tf | provider配置 |
terraform.tfvars | 配置provider要用到的变量 |
variable.tf | 通用变量 |
resource.tf | 资源定义 |
data.tf | 包文件定义 |
output.tf | 输出 |
本文以provider配置文件provider.tf为例。
执行以下命令创建身份认证信息配置文件provider.tf。
vim provider.tf
配置文件信息示例如下。
provider "alicloud" {
region = "cn-beijing"
access_key = "LTA**********NO2"
secret_key = "MOk8x0*********************wwff"
}
执行以下命令初始化工作目录terraform-test。
重要 每个Terraform项目在新建Terraform工作目录并创建配置文件后,都需要初始化工作目录。
terraform init
成功返回示例如下。
Initializing provider plugins...
- Checking for available provider plugins on https://releases.hashicorp.com...
- Downloading plugin for provider "alicloud" (1.25.0)...
The following providers do not have any version constraints in configuration,
so the latest version was installed.
To prevent automatic upgrades to new major versions that may contain breaking
changes, it is recommended to add version = "..." constraints to the
corresponding provider blocks in configuration, with the constraint strings
suggested below.
* provider.alicloud: version = "~> 1.25"
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管理OSS
Terraform安装完成之后,您就可以通过Terraform的操作命令管理OSS了,下面介绍几个常用的操作命令。
terraform plan
terraform plan用于预览将要执行的操作。该命令允许您在正式执行配置文件之前,查看将要执行哪些操作。
使用terraform plan预览创建Bucket的操作示例如下。
执行以下命令创建配置文件test.tf。
vim test.tf
配置文件信息示例如下。
resource "alicloud_oss_bucket" "bucket-acl"{
bucket = "demo-2023"
acl = "private"
}
执行以下命令查看将会执行的操作。
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:
+ alicloud_oss_bucket.bucket-acl
id: <computed>
acl: "private"
bucket: "demo-2023"
creation_date: <computed>
extranet_endpoint: <computed>
intranet_endpoint: <computed>
location: <computed>
logging_isenable: "true"
owner: <computed>
referer_config.#: <computed>
storage_class: <computed>
Plan: 1 to add, 0 to change, 0 to destroy.
------------------------------------------------------------------------
Note: You didn't specify an "-out" parameter to save this plan, so Terraform
can't guarantee that exactly these actions will be performed if
"terraform apply" is subsequently run.
terraform apply
terraform apply用于执行工作目录中的配置文件。
使用terraform apply创建Bucket的操作示例如下。
执行以下命令创建配置文件test.tf。
vim test.tf
配置文件信息示例如下。
resource "alicloud_oss_bucket" "bucket-acl"{
bucket = "demo-2023"
acl = "private"
}
执行以下命令执行配置文件。
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:
+ alicloud_oss_bucket.bucket-acl
id: <computed>
acl: "private"
bucket: "demo-2023"
creation_date: <computed>
extranet_endpoint: <computed>
intranet_endpoint: <computed>
location: <computed>
logging_isenable: "true"
owner: <computed>
referer_config.#: <computed>
storage_class: <computed>
Plan: 1 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
alicloud_oss_bucket.bucket-acl: Creating...
acl: "" => "private"
bucket: "" => "demo-2023"
creation_date: "" => "<computed>"
extranet_endpoint: "" => "<computed>"
intranet_endpoint: "" => "<computed>"
location: "" => "<computed>"
logging_isenable: "" => "true"
owner: "" => "<computed>"
referer_config.#: "" => "<computed>"
storage_class: "" => "<computed>"
alicloud_oss_bucket.bucket-acl: Creation complete after 1s (ID: demo-2023)
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
说明 此配置执行后,如果demo-2023这个Bucket不存在,则创建一个Bucket。如果已存在,且为Terraform创建的空Bucket,则会删除原有Bucket并重新生成。
terraform destroy
terraform destroy可删除通过Terraform创建的空Bucket。
使用terraform import删除通过Terraform创建的空Bucket的操作示例如下。
执行以下命令创建配置文件test.tf。
vim test.tf
配置文件信息示例如下。
resource "alicloud_oss_bucket" "bucket-acl"{
bucket = "demo-2023"
acl = "private"
}
执行以下命令来执行配置文件。
terraform destroy
成功返回示例如下。
Terraform used the selected providers to generate the following execution plan.
Resource actions are indicated with the following symbols:
- destroy
Terraform will perform the following actions:
# alicloud_oss_bucket.bucket-acl will be destroyed
- resource "alicloud_oss_bucket" "bucket-acl" {
- acl = "private" -> null
- bucket = "demo-2023" -> null
- creation_date = "2023-01-04" -> null
- extranet_endpoint = "oss-cn-hangzhou.aliyuncs.com" -> null
- force_destroy = false -> null
- id = "demo-2023" -> null
- intranet_endpoint = "oss-cn-hangzhou-internal.aliyuncs.com" -> null
- location = "oss-cn-hangzhou" -> null
- owner = "1379***" -> null
- redundancy_type = "LRS" -> null
- storage_class = "Standard" -> null
- tags = {} -> null
}
Plan: 0 to add, 0 to change, 1 to destroy.
Do you really want to destroy all resources?
Terraform will destroy all your managed infrastructure, as shown above.
There is no undo. Only 'yes' will be accepted to confirm.
Enter a value: yes
alicloud_oss_bucket.bucket-acl: Destroying... [id=demo-2023]
alicloud_oss_bucket.bucket-acl: Destruction complete after 2s
Destroy complete! Resources: 1 destroyed.
terraform import
如果Bucket不是通过Terraform创建,可通过terraform import导入现有的Bucket。
使用terraform import导入Bucket的操作示例如下。
执行以下命令创建配置文件。
vim main.tf
配置文件信息示例如下。
resource "alicloud_oss_bucket" "bucket" {
bucket = "aliyundoc-demo"
acl = "private"
}
执行以下命令执行配置文件。
terraform import alicloud_oss_bucket.bucket aliyundoc-demo
成功返回示例如下。
alicloud_oss_bucket.bucket: Importing from ID "aliyundoc-demo"...
alicloud_oss_bucket.bucket: Import prepared!
Prepared alicloud_oss_bucket for import
alicloud_oss_bucket.bucket: Refreshing state... [id=aliyundoc-demo]
Import successful!
The resources that were imported are shown above. These resources are now in
your Terraform state and will henceforth be managed by Terraform.