AWS Logo
Menu
Importing AWS RDS Resources with Terraform

Importing AWS RDS Resources with Terraform

This article explores the process of importing existing AWS RDS databases into Terraform infrastructure code.

Published Feb 17, 2025
This article is a continuation of a series of articles on aws rds migration and is the second in a series. First part is describe at article AWS RDS PostgreSQL Migration and Splitting a Monolithic Database into Multiple RDS Instances using python.

Overview

This article explores the process of importing existing AWS RDS databases into Terraform infrastructure code. This is a crucial step in ensuring an Infrastructure as Code (IaC) approach for managing cloud resources.
When a company aims to save costs while planning a Disaster Recovery Plan (DRP), the simplest approach is backup and resource restoration. In cloud infrastructure, resources are typically restored in a different region.
This article does not cover other strategies like stand-by, active-active DRP approaches, as this is not the main topic. The key intent is to demonstrate a "cost-effective and simple..." Terraform import mechanism and its use for importing RDS resources.
When restoring resources, it becomes necessary to manage them with Terraform code. The Terraform import mechanism enables rapid and secure resource recovery.
Before Terraform 1.6, import required manual state management and careful tracking of resource locations, which could lead to potential errors. From version 1.6 onwards, Terraform offers a simpler and more reliable import system, significantly streamlining infrastructure recovery in DRP scenarios.

Import Process Diagram

Import Process Diagram
Import Process Diagram

Why is Importing Necessary?

Importing existing resources allows:
  • Centralized infrastructure management using Terraform
  • Tracking configuration changes through version control
  • Avoiding manual resource setup and reducing error risks
  • Simplified deployment and scaling
  • Efficient recovery after incidents or failures

Key Steps for Importing RDS Resources

1. Change Tracking

  • Infrastructure State Preservation
  • Terraform uses the terraform.tfstate file to store the current state of resources.
  • This helps avoid discrepancies between the actual state in the cloud and the configuration in code.
  • Conflict Prevention
  • Using locking mechanisms (terraform plan) prevents simultaneous conflicting updates.
  • This is particularly useful in collaborative environments.
  • Modification History
  • Using Git or other version control systems helps track changes and quickly revert to previous configurations.
  • Terraform logging enables tracking of the reasons and consequences of modifications.

2. Process Automation

  • CI/CD Integration
  • Terraform can be integrated with automation systems like GitHub Actions, GitLab CI/CD, Jenkins, or AWS CodePipeline.
  • This enables automatic change application after testing.
  • Automated Testing
  • terraform validate checks the correctness of configuration syntax.
  • terraform plan evaluates upcoming changes before applying them.
  • terratest is used for infrastructure unit testing.
  • Scheduled Updates
  • Using automated infrastructure update processes ensures timely updates without risk of failures.
  • Applying terraform apply -auto-approve in controlled environments simplifies configuration updates.
  • Automated Deployment
  • Terraform modules allow standardized environment deployment without manual intervention.
  • Automated deployment reduces time-to-market for products.

3. Scalability

  • Flexible Deployment
  • Terraform supports parameterized variables, making it easy to adapt configurations for different environments (dev, staging, production).
  • Using modules helps avoid code duplication.
  • Multi-Region Deployment
  • Terraform allows managing infrastructure in multiple AWS regions from a single configuration.
  • Using provider with alias simplifies multi-region deployments.
  • In our case, we move resources to the "Blue" region (provider = aws.Blue) and import them into the code.
  • Environment Cloning
  • Terraform supports terraform workspace, enabling the creation of isolated environments for testing without impacting production.
  • The ability to quickly copy configurations makes it easy to create new infrastructure instances.

Code Examples

Import an Existing RDS into Terraform

Declaring an RDS Resource in Terraform

Planning and applying changes

Conclusion

Strategic Benefits

  • Improved Manageability
  • Centralized management of all resources through a single configuration.
  • Ease of tracking and controlling changes.
  • Automated updates and dependency management.
  • Resource Optimization
  • Cost reduction through proper infrastructure planning.
  • Automated scaling and efficient resource distribution.
  • Minimization of human error through automated deployments.

Long-Term Benefits

  • Investment in the Future
  • Simplified infrastructure expansion with IaC.
  • Ensuring deployment stability and repeatability.
  • Compliance with security and regulatory standards.
  • Operational Improvements
  • Reduced infrastructure management time.
  • Automation of deployment and update processes.
  • Enhanced overall reliability and service availability.

References: Hashicorp
 

Comments