terraform 阿里雲基本使用

1. 預備環境
安裝了terraform 的軟件的操做系統(windows linux mac 都可)
具備阿里雲帳戶的 access_key  secret_key
2. 配置
// terraform 的配置相對比較隨意,可是有幾個必須注意的,文件後綴 tf 文件名不須要進行特殊說明
// 如下爲我使用aliyun 提供的provider 進行的使用
說明:
app.tf // 資源建立的定義
va.tf  // 變量的定義

app.tf

# Configure the Alicloud Provider
provider "alicloud" {
  access_key = "${var.access_key}"
  secret_key = "${var.secret_key}"
  region     = "${var.region}"
}

# Create a web server
resource "alicloud_instance" "web" {
  # cn-beijing
  provider          = "alicloud"
  availability_zone = "cn-beijing-b"
  image_id          = "ubuntu_140405_32_40G_cloudinit_20161115.vhd"

  #  instance_network_type = "Classic"
  internet_charge_type  = "PayByBandwidth"

  instance_type        = "ecs.n1.medium"
  io_optimized         = "optimized"
  system_disk_category = "cloud_efficiency"
  security_groups      = ["${alicloud_security_group.default.id}"]
  instance_name        = "web"
}

# Create security group
resource "alicloud_security_group" "default" {
  name        = "default"
  provider    = "alicloud"
  description = "default"
}

上面的代碼比較簡單,就是聲明瞭,須要建立的esc 實例的信息 包括區域、網絡類型、鏡像id、默認的安全策略

va.tf
variable "access_key" { default = "xxxxxxxxxxxxxxxxxxxxxxx"}
variable "secret_key" { default  = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"}
variable "region" {
  default = "cn-beijing"
}

變量的定義,主要是key、區域
3. 使用
// 初始化 init
terraform init 
// 查看信息 plan
terraform plan 
// 進行資源建立
terraform apply 
// 釋放資源
terraform  destroy

每一個步驟的信息相似以下:
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 = "~> 0.1"

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.


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_instance.web
      id:                   <computed>
      allocate_public_ip:   "false"
      availability_zone:    "cn-beijing-b"
      host_name:            <computed>
      image_id:             "ubuntu_140405_32_40G_cloudinit_20161115.vhd"
      instance_name:        "web"
      instance_type:        "ecs.n1.medium"
      internet_charge_type: "PayByBandwidth"
      io_optimized:         "optimized"
      private_ip:           <computed>
      public_ip:            <computed>
      security_groups.#:    <computed>
      status:               <computed>
      subnet_id:            <computed>
      system_disk_category: "cloud_efficiency"
      system_disk_size:     <computed>

  + alicloud_security_group.default
      id:                   <computed>
      description:          "default"
      name:                 "default"


Plan: 2 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.



alicloud_security_group.default: Creating...
  description: "" => "default"
  name:        "" => "default"
alicloud_security_group.default: Creation complete after 1s (ID: sg-2ze0dkuqzl6tfr1pfuul)
alicloud_instance.web: Creating...
  allocate_public_ip:         "" => "false"
  availability_zone:          "" => "cn-beijing-b"
  host_name:                  "" => "<computed>"
  image_id:                   "" => "ubuntu_140405_32_40G_cloudinit_20161115.vhd"
  instance_name:              "" => "web"
  instance_type:              "" => "ecs.n1.medium"
  internet_charge_type:       "" => "PayByBandwidth"
  io_optimized:               "" => "optimized"
  private_ip:                 "" => "<computed>"
  public_ip:                  "" => "<computed>"
  security_groups.#:          "" => "1"
  security_groups.3325292532: "" => "sg-2ze0dkuqzl6tfr1pfuul"
  status:                     "" => "<computed>"
  subnet_id:                  "" => "<computed>"
  system_disk_category:       "" => "cloud_efficiency"
  system_disk_size:           "" => "<computed>"
alicloud_instance.web: Still creating... (10s elapsed)
alicloud_instance.web: Still creating... (20s elapsed)
alicloud_instance.web: Still creating... (30s elapsed)
alicloud_instance.web: Still creating... (40s elapsed)
alicloud_instance.web: Still creating... (50s elapsed)
alicloud_instance.web: Still creating... (1m0s elapsed)
alicloud_instance.web: Creation complete after 1m2s (ID: i-2ze64rcu238frkzql1sf)

Apply complete! Resources: 2 added, 0 changed, 0 destroyed.

alicloud_security_group.default: Refreshing state... (ID: sg-2ze0dkuqzl6tfr1pfuul)
alicloud_instance.web: Refreshing state... (ID: i-2ze64rcu238frkzql1sf)

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  - destroy

Terraform will perform the following actions:

  - alicloud_instance.web

  - alicloud_security_group.default


Plan: 0 to add, 0 to change, 2 to destroy.

Do you really want to destroy?
  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_instance.web: Destroying... (ID: i-2ze64rcu238frkzql1sf)
alicloud_instance.web: Still destroying... (ID: i-2ze64rcu238frkzql1sf, 10s elapsed)
alicloud_instance.web: Destruction complete after 11s
alicloud_security_group.default: Destroying... (ID: sg-2ze0dkuqzl6tfr1pfuul)
alicloud_security_group.default: Destruction complete after 1s

Destroy complete! Resources: 2 destroyed.
4. 阿里雲管理界面的顯示效果
 
 
5. 總結
總的來講仍是比較簡單的,使用起來也比較方便,對於多雲管理方便了好多,同時結合每一個雲廠商的實際
最佳實踐,能夠子啊咱們的實際產品開發商,下降對於雲廠商的較大依賴,同時能夠方便的進行管理
目前發現的一些問題:
文檔上有些還不是很全、部分文檔更新不及時。
同時在積極適應國際化市場,看齊國際廠商上,給予阿里一個大大的贊,但願國內的一些搞雲的公司和學習下。
6. 參考資料
https://www.terraform.io/intro/getting-started/build.html
https://www.terraform.io/docs/providers/alicloud/r/instance.html#internet_charge_type
http://www.infoq.com/cn/news/2015/05/hashimoto-modern-datacenter
http://www.infoq.com/cn/news/2017/10/terraform-multicloud-advances
相關文章
相關標籤/搜索