Terraform Import aws resources - amazon-web-services

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

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.

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 map AWS resource type to Terraform type

I am trying to import existing AWS resources through Terraform import cmd.
Programatically I am able to take AWS resource ID through Resource tagging API but then I can not find a proper way to map it to Terraform type.
For example EC2 instance i-abcd has to be imported in Terraform through the following cmd:
terraform import aws_instance.foo i-abcd
Is there any way that I can determine the Terraform type of the i-abcd knowing that it is an instance in AWS?
Something like a dictionary:
AWS Resource type | Terraform Resource type
instance | aws_instance
Is there any solution like the above one out there or any workarounds to create it without too many manual mappings?
Thanks in advance!

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.

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"
}