3

I need to import the existing Aurora cluster in to terraform. I tried terraform import aws_rds_cluster.sample_cluster cluster statement. I got the state file ready also I could also do Terraform show However, When I try to destroy the cluster Terraform tries to delete the cluster without the instances under it - So the destroy command is failing.

`Error: error deleting RDS Cluster (test): InvalidDBClusterStateFault: Cluster cannot be deleted, it still contains DB instances in non-deleting state.status code: 400, request id: 15dfbae8-aa13-4838-bc42-8020a2c87fe9`

Is there a way I can import the entire cluster that includes instances as well? I need to have a single statefile that can be used to manage entire cluster(including underlying instances). Here is the main.tf that is getting used to call the import -

access_key = "***"
secret_key = "*****"
region = "us-east-1"
}
resource "aws_rds_cluster" "test" {
   engine               = "aurora-postgresql"
   engine_version       = "11.9"
   instance_class       = "db.r5.2xlarge"
   name                 = "test"
   username             = "user"
   password             = "******"
   parameter_group_name = "test"
}```
  • Have you imported `aws_rds_cluster_instance` as well? – Marcin Jan 21 '21 at 11:00
  • @Marcin I was under perception that it will include all the underlying resources as well. Does it has to be done separately or need to mention within the main.tf (which includes resource)? Here is my main.tf which was used to call import- – Chetan Dev Khanna Jan 23 '21 at 10:16
  • You have to import `aws_rds_cluster_instance` as well. – Marcin Jan 23 '21 at 10:17
  • @Marcin provider "aws" { access_key = "*********" secret_key = "*************" region = "us-east-1" } resource "aws_rds_cluster_instance" "test" { engine = "aurora-postgresql" engine_version = "11.9" instance_class = "db.r5.2xlarge" name = "test" username = "user" password = "******" parameter_group_name = "test-paramter" } – Chetan Dev Khanna Jan 23 '21 at 10:17
  • It would be better to update question with correctly formatted code. – Marcin Jan 23 '21 at 10:20
  • Thanks for the response @Marcin. I need to make sure that we have a single statefile created for entire cluster which includes all the underlying instances. – Chetan Dev Khanna Jan 23 '21 at 10:21
  • You have to import all resources that you would normally create by designing the cluster yourself in TF. [former2](https://github.com/iann0036/former2) can generate TF config files. This could be a good start if you are not sure how to import manually everything? – Marcin Jan 23 '21 at 10:24
  • 1
    Thanks @Marcin.This is really helpful ! I have updated the question. I am getting better at asking questions now :) – Chetan Dev Khanna Jan 23 '21 at 10:32
  • No problem. If you don't mind I can provide an answer with some extra info. – Marcin Jan 23 '21 at 10:33

1 Answers1

2

Based on the comments.

Importing just aws_rds_cluster into TF is not enough. One must also import all aws_rds_cluster_instance resources which are part of the cluster.

If the existing infrastructure is complex, instead of fully manual development of TF config files for the importing procedure, an open-sourced third party tool, called former2, could be considered. The tool can generate TF config files from existing resources:

Former2 allows you to generate Infrastructure-as-Code outputs from your existing resources within your AWS account.

TF is one of the outputs supported.

Marcin
  • 215,873
  • 14
  • 235
  • 294