Search code examples
jsonterraformamazon-auroraterraform-aws-modules

How to import AWS Aurora cluster including instances in to terraform


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

Solution

  • 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.