文档首页> Terraform> 快速入门
更新时间:2021-03-18 GMT+08:00
分享

快速入门

本文介绍Terraform的安装配置,并以创建一个华为云VPC为例介绍如何使用Terraform。

安装Terraform

Terraform是以二进制可执行文件发布,您只需下载terraform,然后将terraform可执行文件所在目录添加到系统环境变量PATH中即可。

  1. 登录Terraform官网,下载对应操作系统的安装包。
  2. 解压安装包,并将terraform可执行文件所在目录添加到系统环境变量PATH中。
  3. 在命令行中执行如下命令验证配置路径是否正确。

    terraform

    如果回显如下则说明配置正确,terraform可以运行。

    Usage: terraform [-version] [-help] <command> [args]
    
    ....

认证与鉴权

Terraform支持编排华为云上的各种云资源,使用Terraform管理华为云资源前,您需要获取AK/SK,并在Terraform上进行配置,从而认证鉴权。

您可以使用如下两种方式配置Terraform。

  • 静态凭证(Static credentials)

    静态凭证即在Terraform配置文件中添加AK/SK信息,如下所示。

    provider "huaweicloud" {
      region     = "cn-north-1"
      access_key = "my-access-key"
      secret_key = "my-secret-key"
    }
    • region:区域,即需要创建管理哪个区域的资源。您可以在这里查询华为云支持的区域。
    • access_key:密钥ID,即AK。查询方法请参见访问密钥
    • secret_key:访问密钥,即SK。查询方法请参见访问密钥
  • 环境变量(Environment variables)

    您可以将如下信息添加到环境变量中进行认证鉴权。

    $ export HW_REGION_NAME="cn-north-1"
    $ export HW_ACCESS_KEY="my-access-key"
    $ export HW_SECRET_KEY="my-secret-key"
    • HW_REGION_NAME:区域,即需要创建管理哪个区域的资源。您可以在这里查询华为云支持的区域。
    • HW_ACCESS_KEY:密钥ID,即AK。查询方法请参见访问密钥
    • HW_SECRET_KEY:访问密钥,即SK。查询方法请参见访问密钥

更多配置参数请参见 https://registry.terraform.io/providers/huaweicloud/huaweicloud/latest/docs

使用Terraform创建华为云VPC

下面以创建一个华为云VPC为例介绍如何使用Terraform。示例中使用的Terraform版本为0.13,华为云Provider的版本为1.20.0。

  1. 在工作目录下创建 "versions.tf" 文件,指定华为云Provider的registry源和版本,文件内容如下:

    terraform {
      required_providers {
        huaweicloud = {
          source = "huaweicloud/huaweicloud"
          version = ">= 1.20.0"
        }
      }
    }

    如果需要使用本地的registry源,请参考如何加速下载Provider?

  2. 创建“main.tf”文件,配置华为云Provider并创建一个VPC,文件内容如下:

    # Configure the HuaweiCloud Provider
    provider "huaweicloud" {
      region     = "cn-north-1"
      access_key = "my-access-key"
      secret_key = "my-secret-key"
    }
    
    # Create a VPC
    resource "huaweicloud_vpc" "example" {
      name = "terraform_vpc"
      cidr = "192.168.0.0/16"
    }

    上半部分为HuaweiCloud Provider的配置,包含认证鉴权的内容,请根据认证与鉴权配置相关参数;如果使用环境变量方式认证鉴权,可以省略该部分内容。

    下半部分描述一个名为example的VPC资源,其中VPC名称为terraform_vpc,cidr为192.168.0.0/16。

  3. 执行如下命令初始化。

    terraform init

    回显如下,首次执行时会下载HuaweiCloud Provider并安装。

    $ terraform init
    
    Initializing the backend...
    
    Initializing provider plugins...
    - Finding latest version of huaweicloud/huaweicloud
    - Installing huaweicloud/huaweicloud v1.20.0...
    ...
    Terraform has been successfully initialized!

  4. 执行如下命令查看要创建的资源。

    terraform plan

    回显如下,Terraform会显示要创建哪些资源。

    ...
    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:
    
      # huaweicloud_vpc.example will be created
      + resource "huaweicloud_vpc" "example" {
          + cidr   = "192.168.0.0/16"
          + id     = (known after apply)
          + name   = "terraform_vpc"
          + region = (known after apply)
          + routes = (known after apply)
          + shared = (known after apply)
          + status = (known after apply)
        }
    
    Plan: 1 to add, 0 to change, 0 to destroy.
    ...

  5. 执行如下命令创建资源。

    terraform apply

    根据提示输入“yes”,回显如下,可以看到名为terraform_vpc的VPC已经创建,您也可以到华为云控制台上查看资源是否已经创建。

    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:
    
      # huaweicloud_vpc.example will be created
      + resource "huaweicloud_vpc" "example" {
          + cidr   = "192.168.0.0/16"
          + id     = (known after apply)
          + name   = "terraform_vpc"
          + region = (known after apply)
          + routes = (known after apply)
          + shared = (known after apply)
          + status = (known after apply)
        }
    
    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
    
    huaweicloud_vpc.example: Creating...
    huaweicloud_vpc.example: Creation complete after 7s [id=ceab8267-38e5-4a4c-8065-12967ad9eb31]
    
    Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

后续操作

您可以浏览用户指南了解如何创建常用的华为云资源。

分享:

    相关文档

    相关产品