How to Import Multiple Terraform Resource Instances?
I Am Trying to Import Already Existing Resources, These Cannot Be Recreated, and There Are Many. There Is Configuration That Is Basic to All Resources and for...
I am trying to import already existing resources, these cannot be recreated, and there are many.
There is configuration that is basic to all resources and for some there are small changes.
I would like to import all the resources with a single command, doing it one by one is tedious and prone to mistakes. Currently importing single resources with:
terraform import 'github_repository.repo_config["repo2"]' repo2
What would the import command look like if it were to import all of the resources?
The configuration is as follows:
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 = ""
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"
}
}
}
3 Answers
You can try to create a shell script to automate the importing process. The idea is to declare an array of the ID of the resources that are going to be imported, then loop through the array and input the variables to the command.
Here is the example that I used to import multiple IAM roles to my tfstate.
# Declare a list of roles that will be imported
roles=("role-1" "role-2" "role-3")
# Loop through the array and import the roles to tfstate
for role in "${roles[@]}"; do
terraform import -var="customer_region=${customer_region}" "aws_iam_role.roles[""\"${role}\"""]" "$role"
done
Hopefully this idea can help to automate the importing process. Cheers.
In Terraform v1.5.0 and later, you can use import block to import many resources at once:
Importing multiple resources with single terraform import .. call is not supported natively as of now by terraform.
However terraformer could be something which can reduce your effort. Please go through the Terraformer Github documentation to verify if this works for you.
Note: Supports only organizational resources.