Importing Existing Infrastructure (EC2) Into Terraform
Getting the pre-existing cloud resources under the Terraform management is facilitated by Terraform import
. import is a Terraform CLI command which is used to read real-world infrastructure and update the state.
Let us begin by importing a simple resource – EC2 instance in AWS.
Terraform is an infrastructure as a code tool that enables you to safely and predictably provision and manage infrastructure in any cloud.
For this tutorial I'm configuring Terraform on an EC2 instance. You can configure it locally too.
- Launch a simple EC2 instance with the default settings.
The AWS Command Line Interface (AWS CLI) is an open-source tool from Amazon Web Services (AWS). You can use it to interact with AWS services using commands in your command line shell.
- Create a new IAM user with an administratorAccess role.
- Create an access key by selecting the CLI option.
- Turn off the administrator and user option in Internet Enhanced Security Configuration.
- Now download AWS CLI in the EC2 instance and install it. - Then open cmd and run as administrator. - Run the command to check if AWS CLI is installed properly or not.
aws --version
-
Log into the instance.
-
Download Terraform and make a Terraform folder in C-drive and move the .exe file to it.
-
Setup environment variable.
-
Download the VS code and install Terraform plugins.
Suppose we have one another EC2 instance (server102) running in our AWS account that someone created manually. Now we want to import that in Terraform.
The aim of this step is to import this EC2 instance into our Terraform configuration.
- Create a folder in C-drive and open it in VS code. Write down the below code in the
main.tf
file.
provider "aws" {
region = "us-east-1"
}
resource "aws_instance" "server102" {
ami = "unknown"
instance_type = "unknown"
}
The only reason I have included ami and instance_type attribute, is that they are the required arguments for aws_instance resource block.
- Open the terminal and enter below command.
terraform init
- for initializing terraformaws configure
- to authenticate user- Enter Access key and Secret access key.
Think of it as if the cloud resource (EC2 instance) and its corresponding configuration were available in our files. All that’s left to do is to map the two into our state file. We do that by running the import command as follows.
terraform import aws_instance.server102 instance_id
A successful output should look like this:
Please notice that the directory now also contains terraform.tfstate file. This file was generated after the import command was successfully run.
- Get values instance parameter from state file and replace with the
unknown
keyword.
provider "aws" {
region = "us-east-1"
}
resource "aws_instance" "server102" {
ami = "ami-0f9c44e98edf38a2b"
instance_type = "t2.micro"
tags = {
Name = "server102"
}
}
- Enter command -
terraform plan
This time the plan does not indicate the replacement of the EC2 instance. If you get the same output, you are successful in partially importing our cloud resource. You are currently in a state of lowered risk—if we apply the configuration now, the resource will not be replaced, but a few attributes would change.
To destroy instance, enter command terraform destroy --auto-approve
This will terminate the instance.