Reputation: 151
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
Upvotes: 1
Views: 15839
Reputation: 394
If you created the rds resource like module, need to use module in front 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>
Upvotes: 0
Reputation: 529
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" "default" {
}
then run:
terraform import aws_rds_cluster.default <identifier>
From here, run terraform show
and find the resource you just imported and copy the contents back into your rds.tf
file (exclude any exported attributes mentioned until you get a clean terraform plan
Note: To import a 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
Upvotes: 0
Reputation: 41
resource "aws_db_instance" "xyz"{
#... db configuration...
}
run terraform import aws_db_instance.xyz aws_db_identifier
Upvotes: 4
Reputation: 2113
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.
Upvotes: 2
Reputation: 808
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!
Upvotes: 3
Reputation: 607
you need to create the resource block in your terraform project before you can import the existing resource.
Upvotes: 0
Reputation: 5897
you would nee mention the resource_name - User-defined name of the resource.
terraform import aws_db_instance.<resource_name> <DataBaseIndentifier>
Upvotes: 1