Import Existing Cloud Resources
Bring existing cloud resources under Terraform management without recreating or disrupting them.
Prerequisites
- -Terraform installed
- -Existing cloud resources to import
- -Cloud provider credentials configured
Steps
Write the resource block for the existing resource
Define a resource block in your configuration that matches the existing cloud resource's type and settings.
Check the provider documentation for all required and optional arguments for the resource type.
Import the resource into state
Run terraform import to associate the existing resource with your new resource block in state.
Import only updates state. If your configuration does not match the real resource, the next apply will modify it.
Generate a plan to find config drift
Run plan to see what differences exist between your written config and the actual resource attributes.
Adjust configuration until plan is clean
Update your .tf file to match the real resource attributes until terraform plan shows no changes.
Exit code 0 means no changes. Exit code 2 means there are still differences to resolve.
Use import blocks for bulk import (Terraform 1.5+)
For Terraform 1.5 and later, use declarative import blocks to import multiple resources in one apply.
The -generate-config-out flag auto-generates the resource config for you, saving manual work.
Full Script
FAQ
Discussion
Loading comments...