简体   繁体   English

将 terraform 资源导入本地块

[英]Importing terraform resources into a local block

I have a project that has many SQS queues in AWS that we need to manage.我有一个项目,在 AWS 中有许多我们需要管理的 SQS 队列。 I need to import those queues into my terraform code, but since they're already being used, I can't destroy and recreate them.我需要将这些队列导入到我的 terraform 代码中,但由于它们已经被使用,我无法销毁和重新创建它们。

Since we have many queues, we use a locals block instead of its resource block to define some of its arguments, such as name , delay_seconds and others.由于我们有很多队列,我们​​使用locals块而不是它的resource块来定义它的一些参数,例如namedelay_seconds等。 (this is because we don't want to add over 10 resource blocks to import the queues into them and have 100+ lines of code) (这是因为我们不想添加超过 10 个resource块来将队列导入其中并有 100 多行代码)

Below, example code that we use to create them:下面是我们用来创建它们的示例代码:

provider "aws" {
  region = "us-east-2"
}

locals {
  sqs_queues = {
    test-01 = {
      name          = "test-import-terraform-01"
      delay_seconds = 30
    }
    test-02 = {
      name          = "test-import-terraform-02"
      delay_seconds = 30
    }
  }
}

resource "aws_sqs_queue" "queue" {
  for_each      = local.sqs_queues
  name          = each.value.name
  delay_seconds = each.value.delay_seconds
}

This in turn will create the following queues: test-import-terraform-01 and test-import-terraform-02 as usual.这反过来将创建以下队列:像往常一样的test-import-terraform-01test-import-terraform-02 Querying my statefile, i can see then defined as such:查询我的状态文件,我可以看到然后定义如下:

aws_sqs_queue.queue["test-01"]
aws_sqs_queue.queue["test-02"]

Based on that, i would like to import two existing queues to my code: test-import-terraform-03 and test-import-terraform-04 .基于此,我想将两个现有队列导入我的代码: test-import-terraform-03test-import-terraform-04 I thought about adding these two maps to my locals block:我考虑将这两个地图添加到我的locals块中:

test-03 = {
  name          = "test-import-terraform-03"
  delay_seconds = 30
}
test-04 = {
  name          = "test-import-terraform-04"
  delay_seconds = 30
}

But when I try to import them, I get the following error for either queues:但是,当我尝试导入它们时,任一队列都会出现以下错误:

$ terraform import aws_sqs_queue.queue["test-03"] https://sqs.us-east-2.amazonaws.com/12345678910/test-import-terraform-03
zsh: no matches found: aws_sqs_queue.queue[test-03]

Is doing something like that possible?做这样的事情可能吗?

Your problem is not to do with Terraform, but with shell expansion (note the error message comes from zsh ).您的问题与 Terraform 无关,而与 shell 扩展有关(注意错误消息来自zsh )。

Try quoting your shell arguments properly:尝试正确引用您的 shell 参数:

terraform import 'aws_sqs_queue.queue["test-03"]' 'https://sqs.us-east-2.amazonaws.com/12345678910/test-import-terraform-03'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM