0
0
Terraformcloud~30 mins

Terraform import command - Mini Project: Build & Apply

Choose your learning style9 modes available
Using the Terraform Import Command
📖 Scenario: You have an existing AWS S3 bucket created outside Terraform. You want to manage it using Terraform without recreating it.
🎯 Goal: Learn how to write Terraform configuration for an existing AWS S3 bucket and import it into Terraform state using the terraform import command.
📋 What You'll Learn
Create a Terraform configuration file defining an AWS S3 bucket resource with the exact bucket name.
Set a variable for the bucket name.
Use the terraform import command to import the existing bucket into Terraform state.
Verify the imported resource is managed by Terraform.
💡 Why This Matters
🌍 Real World
Often, cloud resources are created manually or by other tools. Importing them into Terraform lets you manage them as code without recreating.
💼 Career
Knowing how to import existing infrastructure is essential for cloud engineers and DevOps professionals to maintain infrastructure as code.
Progress0 / 4 steps
1
Create Terraform configuration for the S3 bucket
Create a file named main.tf and write a Terraform resource block for an AWS S3 bucket named exactly my-existing-bucket. Use the resource name aws_s3_bucket.existing_bucket.
Terraform
Need a hint?

Use the resource block with type aws_s3_bucket and name existing_bucket. Set the bucket attribute to "my-existing-bucket".

2
Add a variable for the bucket name
Create a variable named bucket_name in a file named variables.tf with type string and default value "my-existing-bucket".
Terraform
Need a hint?

Define a variable block with name bucket_name, type string, and default "my-existing-bucket". Use var.bucket_name in the resource bucket attribute.

3
Import the existing S3 bucket into Terraform state
Run the command terraform import aws_s3_bucket.existing_bucket my-existing-bucket in your terminal to import the existing bucket into Terraform state.
Terraform
Need a hint?

Use the terraform import command with the resource address aws_s3_bucket.existing_bucket and the bucket name my-existing-bucket.

4
Verify the imported resource in Terraform state
Add the command terraform state list to verify that aws_s3_bucket.existing_bucket is now managed by Terraform.
Terraform
Need a hint?

Use terraform state list to see all resources Terraform manages. Confirm aws_s3_bucket.existing_bucket is listed.