如何在Terraform 0.13中将IBM Cloud Provider插件用于Terraform?

问题描述

terraform 0.13刚刚发布(https://www.hashicorp.com/blog/announcing-hashicorp-terraform-0-13/),它改变了与第三方提供商(https://www.terraform.io/upgrade-guides/0-13.html#explicit-provider-source-locations)的合作方式。

运行terraform init时遇到错误

$ terraform init

Initializing the backend...

Initializing provider plugins...
- Finding latest version of hashicorp/ibm...

Error: Failed to install provider

Error while installing hashicorp/ibm: provider registry registry.terraform.io
does not have a provider named registry.terraform.io/hashicorp/ibm

以前,此功能可用于terraform 0.12.29和IBM提供程序1.10.0。

解决方法

以下是Linux的说明以及Terraform和IBM提供程序的当前版本:

安装Terraform

  1. 下载Terraform 0.13
    wget https://releases.hashicorp.com/terraform/0.13.0/terraform_0.13.0_linux_amd64.zip
    
  2. 解压缩提供者
    unzip terraform_0.13.0_linux_amd64.zip
    
  3. 将其移动到路径中的文件夹,例如:
    mv terraform /usr/local/bin/
    
  4. 确保版本为0.13
    terraform version
    

安装IBM提供程序

  1. 创建用于放置插件的文件夹:
    mkdir -p ~/.terraform.d/plugins/localdomain/provider/ibm/1.10.0/linux_amd64
    
  2. 获取提供者:
    wget https://github.com/IBM-Cloud/terraform-provider-ibm/releases/download/v1.10.0/terraform-provider-ibm_1.10.0_linux_amd64.zip
    
  3. 解压缩提供者:
    unzip terraform-provider-ibm_1.10.0_linux_amd64.zip
    
  4. 将提供程序移动到先前创建的文件夹:
    mv terraform-provider-ibm_v1.10.0 ~/.terraform.d/plugins/localdomain/provider/ibm/1.10.0/linux_amd64
    

使用简单的Terraform文件进行测试

  1. 创建main.tf

    terraform {
      required_providers {
        ibm = {
          source  = "localdomain/provider/ibm"
          version = "1.10.0"
        }
      }
    }
    
    variable ibmcloud_api_key {
    }
    
    provider "ibm" {
      ibmcloud_api_key = var.ibmcloud_api_key
    }
    
    resource ibm_resource_group new_group {
      name = "created-by-terraform"
    }
    
  2. 创建terraform.tfvars并填写您的IBM Cloud API密钥:

    ibmcloud_api_key="REPLACE_WITH_YOUR_KEY"
    
  3. 初始化地形

    terraform init
    

    将导致:

    Initializing the backend...
    
    Initializing provider plugins...
    - Finding localdomain/provider/ibm versions matching "1.10.0"...
    - Installing localdomain/provider/ibm v1.10.0...
    - Installed localdomain/provider/ibm v1.10.0 (unauthenticated)
    
    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.
    
  4. 并申请

    terraform apply
    

    将导致:

    ...
      Enter a value: yes
    
    ibm_resource_group.new_group: Creating...
    ibm_resource_group.new_group: Creation complete after 2s [id=2142c8122344458d59b8729708464a]
    
    Apply complete! Resources: 1 added,0 changed,0 destroyed.
    

快乐的地形改造!

,

IBM Provider现在已发布到资源库,因此您可以使用新的terraform 13 Provider语法,例如:

terraform {
  required_version = ">= 0.13"
  required_providers {
    ibm = {
      source  = "ibm-cloud/ibm"
      version = "1.11.2"
    }
  }
}
,
terraform {
  required_version = ">= 0.13.3"
  required_providers {
    ibm = {
      source  = "ibm-cloud/ibm"
      version = "1.12.0"
    }
  }
}

这将为您提供最新版本。