【问题标题】:How to import multiple terraform resource instances?如何导入多个 Terraform 资源实例?
【发布时间】:2023-01-07 21:58:53
【问题描述】:

我正在尝试导入已经存在的资源,这些资源无法重新创建,而且有很多。

有所有资源的基本配置,有些资源有小的变化。

我想用一个命令导入所有资源,一个一个地做很繁琐而且容易出错。 目前正在导入单个资源:

terraform import 'github_repository.repo_config["repo2"]' repo2

如果要导入所有资源,import 命令会是什么样子?

配置如下:

terraform {
  required_providers {
    github = {
      source  = "integrations/github"
      version = "~> 5.0"
    }
  }
}

provider "github" {
  owner = "medecau"
}

variable "repo_config" {
  type = map(object({
    description  = string
    homepage_url = string
    topics       = list(string)
  }))
  default = {
    "repo1" = {
      description  = "Repo 1"
      homepage_url = "https://medecau.github.io/repo1/"
      topics       = ["topic1", "topic2", "topic3"]
    }
    "repo2" = {
      description  = "Repo 2"
      homepage_url = null
      topics       = null
    }
  }
}
variable "default_repo_config" {
  type = object({
    description  = string
    homepage_url = string
    topics       = list(string)
  })
  default = {
    description  = ""
    homepage_url = ""
    topics       = []
  }
}

data "github_repositories" "medecau_repos" {
  query           = "user:medecau"
  include_repo_id = true
}

resource "github_repository" "repo_config" {
  # cast to set to remove duplicates
  for_each = toset(data.github_repositories.medecau_repos.names)
  name     = each.value

  description  = lookup(var.repo_config, each.value, var.default_repo_config).description
  homepage_url = lookup(var.repo_config, each.value, var.default_repo_config).homepage_url
  topics       = lookup(var.repo_config, each.value, var.default_repo_config).topics

  has_issues           = true
  has_projects         = false
  has_wiki             = false
  vulnerability_alerts = true
  security_and_analysis {
    advanced_security {
      status = "enabled"
    }
    secret_scanning {
      status = "enabled"
    }
    secret_scanning_push_protection {
      status = "enabled"
    }
  }
}

【问题讨论】:

  • 这仍然是不可能的,我相信路线图上没有任何内容。

标签: github terraform terraform-provider-github


【解决方案1】:

截至目前,terraform 本身不支持使用单个 terraform import .. 调用导入多个资源。

但是 terraformer 可能会减少您的工作量。请通过Terraformer Github documentation 验证这是否适合您。

笔记:仅支持组织资源。

【讨论】:

    猜你喜欢
    • 2021-07-29
    • 2020-06-03
    • 2020-01-23
    • 2019-03-16
    • 2020-11-18
    • 2020-04-29
    • 2020-02-27
    • 1970-01-01
    • 2021-12-27
    相关资源
    最近更新 更多