Unbale to import subnets using google cloud network module - google-cloud-platform

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.

Related

Import terraform AWS VPC subnet having CIDR in resource name

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

How to import existing AWS resources like VPC using cdktf?

I can import AWS resources using AWS CDK (python) like this
# lookup existing VPC
vpc = ec2.Vpc.from_lookup(self,"vpc",vpc_id=vpcID,)
#lookup existing Security group
sec_group = ec2.SecurityGroup.from_lookup_by_id(self,'SG',sgID)
I cannot find proper documentation or example of doing the same in terraform cdktf
CDKTF(Terraform) provides data sources to fetch information from resources created outside of Terraform, therefore you can use the DataAwsVpc method.

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 using name tag for the resource

I am trying to import the existing resources into Terraform state. But I want to automate the import along with a terraform resource creation script.
I am creating a ELB and 2 instances in existing VPC, so first I have to import the existing VPC into my state file by using the name tag of the VPC. But I am seeing the import is working only by using the id key. Is it possible to import using other parameters apart from ID?
No you can't use other tags, you have to use the VPC ID to import your VPC
https://www.terraform.io/docs/providers/aws/r/vpc.html
https://www.terraform.io/docs/import/usage.html
You can achieve this using filter field.
e.g.
data "aws_vpc" "selected" {
filter = "tag:name=value"
}