Import terraform AWS VPC subnet having CIDR in resource name - amazon-web-services

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

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

Related

Unbale to import subnets using google cloud network module

I am trying to manage google cloud infrastructure using terraform modules. We have existing infrastructure I would like to use modules to import them as well.
I got this VPC
name: test-vpc
project: project-1
subnets: subnet-01,subnet-02
I used this google cloud module to import VPC and it worked fine, it imported only vpc but not subnets.
terraform import module.vpc.module.vpc.google_compute_network.network projects/project-1/global/networks/test-vpc
Next, I tried to import the subnet as well using the below command following google and some other documents. Whatever I do I am unable to import the subnet, I saw the subnets module got for and for_each loops, Could someone advise how can I import subnets using gcloud modules?
terraform import module.vpc.module.subnets.google_compute_subnetwork.subnetwork projects/project-1/regions/europe-west3/subnetworks/subnet-01
Thank you
I tried all these commands:
terraform import module.vpc.module.subnets.google_compute_subnetwork.subnetwork projects/project-1/regions/europe-west3/subnetworks/subnet-01
terraform import module.vpc.module.subnets.google_compute_subnetwork.subnetwork[\"subnet-01\"] projects/project-1/regions/europe-west3/subnetworks/subnet-01
terraform import module.vpc.module.subnets.google_compute_subnetwork.subnetwork[\"europe-west3\/subnet-01\"] projects/project-1/regions/europe-west3/subnetworks/subnet-01
I don't see a subnet in the tfstate.

importing aws_iam_policy multiple times

I have created resource stub for importing iam customer managed policy as below.
resource "aws_iam_policy" "customer_managed_policy" {
name = var.customer_managed_policy_name
policy = "{}"
}
The import command used is:
$ terraform import -var 'customer_managed_policy_name=ec2-readonly' aws_iam_policy.customer_managed_policy arn:aws:iam::<account ID>:policy/ec2-readonly
This works fine for first time. But If I want to make it dynamic in order to import any number of policies, I don't know how to do.
Because "aws_iam_policy" resource will use policy name and policy data/json as attributes, for them by using for_each or list, multiple resources can be created but in import command I need to pass policy arn which is different.
I think there is a misunderstanding on how terraform works.
Terraform maps 1 resource to 1 item in state and the state file is used to manage all created resources.
To import "X" resources, "X" resources must exist in your terraform configuration so "X" can be mapped to state.
2 simple ways to achieve this would be by using "count" or "for_each" to map "X" resources to state. Therefore being able to import "X" resources.
Now, it is important to noticed that after you import a resource, if your terraform configuration it's not equal to the imported resource, once you run terraform apply, terraform will be update all imported resources to match your terraform configuration file.

Terraform AWS Provider - import aws_api_gateway_account

When importing aws_api_gateway_account it is not matching the output of “aws apigateway get-account”
The cloudwatchRoleArn is not imported properly. I am using Terraform 1.2.2 and aws provider 4.18.0.
I am importing according to the documentation Terraform Registry
Ex:
terraform import aws_api_gateway_account.rest_api_account api-gateway-account

Terraform Import aws resources

I previously imported ec2 instance in terraform state now I want to bring ec2 root volume under terraform state as well.
On my testing I was able to import ec2 instance and ebs volume using the following commands
`terraform import aws_ebs_volume.id vol-01234`
`terraform import aws_instance.myec2 i-12345678`
Please help me how I can I import aws_instance.root_block_device ?
Command terraform import aws_instance.my_ec2.root_block_device vol-01234 does seems to work
There are several ways to import. I used a method to find "type of reource to import: aws instance, oogle_dns_record_set " from resource in document, input data as required in import

terraform import fargate cluster

I have an existing manually created fargate cluster named "test-cluster" in us-west-1
In terraform configuration file i created
resource "aws_ecs_cluster" "mycluster" {
}
I run terraform command to import the files
terraform import aws_ecs_cluster.mycluster test-cluster
I receive this error message
Error: Cannot import non-existent remote object
While attempting to import an existing object to aws_ecs_cluster.cluster, the
provider detected that no object exists with the given id. Only pre-existing
objects can be imported; check that the id is correct and that it is
associated with the provider's configured region or endpoint, or use
"terraform apply" to create a new remote object for this resource.
I've also ran aws configure adding the correct region.
Based on the comments.
The issue was caused by using wrong account in terraform and/or AWS console.
The solution was to use correct account.