Import Existing Resources using Terraform



In this blog, I will walk you through the ways to import pre-existing cloud resources in terraform.

Why Import is required?

There will be a scenario, where you will have a stringent timeline to setup a cloud Infrastructure and it can lead to situations where infrastructure needs to be created manually due to time pressures, emergency releases or just the fact that the infrastructure exists, and terraform was never used in the first instance. In a worst case scenario, you can lose the terraform.tfstate file. Terraform import will be essential in most of all these scenarios.

Let's Import

With an understanding of why Import is required, let us begin by importing a simple AWS resource S3. Firstly, we need to setup the Terraform and AWS credentials locally, we will not go into the details of installing and setting AWS credentials in this blog but you can follow the official websites for Installing a Terraform and  Setting the AWS credentials. To import the S3 resource into Terraform, follow the below step-by-step guide.


Step 1 – Create the S3 bucket Manually

As a simple example, I am taking S3 as a resource to import. Lets create the S3 bucket manually from the AWS console. This could be an optional step if you already have a target resource to be imported.

In AWS Management console, navigate to S3 service and create a bucket with your desired name, for this example I have taken the bucket name as terraform-import-example-bucket


Step 2 – Create main.tf and set Terraform Provider Configuration

We are going to import S3 bucket into the terraform configuration, create a file main.tf and configure the provider in the tf file. The file should like below
// Provider configuration
terraform {
 required_providers {
   aws = {
     source  = "hashicorp/aws"
     version = "~> 3.0"
   }
 }
}
 
provider "aws" {
 region = "ap-south-1"
}

Run terraform init to initialize the terraform modules. Below is the output for successful terraform initialization.


Step 3: Enter the config of the resource to import

We need to enter the configuration of S3. Basically, it will be hard to enter all the arguments of the resource but terraform import doesn't require all arguments to pass in the configuration instead only the required one. Append the below config in main.tf

resource "aws_s3_bucket" "myimportbucket" {
 }


Step 4: Import the Resource

We will import the S3 bucket which was configured manually to Infrastructure as a code and we can achieve this by running the import command as below

terraform import aws_s3_bucket.myimportbucket <bucket_name>
where <bucket_name> is name of the bucket which we are importing, in this case, bucket name is terraform-import-example-bucket

You should be seeing below output after running the command

Step 5: Validate the state file and plan

You should observe a terraform.tfstate file will be created after importing the resource. In this case we haven't used any remote backend so state file will be updated in the local. It's not recommended to use tf state file in local, we have to keep state file in remote location like S3.

You can validate the S3 resource configuration from the state file. Now we have the configuration and lets run the terraform plan and observe the output.

We can see there is change happening which we have to avoid. Eventually, we should achieve a state of 0 difference. But this change is a bug which is being tracked in issues, as a workaround, I added the arguments in the ignore_changes like below
resource "aws_s3_bucket" "myimportbucket" {
  lifecycle {
    ignore_changes = [
      acl,
      force_destroy
    ]
  }
}

Let's run terraform plan again

We can observe, there is no difference in the configuration. If you want to modify any configuration later then you can easily change in the main file and you can maintain you infrastructure within the code.


Conclusion:

In this blog post, I have shown you on how the terraform import can be used to import existing resources. We took S3 as an example of existing resource and later S3 resource configuration imported into the terraform. Hope this blog helped you in your similar use case.

Thank you for reading!

Comments

Popular posts from this blog

Connect to Linux EC2 Instance if Key pair is lost after Initial Launch

Setup Grafana on AWS EKS and integrate with AWS Cloudwatch

Start or Stop services in multiple Windows EC2 Instances using AWS Systems Manager

Concourse CI Installation and Configuration in Windows

Install SSM Agent in Amazon EC2 Instance

Deploy AWS infrastructure using Terraform and GitHub Actions

Automate Permission Boundary Attachment to IAM roles and Users

AWS Route 53 Inbound Resolver to resolve DNS for Multi Account Organization

Hosting AWS VPC Interface Endpoints in Shared Model