Here is the complete Terraform export guide in one single file, with section titles clearly emphasized.
Exporting Existing AWS Infrastructure to Terraform
1. Overview
It is possible to export an existing AWS environment into Terraform code. Although AWS does not provide a native method, the Terraformer tool can be used to convert AWS resources into Terraform configurations.
2. Using Terraformer (Recommended)
What is Terraformer?
Terraformer is a tool that automatically converts AWS infrastructure into Terraform code. It also generates a Terraform state file, allowing Terraform to manage the resources immediately.
Key Features:
- Converts AWS resources into Terraform configurations automatically.
- Maintains dependencies between resources.
- Generates
terraform state
, making it easy to manage infrastructure.
3. Steps to Export AWS Configuration with Terraformer
Step 1: Install Terraformer
To install Terraformer on macOS, run:
brew install terraformer
For Linux, download the binary and move it to a directory in your PATH
:
wget https://github.com/GoogleCloudPlatform/terraformer/releases/latest/download/terraformer-linux-amd64
chmod +x terraformer-linux-amd64
sudo mv terraformer-linux-amd64 /usr/local/bin/terraformer
Verify the installation:
terraformer --version
Step 2: Configure AWS Credentials
Terraformer uses AWS CLI credentials. Configure them using:
aws configure
Alternatively, set the credentials as environment variables:
export AWS_ACCESS_KEY_ID="your-access-key"
export AWS_SECRET_ACCESS_KEY="your-secret-key"
export AWS_REGION="us-east-1"
Step 3: Export AWS Resources
Export all AWS resources:
terraformer import aws --resources=all --regions=us-east-1
To export only specific resources, such as EC2, S3, and IAM:
terraformer import aws --resources=ec2,s3,iam --regions=us-east-1
To export resources with specific tags:
terraformer import aws --resources=ec2 --regions=us-east-1 --filter="Name=tags.environment;Value=production"
Step 4: Review Exported Terraform Code
Terraformer generates Terraform configuration files in the generated/aws/
directory. The directory structure will look like this:
generated/aws/
├── ec2
│ ├── instances.tf
│ ├── security_groups.tf
│ ├── variables.tf
│ ├── provider.tf
│ ├── terraform.tfstate
├── s3
│ ├── buckets.tf
│ ├── variables.tf
│ ├── provider.tf
│ ├── terraform.tfstate
├── iam
│ ├── users.tf
│ ├── roles.tf
│ ├── policies.tf
Step 5: Initialize Terraform
Navigate to the exported directory and initialize Terraform:
cd generated/aws
terraform init
Step 6: Apply Terraform State Management
Check the changes with:
terraform plan
Apply the Terraform configuration:
terraform apply
This step ensures that Terraform starts managing the existing AWS infrastructure.
4. Considerations and Limitations
- Not all AWS services are supported. Some resources must be added manually.
- Conflicts may occur when applying Terraform. Review the generated state and configuration carefully.
- Terraform and Terraformer versions must be kept updated since new AWS features may not be compatible with older versions.
5. Summary
Terraformer (Recommended)
Uses terraformer
to automatically export AWS configuration.
Manual Export
Uses aws cli + terraform import
to import resources individually.
Infrastructure as Code Optimization
Uses terraform state
management to enable Terraform control over existing AWS infrastructure.
Terraformer provides an efficient way to convert an existing AWS infrastructure into Terraform code, making it easier to manage as Infrastructure as Code.
6. Conclusion
By following these steps, you can export an existing AWS environment into Terraform and manage it efficiently.
If you need additional support for specific AWS services, refer to the official Terraform documentation.