Monday, June 7, 2021

How do you import existing resources into Terraform from AWS? | Terraform Import command

Let's say that you have created EC2 instances manually even before you started using Terraform. If you like to import your EC2 instance into Terraform management you can use import command.

 The terraform import command is used to import your existing resources(that were created outside Terraform) into terraform state. The command currently can only import one resource at a time. This means you can't yet point Terraform import to an entire collection of resources such as an AWS VPC and import all of it. 

Watch the steps in YouTube Video:


To import a resource, create a tf file first write a resource block for it in your configuration, establishing the name by which it will be known to Terraform:


sudo vi myec2.tf

resource "aws_instance" "myinstance" {

  # ...instance configuration...leave this as it is

}


terraform import can be run to attach an existing EC2 instance to this resource configuration:


pick up the instance ID of EC2 instance you want to import and execute the below command:
terraform import aws_instance.myinstance i-abcd1234


No comments:

Post a Comment

How to Setup Self-Hosted Linux Docker Build Agent in Azure DevOps | How to configure Self-Hosted Linux Docker Agents in Azure Pipelines | Create Custom Build Agents in Azure DevOps

  Let us learn how to configure a self-hosted agent using Docker in Azure DevOps pipelines. What is an Agent? An agent is computing infrastr...