I need to import AWS VPC subnets into terraform using import command. When I run terraform plan
command I get below output
module.test-vpc.aws_subnet.play["10.76.175.0/24"]
how do I import this resource as it contains this ["10.76.175.0/24"] cidr block. Below are the command I tried which is failing with this error Error: Invalid number literal
terraform import module.test-vpc.aws_subnet.play[10.76.175.0/24] sub-xyz
I tired below commands that got successful import but unable to recognise resources when I run terraform plan
again.
terraform import module.test-vpc.aws_subnet.play sub-xyz
terraform import module.test-vpc.aws_subnet.play[0] sub-xyz
CodePudding user response:
The module probably use a for_each condition, so the right command should be
terraform import module.test-vpc.aws_subnet.play["10.76.175.0/24"] sub-xyz
or
terraform import 'module.test-vpc.aws_subnet.play["10.76.175.0/24"]' sub-xyz
with quotes. Because you reference a resource by the key.
It's also possible to reference the resources by a number that represent the order in the map but is not recommended because it's hard to understand if you are doing the right import.
So, doing the commands
terraform import module.test-vpc.aws_subnet.play sub-xyz
terraform import module.test-vpc.aws_subnet.play[0] sub-xyz
you already imported the resources so you don't see that in plan anymore. You can remove the resource from the state by
terraform state rm module.test-vpc.aws_subnet.play[0]
and re-import the resource