📖 Scenario: You have an existing AWS S3 bucket created outside Terraform. You want to manage it using Terraform by importing it and writing the configuration.
🎯 Goal: Create a Terraform configuration that matches the imported AWS S3 bucket resource.
📋 What You'll Learn
Create a Terraform resource block for the S3 bucket named exactly aws_s3_bucket.example.
Add a variable bucket_name with the exact value my-imported-bucket.
Use the variable bucket_name in the resource configuration.
Add a lifecycle block to ignore changes to the acl attribute.
💡 Why This Matters
🌍 Real World
Managing existing cloud resources with Terraform is common when adopting infrastructure as code in teams.
💼 Career
Cloud engineers and DevOps professionals often import resources and write configurations to maintain infrastructure consistency.
Progress0 / 4 steps
1
Define the bucket_name variable
Create a Terraform variable called bucket_name with the default value "my-imported-bucket".
Terraform
Hint
Use the variable block with a default attribute.
2
Create the aws_s3_bucket.example resource block
Create a Terraform resource block named aws_s3_bucket.example that uses the variable bucket_name for the bucket attribute.
Terraform
Hint
Use resource "aws_s3_bucket" "example" and set bucket = var.bucket_name.
3
Add a lifecycle block to ignore changes to acl
Inside the aws_s3_bucket.example resource block, add a lifecycle block that ignores changes to the acl attribute.
Terraform
Hint
Use lifecycle { ignore_changes = ["acl"] } inside the resource block.
4
Complete the Terraform configuration for the imported S3 bucket
Ensure the Terraform configuration includes the variable "bucket_name" block and the aws_s3_bucket.example resource block with the lifecycle block ignoring acl changes.
Terraform
Hint
Make sure all parts are present exactly as specified.
Practice
(1/5)
1. What is the main purpose of writing Terraform configuration for an imported resource?
easy
A. To delete the resource automatically
B. To create a new resource from scratch
C. To match the existing resource so Terraform can manage it
D. To backup the resource data externally
Solution
Step 1: Understand Terraform import
Terraform import links an existing resource to Terraform state but requires matching configuration to manage it.
Step 2: Purpose of configuration
Writing configuration that matches the existing resource allows Terraform to track and manage it properly.
Final Answer:
To match the existing resource so Terraform can manage it -> Option C
Quick Check:
Import needs matching config = A [OK]
Hint: Import needs matching config to manage resource [OK]
Common Mistakes:
Thinking import creates new resources
Assuming import deletes resources
Believing import backs up data
2. Which Terraform block is required before importing an existing AWS S3 bucket named my-bucket?
easy
A. resource "aws_s3_bucket" "my_bucket" { bucket_name = "my-bucket" }
B. resource "aws_s3_bucket" "my_bucket" { bucket = "my-bucket" }
C. resource "aws_s3_bucket" "my_bucket" { name = "my-bucket" }
D. resource "aws_s3_bucket" "my_bucket" { id = "my-bucket" }
Solution
Step 1: Identify correct attribute for S3 bucket
The correct attribute to specify the bucket name in Terraform AWS provider is bucket.
Step 2: Match syntax for resource block
The resource block must use resource "aws_s3_bucket" "my_bucket" { bucket = "my-bucket" } to match the existing bucket.
After running terraform import aws_vpc.main vpc-1234abcd, Terraform plan shows it wants to change the CIDR block. What is the likely cause?
medium
A. The actual VPC CIDR block differs from the configured one
B. Terraform import failed and did not link the resource
C. The resource name in config does not match the import command
D. Terraform requires a different syntax for VPC import
Solution
Step 1: Analyze import and plan behavior
Import links the existing VPC to Terraform state, but if the configuration CIDR block differs from the actual VPC, Terraform plans to update it.
Step 2: Identify cause of plan changes
The difference in CIDR block values causes Terraform to want to change the resource to match config, which is not allowed for VPC CIDR.
Final Answer:
The actual VPC CIDR block differs from the configured one -> Option A
Quick Check:
Config mismatch causes plan changes [OK]
Hint: Check config matches actual resource attributes exactly [OK]
Common Mistakes:
Assuming import failed without checking state
Confusing resource name with import ID
Thinking import syntax is wrong
5. You have an existing Azure resource group named rg-prod created outside Terraform. You want to import it and manage it with Terraform. Which steps correctly describe the process?
hard
A. Create a new resource group in Terraform, then delete the existing one manually.
B. Run 'terraform import' first without any resource block, then write the resource block after.
C. Write a resource block with any name, run 'terraform import' with resource group name only, then apply.
D. Write a resource block with name 'rg-prod', run 'terraform import azurerm_resource_group.prod /subscriptions/0000/resourceGroups/rg-prod', then run 'terraform plan' to verify.
Solution
Step 1: Write matching resource block
Before importing, write a resource block with the correct resource type and name matching the existing resource group.
Step 2: Run terraform import with full resource ID
Use the full Azure resource ID to import the existing resource group into Terraform state.
Step 3: Verify with terraform plan
Run 'terraform plan' to confirm Terraform recognizes the imported resource and no unexpected changes are planned.
Final Answer:
Write a resource block with name 'rg-prod', run 'terraform import azurerm_resource_group.prod /subscriptions/0000/resourceGroups/rg-prod', then run 'terraform plan' to verify. -> Option D
Quick Check:
Config first, import with full ID, then plan [OK]
Hint: Write config first, import with full ID, then plan [OK]
Common Mistakes:
Importing without resource block
Using partial resource ID in import
Creating duplicate resources instead of importing
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.