Terraform: Terraform fails to import AutoScaling Group resource the name contains spaces

Created on 29 Jan 2018  ·  9Comments  ·  Source: hashicorp/terraform

Terraform Version

Terraform v0.10.8

Terraform Configuration Files

resource "aws_autoscaling_group" "private_pushpublisher_asg_1a" {
  availability_zones = ["${element(split(",", module.network.availability_zone), 0)}"]
  name = "PushPublisher ASG1a"
  max_size = 100
  min_size = 0
  desired_capacity = 2
  launch_configuration = "${aws_launch_configuration.push_publisher_launch_configuration.name}"
  target_group_arns = ["${aws_lb_target_group.private_pushpublisher_target_group_1a.arn}"]

  lifecycle {
    create_before_destroy = true # Prevents Terraform failing when LC is updated and attached to ASG
    ignore_changes = ["desired_capacity"]
  }

  tags {
    key = "Name"
    value = "push-publisher"
    propagate_at_launch = true
  }
  tags {
    key = "terraform"
    value = "true"
    propagate_at_launch = true
  }
}

Expected Behavior

Terraform imports existing resource into Terraform configuration

Actual Behavior

Throws an error:

The import command expects two arguments.
Usage: terraform import [options] ADDR ID

Steps to Reproduce

  1. terraform init
  2. terraform import "module.infrastucture.aws_autoscaling_group.private_pushpublisher_asg_1a" "PushPublisher ASG1a"
  3. terraform import module.infrastucture.aws_autoscaling_group.private_pushpublisher_asg_1a PushPublisher ASG1a

Additional Context

Escaping the space with "\" results in the same problem

bug cli

All 9 comments

Steps to reproduce disappeared:
terraform import "module.infrastucture.aws_autoscaling_group.private_pushpublisher_asg_1a" "PushPublisher ASG1a"
and
terraform import module.infrastucture.aws_autoscaling_group.private_pushpublisher_asg_1a PushPublisher ASG1a

Both fail

Does this work for you? terraform import module.infrastucture.aws_autoscaling_group.private_pushpublisher_asg_1a PushPublisher\ ASG1a

No, escaping the space results in the same issue.

For anyone that comes across this problem, I found a temporary solution by creating a variable containing the autoscaling group name, i.e.

asg="PushPublisher ASG1a"
terraform import module.infrastucture.aws_autoscaling_group.private_pushpublisher_asg_1a $asg

Hi @completenovice! I'm sorry this isn't working as expected.

It seems like this is a shell-related escaping issue, but it's weird that the escaping didn't work.

Could you tell us what OS you are using and what shell you are using on that OS? Unfortunately escaping and parsing behavior can vary between shells.

@apparentlymart - Sure, that would make sense. I'm running OSX 10.12.6 with ZSH 5.2.

I'm wondering if ignoring quotes is also related to the shell, or whether that is a Terraform issue? i.e. "PushPublisher ASG1a" not being passed as a single argument

:thinking:

I must admit that I don't have much experience with zsh, so I'm not totally sure what's going on here. However, you mentioned that this worked when you used a variable:

asg="PushPublisher ASG1a"
terraform import module.infrastucture.aws_autoscaling_group.private_pushpublisher_asg_1a $asg

In bash I believe that would still get handled as two separate arguments, since tokenization happens _after_ variables are inserted, and so for bash I would write the above as:

asg="PushPublisher ASG1a"
terraform import module.infrastucture.aws_autoscaling_group.private_pushpublisher_asg_1a "$asg"

The quotes around $asg then ensure that the shell tokenizer treats the result of that expansion as a single argument.

The fact that you were able to get Terraform to recognize a space within an argument _somehow_ suggests that this isn't a Terraform bug, since if e.g. Terraform were applying additional splitting to the argument separate from the shell's expansion then there should be _no_ form of this that would actually work.

To try to understand better what's going on here I made a small Go program that just prints out the arguments array as-is. Here's my different experiments under bash first:

$ ./show-args import module.infrastructure.autoscaling_group.private_pushpublisher_asg_1a PushPublisher ASG1a
([]string) (len=5 cap=5) {
 (string) (len=11) "./show-args",
 (string) (len=6) "import",
 (string) (len=68) "module.infrastructure.autoscaling_group.private_pushpublisher_asg_1a",
 (string) (len=13) "PushPublisher",
 (string) (len=5) "ASG1a"
}
$ ./show-args import module.infrastructure.autoscaling_group.private_pushpublisher_asg_1a "PushPublisher ASG1a"
([]string) (len=4 cap=4) {
 (string) (len=11) "./show-args",
 (string) (len=6) "import",
 (string) (len=68) "module.infrastructure.autoscaling_group.private_pushpublisher_asg_1a",
 (string) (len=19) "PushPublisher ASG1a"
}
$ ./show-args import module.infrastructure.autoscaling_group.private_pushpublisher_asg_1a PushPublisher\ ASG1a
([]string) (len=4 cap=4) {
 (string) (len=11) "./show-args",
 (string) (len=6) "import",
 (string) (len=68) "module.infrastructure.autoscaling_group.private_pushpublisher_asg_1a",
 (string) (len=19) "PushPublisher ASG1a"
}
$ export asg="PushPublisher ASG1a"
$ asg="PushPublisher ASG1a" ./show-args import module.infrastructure.autoscaling_group.private_pushpublisher_asg_1a $asg
([]string) (len=5 cap=5) {
 (string) (len=11) "./show-args",
 (string) (len=6) "import",
 (string) (len=68) "module.infrastructure.autoscaling_group.private_pushpublisher_asg_1a",
 (string) (len=13) "PushPublisher",
 (string) (len=5) "ASG1a"
}
$ asg="PushPublisher ASG1a" ./show-args import module.infrastructure.autoscaling_group.private_pushpublisher_asg_1a "$asg"
([]string) (len=4 cap=4) {
 (string) (len=11) "./show-args",
 (string) (len=6) "import",
 (string) (len=68) "module.infrastructure.autoscaling_group.private_pushpublisher_asg_1a",
 (string) (len=19) "PushPublisher ASG1a"
}

Here's the same experiment under zsh on my Linux machine:

$ ./show-args import module.infrastructure.autoscaling_group.private_pushpublisher_asg_1a PushPublisher ASG1a
([]string) (len=5 cap=5) {
 (string) (len=11) "./show-args",
 (string) (len=6) "import",
 (string) (len=68) "module.infrastructure.autoscaling_group.private_pushpublisher_asg_1a",
 (string) (len=13) "PushPublisher",
 (string) (len=5) "ASG1a"
}
$ ./show-args import module.infrastructure.autoscaling_group.private_pushpublisher_asg_1a "PushPublisher ASG1a"
([]string) (len=4 cap=4) {
 (string) (len=11) "./show-args",
 (string) (len=6) "import",
 (string) (len=68) "module.infrastructure.autoscaling_group.private_pushpublisher_asg_1a",
 (string) (len=19) "PushPublisher ASG1a"
}
$ ./show-args import module.infrastructure.autoscaling_group.private_pushpublisher_asg_1a PushPublisher\ ASG1a
([]string) (len=4 cap=4) {
 (string) (len=11) "./show-args",
 (string) (len=6) "import",
 (string) (len=68) "module.infrastructure.autoscaling_group.private_pushpublisher_asg_1a",
 (string) (len=19) "PushPublisher ASG1a"
}
$ ./show-args import module.infrastructure.autoscaling_group.private_pushpublisher_asg_1a $asg
([]string) (len=4 cap=4) {
 (string) (len=11) "./show-args",
 (string) (len=6) "import",
 (string) (len=68) "module.infrastructure.autoscaling_group.private_pushpublisher_asg_1a",
 (string) (len=19) "PushPublisher ASG1a"
}
$ ./show-args import module.infrastructure.autoscaling_group.private_pushpublisher_asg_1a "$asg"
([]string) (len=4 cap=4) {
 (string) (len=11) "./show-args",
 (string) (len=6) "import",
 (string) (len=68) "module.infrastructure.autoscaling_group.private_pushpublisher_asg_1a",
 (string) (len=19) "PushPublisher ASG1a"
}

This does at least seem to confirm the result that zsh treats a variable interpolation as a single argument even if the value contains a space, unlike bash. However, all of the other cases seem to behave the same as bash, so it's confusing as to why backslash escaping and quoting would not have worked for you with terraform import. The error message you referred to is looking at the same arguments array as my test program here (albeit with some preprocessing already done for flag parsing).

I also tried some of these examples under zsh while actually running terraform import. I don't have a suitable resource to import, but the error message I got in the following examples suggest that Terraform was able to progress past the test you saw as failing because the check for _this_ error message is later:

$ terraform import autoscaling_group.private_pushpublisher_asg_1a PushPublisher ASG1a  
The import command expects two arguments.
Usage: terraform import [options] ADDR ID

  Import existing infrastructure into your Terraform state.

  This will find and import the specified resource into your Terraform
  state, allowing existing infrastructure to come under Terraform
  management without having to be initially created by Terraform.
# ... etc ...

$ terraform import autoscaling_group.private_pushpublisher_asg_1a PushPublisher\ ASG1a 
Error: resource address "autoscaling_group.private_pushpublisher_asg_1a" does not exist in the configuration.

Before importing this resource, please create its configuration in the root module. For example:

resource "autoscaling_group" "private_pushpublisher_asg_1a" {
  # (resource arguments)
}

$ terraform import autoscaling_group.private_pushpublisher_asg_1a "PushPublisher ASG1a"                     
Error: resource address "autoscaling_group.private_pushpublisher_asg_1a" does not exist in the configuration.

Before importing this resource, please create its configuration in the root module. For example:

resource "autoscaling_group" "private_pushpublisher_asg_1a" {
  # (resource arguments)
}

$ terraform import autoscaling_group.private_pushpublisher_asg_1a $asg                 
Error: resource address "autoscaling_group.private_pushpublisher_asg_1a" does not exist in the configuration.

Before importing this resource, please create its configuration in the root module. For example:

resource "autoscaling_group" "private_pushpublisher_asg_1a" {
  # (resource arguments)
}

So with all of this said, I'm not sure what to suggest here. :confounded:

Good investigation and an interesting read, thank you!

Looking over your results it does look to be some kind of weird shell issue, but why escaping spaces in ZSH worked for you and not me I'm not sure. I'm not going to fret over it. There are relatively simple workarounds available and hopefully others experiencing the same problem will find this thread. 😄

I'm going to lock this issue because it has been closed for _30 days_ ⏳. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

Was this page helpful?
0 / 5 - 0 ratings