Terrafom import on windows

605 views Asked by At

i was following this tutorial https://learn.hashicorp.com/tutorials/terraform/state-cli?in=terraform/cli and i came across a problem. I'm using cmd.exe

i copied&pasted command terraform import aws_security_group.sg_8080 $(terraform output -raw security_group) from article Remove resource from the state, and i got error visible below, What should i do?

    The import command expects two arguments.
Usage: terraform [global options] 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.

  The ADDR specified is the address to import the resource to. Please
  see the documentation online for resource addresses. The ID is a
  resource-specific ID to identify that resource being imported. Please
  reference the documentation for the resource type you're importing to
  determine the ID syntax to use. It typically matches directly to the ID
1

There are 1 answers

0
Martin Atkins On

The tutorial you referred to is giving an example command line which relies on some expansion capabilities of typical Unix shells.

One way to achieve a similar result in the Windows Command Prompt (cmd.exe) is to manually run the nested command and then copy-paste its result into the argument of the second command.

For example, first run the command inside the $( ... ) sequence:

C:\example> terraform output -raw security_group
sg-0096a764b1e76f7fd

Here I've assumed that the output would be the same as the ID shown in the example in the tutorial, but of course in your case you will have a different ID with an sg- prefix reflecting the actual security object in your AWS account.

You can then place that security group ID into the outer command line instead of the $( ... ) sequence:

C:\example> terraform import aws_security_group.sg_8080 sg-0096a764b1e76f7fd

This two-step process should reproduce the same effect that a Unix-style shell would've achieved with the command line shown in the tutorial.


I'm not familiar enough with cmd.exe to suggest a direct single-step command similar to the one in the tutorial, and I don't have a Windows system to test on, but there is a question on the SuperUser StackExchange which is asking the same thing you asked in more general terms, agnostic of Terraform; perhaps the answers there will be helpful.