1
votes

I am terraform newbie. I've created RDS manually in AWS.

Now I tried to import RDS into terraform using https://www.terraform.io/docs/providers/aws/r/db_instance.html so that I can use terraform to update RDS. The database name is mydb. I have ran terraform init but got this error, still not sure how to resolve it. Please advise. thx

terraform import aws_db_instance mydb
Error: invalid resource address "aws_db_instance"

For information on valid syntax, see:
https://www.terraform.io/docs/internals/resource-addressing.html
7

7 Answers

4
votes
resource "aws_db_instance" "xyz"{

     #... db configuration...
}

run terraform import aws_db_instance.xyz aws_arn_for_db

2
votes

I know this question is old, but other people might be interested in a solution. In this example, I have a RDS PostGreSQL database named "db1" which I want to import into terraform.

You will need to describe the database to terraform, like this example:

resource "aws_db_instance" "db1" {
     allocated_storage    = 20
     engine               = "postgres"
     engine_version       = "11.5"
     instance_class       = "db.t2.micro"
     name                 = "db1"
     username             = "postgres"
     password             = "secret"
     parameter_group_name = "default.postgres11"
}

Invoke terraform as:

terraform import aws_db_instance.db1 db1  

And hopefully you will see

aws_db_instance.db1: Importing from ID "db1"...
aws_db_instance.db1: Import prepared! 
  Prepared aws_db_instance for import
aws_db_instance.db1: Refreshing state... [id=db1]

Import successful!
2
votes

Terraform import creates the state file .tfstate file only based on the existing configuration (resource.tf/main.tf).

As of now terraform doesn't provision the configuration file for you. Whenever you use import command, it creates a tfstate file based on your existing AWS resource and maps it with the resource configuration which you created.

Then you can run terraform plan command to see what is the difference between your resource and the tfstate file to adjust your resource parameters according.

The error which you got clearly mentioned that you don't have any local resource configuration available to map the tfstate state.

Step 1: create a local resource file

resource "aws_db_instance" "db1" {
     allocated_storage    = 20
     engine               = "postgres"
     engine_version       = "11.5"
.
.
.
.
other config etc...
}

Step 2: use terraform import to import existing resource and map it to 'aws_db_instance'.

Step 3: Run terraform plan to see the config difference between your local resource and imported resource and fix it.

Once terraform come up with creating new config automatically whenever we import existing resource, we don't need to worry about this at all.

0
votes

you would nee mention the resource_name - User-defined name of the resource.

terraform import aws_db_instance.<resource_name> <DataBaseIndentifier>
0
votes

If you created the rds resource like module, need to use module inford of the resource name.

you can see the resource name in terraform plan command.

terraform import module.rds_sample.aws_rds_cluster_instance.rds_cluster_instances <R-name>
0
votes

you need to create the resource block in your terraform project before you can import the existing resource.

0
votes

Currently, you will need to setup an empty resource block in a .tf file before importing existing resources into Terraform.

Example

rds.tf:

resource "aws_rds_cluster" "reware_aurora_cluster" {
   
} 

then run:

terraform import aws_rds_cluster.reware_aurora_cluster <identifier>

Then you should see Import successful! and from here you should run terraform show and find the resource you just imported and copy the contents back into your rds.tf file (exclude any attributes that are listed in the Terraform resource's doc as exported. enter image description here

Note: If you are allowed to import that specific resource, you can find more at the very bottom on the resource's Terraform docs page, e.g. https://www.terraform.io/docs/providers/aws/r/rds_cluster.html