Import Existing AWS Resources using Terraform (S3 Bucket)

In this we are going to cover How to Import Existing AWS Resources using Terraform (S3 Bucket).

Terraform import existing infrastructure Featured Image 1

What if we have infrastructure that we already created without using Terraform, but we want to manage with Terraform. In such cases, we can use the Terraform import command to import those resources into our Terraform state.

What is Terraform Import Command?

  • Terraform is an IAC tool that helps us manage our infrastructure by building, changing, and versioning infrastructure. When we create infrastructure with Terraform, we define our desired state in code. Then Terraform creates, modifies, or deletes the resources as needed to match that desired state.
  • The terraform import command in Terraform is used to bring existing infrastructure, which was not initially created by Terraform, under Terraform management. This command allows you to associate and import existing resources into your Terraform state, enabling you to manage them using Terraform’s configuration files.
  • When you import an existing resource, Terraform creates a new state resource for that resource, based on the current state of the resource in the provider. Terraform then updates that state resource to match the desired state defined in your configuration.
  • Example: Suppose you have an existing AWS S3 Bucket with the name “my-existing-bucket“. You can import it into Terraform like this:
terraform import aws_s3_bucket.my_bucket my-existing-bucket
  • Here, aws_s3_bucket.my_bucket is the Terraform resource type and name, and my-existing-bucket is the actual S3 bucket name.
  • The terraform import command is useful when you have pre-existing infrastructure that you want to transition to Terraform without recreating it. It helps maintain and manage infrastructure as code, providing consistency and versioning benefits.

Prerequisites

  • You’ll need an Amazon Web Services (AWS) account. If you don’t have one, you can sign up for a free AWS account.
  • Basic knowledge of AWS services and concepts.
  • Visual Studio on your system.
  • Hashicorp Terraform Extension. You can download it from the official Terraform website.

How to Import Existing AWS Resources using Terraform (S3 Bucket)

Steps for implementation to this project

  • Let’s go to AWS Console and Search S3 service and click on create a bucket.
s3 serch
  • Provide a unique name, mine is “terraform-s3buc” choose a region and leave the rest to defaults. Then click on create bucket.
s3 create
  •  You will see the green pop up with ” Successfully created bucket ‘terraform-s3buc‘ “
  • Now head back to your local terminal. Create a new folder to work and cd into that folder. Then open vs code for further execution.
s3 command
  • Now, Let’s create the following organizational structure as shown below.
    • Create a Folder – terraform-s3buc
    • Create 4 Files in our terraform-s3buc – 1) variables.tf  2) terraform.tfvars 3) main.tf
s3 folder

Create a variables.tf file in terraform-s3buc folder

  • Enter the below code
#variables.tf
variable "access_key" {
    description = "Access key to AWS console"
}
variable "secret_key" {
    description = "Secret key to AWS console"
}
variable "region" {
    description = "AWS region"
}
s3 var

Create a terraform.tfvars file in terraform-s3buc folder

  • Enter the below code
#terraform.tfvars
region = "us-east-1"
access_key = "<YOUR AWS CONSOLE ACCESS ID>"
secret_key = "<YOUR AWS CONSOLE SECRET KEY>"
s3tfvarss

Create a main.tf file in terraform-s3buc folder

  • Enter the below code
#main.tf
#defining the provider as aws
provider "aws" {
    region     = "${var.region}"
    access_key = "${var.access_key}"
    secret_key = "${var.secret_key}"
}

#create s3 bucket
resource "aws_s3_bucket" "my_bucket" {
  bucket = "terraform-s3buc"
}
s3 maijn

Initialize and Apply Terraform Configuration

  • Open a terminal in the directory containing your main.tf file and run the following commands:
terraform init
s3 init
  • Run the following command to import the S3 bucket:
terraform import aws_s3_bucket.my_bucket terraform-s3buc 
  • This command tells Terraform to import the existing S3 bucket with the name ‘terraform-s3buc’ and create a new Terraform state resource for it named aws_s3_bucket.my_bucket.
s3 import
#main.tf
#defining the provider as aws
provider "aws" {
    region     = "${var.region}"
    access_key = "${var.access_key}"
    secret_key = "${var.secret_key}"
}

#create s3 bucket
resource "aws_s3_bucket" "my_bucket" {
  bucket = "terraform-s3buc"
  tags = {
    "name" = "terrform-s3bucket"
  }
  force_destroy = false
}
Screenshot 2023 12 30 234703
  • Note that after running the import command, we need to create a resource block for it, so it actually requires some work beforehand. However, it is a way to bring the existing infrastructure in to terraform.
  • Verify that the import was successful by running terraform plan. Terraform should show that there are no changes to be made to the S3 bucket resource.
s3 plan
  • Great. Now, we have imported our S3 bucket into our terraform code.
  • Note: Do not forget to destroy s3 bucket with terraform destroy command (Since we imported it, you do not need to go to AWS console to manually delete it)

Conclusion:

In this article, we’ve demonstrated How to import existing AWS Infrastructure into the Terraform and destroyed it later not to incur charges.

Reference:-

About Sohail Behlim

Hey, I am Sohail Behlim, I am an aspiring DevOps and Cloud enthusiast who is eager to embark on a journey into the world of DevOps and Cloud. With a strong passion for technology and a keen interest in DevOps and Cloud based solutions, I am driven to learn and contribute to the ever-evolving field of DevOps and Cloud.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share via
Copy link