Importing existing AWS infrastructure into Terraform involves several approaches, depending on your needs. Here are the common methods:
1. Using terraform import
Terraform's import command allows you to bring existing AWS resources under Terraform management by mapping them to your configuration.
Steps:
-
Write a Terraform configuration that defines the resource you want to import.
Example (main.tf):
resource "aws_instance" "example" {
ami = "ami-0c55b159cbfafe1f0" # Example AMI
instance_type = "t2.micro"
# Other required arguments
}
-
Run terraform import to associate the existing AWS resource with the Terraform state.
terraform import aws_instance.example i-1234567890abcdef0
- Replace
i-1234567890abcdef0 with the actual AWS resource ID.
-
Update the configuration to match the imported resource's settings.
- Run
terraform plan to detect differences and update the config accordingly.
Limitations:
- Only imports the resource into the state file; you must manually define the configuration.
- Does not automatically generate Terraform code.
2. Using terraform plan -generate-config-out (Terraform 1.5+)
Terraform 1.5 introduced a way to generate configuration from imported resources.
Steps:
-
Import the resource as usual:
terraform import aws_instance.example i-1234567890abcdef0
-
Generate a configuration file:
terraform plan -generate-config-out=generated_resources.tf
- This creates a file with the imported resource's configuration.
-
Review and refine the generated configuration.
3. Using AWS Terraformer (Open-Source Tool)
AWS Terraformer is a tool by Google that can automatically generate Terraform configurations from existing AWS resources.
Steps:
-
Install Terraformer:
brew install terraformer # macOS
Or download from GitHub.
-
Run Terraformer to import AWS resources:
terraformer import aws --resources=ec2 --regions=us-east-1 --profile=default
- This generates
.tf files in a generated/aws directory.
-
Review and modify the generated Terraform code.
Pros:
- Automatically generates Terraform configurations.
- Supports multiple AWS services.
Cons:
- May not cover all AWS resources.
- Requires manual adjustments for complex setups.
4. Using AWS Cloud Development Kit (CDK) + cdktf
If you're using AWS CDK, you can use cdktf to import existing infrastructure.
Steps:
- Install
cdktf:
npm install -g cdktf-cli
- Initialize a project:
cdktf init --template=typescript
- Use
cdktf import to import resources.
5. Using Third-Party Tools
- Terragrunt (for managing multiple Terraform modules).
- Infracost (for cost estimation, but can help identify resources).
- AWS Config + Custom Scripts (to extract resource details and generate Terraform).
Best Practices for Importing AWS Infrastructure
- Backup your Terraform state before importing.
- Use
terraform plan after importing to detect drift.
- Modularize your Terraform code for better management.
- Test in a non-production environment first.
Conclusion
- For single resources, use
terraform import.
- For bulk imports, use Terraformer.
- For automated workflows, consider CDK or custom scripts.
Would you like a detailed guide on any specific method?