0
0
Terraformcloud~30 mins

Import state verification in Terraform - Mini Project: Build & Apply

Choose your learning style9 modes available
Import State Verification with Terraform
📖 Scenario: You are managing cloud resources using Terraform. Sometimes, resources are created outside Terraform, and you need to bring them under Terraform management without recreating them. This process is called importing state.In this project, you will create a Terraform configuration for an AWS S3 bucket, then simulate importing an existing bucket into Terraform state, and verify the import by checking the Terraform state file.
🎯 Goal: Build a Terraform configuration for an AWS S3 bucket, add a variable for the bucket name, import an existing bucket into Terraform state, and verify the import by inspecting the Terraform state file.
📋 What You'll Learn
Create a Terraform configuration file defining an AWS S3 bucket resource named my_bucket with a specific bucket name.
Add a Terraform variable named bucket_name to configure the bucket name.
Write the Terraform import command to import an existing S3 bucket into the my_bucket resource state.
Verify the import by checking the Terraform state file for the my_bucket resource.
💡 Why This Matters
🌍 Real World
Importing existing cloud resources into Terraform is common when migrating to infrastructure as code or managing legacy resources.
💼 Career
Cloud engineers and DevOps professionals often need to import resources to maintain consistent infrastructure management and avoid resource duplication.
Progress0 / 4 steps
1
Create Terraform configuration for AWS S3 bucket
Create a Terraform configuration file named main.tf that defines an AWS S3 bucket resource called my_bucket with the bucket name set to "example-bucket-terraform-import". Use the aws_s3_bucket resource type and set the bucket attribute exactly as shown.
Terraform
Need a hint?

Use the resource block with type aws_s3_bucket and name my_bucket. Set the bucket attribute to the exact bucket name.

2
Add a Terraform variable for the bucket name
Add a Terraform variable named bucket_name in a file named variables.tf with type string and default value "example-bucket-terraform-import". Then update the bucket attribute in the my_bucket resource in main.tf to use this variable.
Terraform
Need a hint?

Define a variable block with name bucket_name, type string, and default value matching the bucket name. Use var.bucket_name in the resource.

3
Write the Terraform import command
Write the exact Terraform CLI command to import the existing AWS S3 bucket named example-bucket-terraform-import into the Terraform resource aws_s3_bucket.my_bucket. The command should start with terraform import.
Terraform
Need a hint?

The import command format is terraform import <resource_type>.<resource_name> <resource_id>.

4
Verify the import by inspecting Terraform state
Write the Terraform CLI command to show the state details of the resource aws_s3_bucket.my_bucket. The command should start with terraform state show.
Terraform
Need a hint?

Use terraform state show <resource_type>.<resource_name> to view the imported resource details.