【问题标题】:terraform import throws "Index value required" error for a string key valueterraform import 为字符串键值引发“需要索引值”错误
【发布时间】:2020-01-28 23:15:08
【问题描述】:

如果它是由 terraform 从头开始​​创建的,这就是它在状态文件中的样子:

{
  "module": "module.rds",
  "mode": "managed",
  "type": "aws_cloudwatch_log_subscription_filter",
  "name": "rds_logs_delivery",
  "each": "map",
  "provider": "provider.aws",
  "instances": [
    {
      "index_key": "prod-service-master.audit",
      "schema_version": 0,
      "attributes": {
        "destination_arn": "arn:aws:firehose:us-east-5:9999999999:deliverystream/prod-rds-logs",
        "distribution": "ByLogStream",
        "filter_pattern": "",
        "id": "cwlsf-9999999999",
        "log_group_name": "/aws/rds/instance/prod-service-master/audit",
        "name": "rds-logs-delivery-prod-service-master",
        "role_arn": "arn:aws:iam::9999999999:role/cloudwatch-logs-to-kinesis"
      },
      "private": "abcdefg",
      "depends_on": [
        "data.aws_caller_identity.current",
        "data.aws_partition.current",
        "data.terraform_remote_state.kinesis_delivery_stream"
      ]
    },

由于某些原因,我需要将其导入另一个 tf 状态文件。所以我运行了这个命令:

 terraform import module.rds.aws_cloudwatch_log_subscription_filter.rds_logs_delivery["prod-service-reports.audit"] rds-logs-delivery-prod-service-master

我收到了这个错误:

Error: Index value required

  on <import-address> line 1:
   1: module.rds.aws_cloudwatch_log_subscription_filter.rds_logs_delivery[prod-service-reports.audit]

Index brackets must contain either a literal number or a literal string.

我该如何解决?我试过了

  1. 将双引号替换为单引号,并且
  2. 没有报价

但我仍然遇到同样的错误。

【问题讨论】:

  • 您使用的索引确实看起来像一个变量而不是文字。资源是什么样的,所以我们知道它是如何被迭代的?
  • 你解决过这个问题吗?我有同样的问题,试图导入一个带有 for_each 的资源,所以我必须引用实例,但导入会不断剥离 " ,即使使用转义字符

标签: terraform


【解决方案1】:

正如@JamesK 在上面这个答案中所建议的那样,只有在您使用 Linux 或更具体地在 Windows 下使用 bash 或 Git Bash 时才是解决方案。

PowerShell 的解决方案如下:

terraform import resource[\`"prod-service-reports.audit\`"] ...

感谢here的回答:)

【讨论】:

    【解决方案2】:

    您需要转义引号。如,

    terraform import   module.rds.aws_cloudwatch_log_subscription_filter.rds_logs_delivery[\"prod-service-reports.audit\"] rds-logs-delivery-prod-service-master
    

    【讨论】:

      【解决方案3】:

      我发现你需要做这样的事情

      terraform state rm 'resource[\"thing\"]'
      

      【讨论】:

      • 您在重复以前的答案已经提供的内容。
      • @Lethargos 但​​由于缺少单引号而无法正常工作
      • @Lethargos 再次点击了这个,其他答案都不起作用,只有这个起作用,这是截然不同的
      • 谢谢你,克里斯。我需要这种语法,因为其他语法不起作用。
      猜你喜欢
      • 2021-07-04
      • 2021-06-23
      • 1970-01-01
      • 2021-11-01
      • 2011-03-03
      • 2021-02-10
      • 1970-01-01
      • 1970-01-01
      • 2021-11-27
      相关资源
      最近更新 更多