What is Terraform?

Terraform is a powerful tool that allows you to define and manage your cloud resources using code. 

But what if you already have the terraform resources set up manually or through other means? 

In this blog, we will be looking into how to import these existing resources into Terraform. Once you know this, you can easily get your hands on managing everything in one place to make your Terraform workspace better.

Why Do You Have To Import Resources? 

Suppose, you work in a large organization where various teams manage different aspects of the infrastructure. While your team follows infrastructure as code (IaC) practices using Terraform for provisioning and managing most resources, there are situations where resources are created manually, and at a later stage, you want to import them in Terraform so the manually created resource can also be tracked and managed. 

Effortlessly Import Existing Resources into Terraform

The steps to import existing resources into Terraform are as follows:

  1. Identifying the Resources
  2. Prepare the Terraform Configuration
  3. Run the Import Command 
  4. Update the configurations 
  5. Verify the Import with Terraform Plan

Now, let’s implement all the above steps and see how it works.

We will create an EC2 Instance manually in the AWS console and then we import it into Terraform.

The following steps will guide you through the whole process – 

Step-01:

  • First, create one EC2 instance using the console and note the AMI ID and instance ID of EC2. 

 Step-02:

  • Set up the Terraform in your local and provide necessary access, secret keys, and region.
  • Create two configuration files- main.tf and providers.tf, you can take a reference from the below-attached images.

Note: Keep in mind that this block is not for creating resources, when we import the existing instance into the terraform state file, after that we add the necessary attributes in this block.

Step-03:

  • Now, let’s import the instance by running the below command. The instance ID is the one that we noted down from the console.

terraform import aws_instance.myec2  <instance_id>

If you get any error while running this command, you will first need to run the terraform init command and then the import command again.

Once the import is complete, check the tfstate file. The instance details should be there.

Step-04:

  • Now, as your instance got imported, let’s add the necessary attribute in the resource block.
  • Specify the specs like type, AMI ID as per the EC2 instance created in the console. 

Step-05:

Moving on, let’s test our configuration to check if it is correct. Run the terraform plan command.

The output should be like the below image:

Challenge Yourself

Solution:

  • First, create a bucket using a console.
  • Subsequently, include a resource block for this resource in the main.tf file.
  • After this, run the terraform import command:

terraform import aws_s3_bucket.mybucket <bucket_name>

  • After successful import, add the necessary attribute to the bucket resource block.
  • Check the configuration by running the Terraform plan.

The solution has covered everything in the gist that one might require to answer “how to create s3 bucket in AWS”

Conclusion

In this blog, we’ve explored the way of importing existing resources into Terraform and walked through a step-by-step guide on how to do it. This approach can be used in other cloud providers as well. 

Hope the article helped, and you can visit our website CloudZenia, for more such blogs.
Happy Learning!

Nov 15, 2023