Learn to import existing AWS resources into Terraform for effective management and streamline your infrastructure as code practices.
In this guide, you’ll learn how to import your pre-existing AWS resources into Terraform. This process is essential when some resources—such as an EC2 instance—have been created using other tools or methods and you want to manage them with Terraform. For more context on alternative provisioning tools, refer to the Ansible Advanced Course.
A unique identifier for the resource you wish to import (for example, the EC2 instance ID).
The resource’s current configuration information from AWS.
Remember that the Terraform Import command updates only the state file. You must manually update your configuration files to reflect the imported resources.
Start by creating an empty resource block in your Terraform configuration file. In this example, we define a resource named “webserver-2”. You can choose any unique name that suits your configuration and state.
With your resource block in place, execute the Terraform Import command from your terminal. The syntax is as follows: terraform import [resource address] [unique resource attribute]For an EC2 instance, the resource address is “aws_instance.webserver-2” and the unique resource attribute is the instance ID. Run the command as shown below:
Copy
Ask AI
$ terraform import aws_instance.webserver-2 i-026e13be10d5326f7aws_instance.webserver-2: Importing from ID "i-026e13be10d5326f7"...aws_instance.webserver-2: Import prepared!Prepared aws_instance for importaws_instance.webserver-2: Refreshing state... [id=i-026e13be10d5326f7]Import successful!The resource has been imported into your Terraform state and is now managed by Terraform.
Step 3: Update the Resource Block with Configuration
After a successful import, inspect either the AWS management console or the Terraform state file to capture key resource attributes needed for the configuration. An example snippet from the state file might look like this:
To validate that Terraform correctly recognizes the imported resource, run the Terraform plan. This operation compares your configuration with the current state and should report no changes if everything is in sync.
Copy
Ask AI
$ terraform planRefreshing Terraform state in-memory prior to plan...The refreshed state will be used to calculate this plan, but will not be persisted to local or remote state storage.aws_instance.webserver-2: Refreshing state... [id=i-0d7c008069819ff8]-------------------------------------------------------------------------------No changes. Infrastructure is up-to-date.
The message “No changes. Infrastructure is up-to-date.” confirms that your EC2 instance is now fully managed by Terraform with no discrepancies.
Now that your EC2 instance is under Terraform management, proceed with the usual Terraform workflow for future modifications. Always update your configuration file and run terraform plan followed by terraform apply to implement changes.
By following these steps, you effectively bring your external AWS resources under the management of Terraform, streamlining your infrastructure as code practices. Happy provisioning, and see you in the next guide!