使用Terraform来玩转华为云1-准备工作

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

下回分解实例创建!

购买华为云请点击立即购买

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值