terraform 工具准备
https://www.terraform.io/downloads.html
选择对应平台的版本,例如 macOS 或者 Linux
下载,解压,可以放到
/usr/local/bin/
目录下
$ terraform -v
Terraform v0.11.11
Your version of Terraform is out of date! The latest version
is 0.11.13. You can update by downloading from www.terraform.io/downloads.html
确认可以正常执行
准备华为云的access_key和secret_key
华为云控制台 我的凭证 管理访问密钥 新增访问密钥
然后设置环境变量
export TF_VAR_hw_access_key=“XXX"
export TF_VAR_hw_secret_key=“YYY"
export TF_VAR_hw_region=cn-east-2
准备工程文件
$ cat main.tf
provider "huaweicloud" {
# tenant_name = "cn-east-2"
auth_url = "https://iam.myhwclouds.com/v3"
insecure = "false"
region = "${var.hw_region}"
access_key = "${var.hw_access_key}"
secret_key = "${var.hw_secret_key}"
# version = "1.4.0"
}
data "huaweicloud_images_image_v2" "os" {
name = "${var.image_name}"
most_recent = true
}
$ cat vars.tf
variable "hw_access_key" {
type = "string"
}
variable "hw_secret_key" {
type = "string"
}
variable "hw_region" {
default = "cn-east-2"
description = "The region name where the resource will be created"
}
variable "hw_az" {
default = "cn-east-2a"
description = "The availability zone name where the resource will be created"
}
variable "flavor_name" {
default = "t6.small.1"
description = "Define the instance flavor / instance type"
}
variable "image_name" {
default = "CentOS 7.6 64bit"
description = "The OS image of instance that we will use"
}
$ cat output.tf
output "os_image_id" {
value = "${data.huaweicloud_images_image_v2.os.id}"
}
开搞
初始化 Terraform (下载华为云插件)
$ terraform init
Initializing provider plugins...
- Checking for available provider plugins on https://releases.hashicorp.com...
- Downloading plugin for provider "huaweicloud" (1.4.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.huaweicloud: version = "~> 1.4"
Terraform has been successfully initialized!
Plan
$ 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.
data.huaweicloud_images_image_v2.os: Refreshing state...
------------------------------------------------------------------------
No changes. Infrastructure is up-to-date.
Apply,可以打印我们的output
$ terraform apply
data.huaweicloud_images_image_v2.os: Refreshing state...
Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
Outputs:
os_image_id = 9526f9b7-423c-4fdc-92ad-f1630a524652
下回分解实例创建!