Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

terraform import throws "Index value required" error for a string key value

Tags:

terraform

This is what it would look like in the state file if it is created from the scratch by 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"
      ]
    },

For some reasons I need to import this into another tf state file. So I ran this command:

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

I got this error:

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.

How can I fix it? I have tried

  1. replace double quote with single quote, and
  2. no quote

but I still get the same error.

like image 221
Anthony Kong Avatar asked Jan 28 '20 23:01

Anthony Kong


2 Answers

You need to escape the quotes. As in,

terraform import   module.rds.aws_cloudwatch_log_subscription_filter.rds_logs_delivery[\"prod-service-reports.audit\"] rds-logs-delivery-prod-service-master
like image 181
JamesK Avatar answered Oct 14 '22 17:10

JamesK


As suggested by @JamesK in this answer above is only the solution if you are using Linux or more specifically bash or Git Bash under Windows.

The solution for PowerShell is the following:

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

Thanks to the answer here :)

like image 25
sikkiv Avatar answered Oct 14 '22 17:10

sikkiv