0
0
Terraformcloud~30 mins

Bulk import strategies in Terraform - Mini Project: Build & Apply

Choose your learning style9 modes available
Bulk Import Strategies with Terraform
📖 Scenario: You are managing cloud resources using Terraform. You have multiple existing cloud resources that were created outside Terraform. To manage them efficiently, you want to import these resources into Terraform state in bulk.This project will guide you step-by-step to create a Terraform configuration and a bulk import script to import multiple resources at once.
🎯 Goal: Build a Terraform configuration for multiple cloud resources and create a bulk import script that imports these resources into Terraform state efficiently.
📋 What You'll Learn
Create a Terraform configuration file with multiple resource blocks
Define a list variable with resource IDs to import
Write a shell script to loop over the resource IDs and import them into Terraform state
Add a Terraform output to confirm imported resources
💡 Why This Matters
🌍 Real World
Cloud engineers often need to manage existing cloud resources with Terraform. Bulk import strategies save time and reduce errors when bringing many resources under Terraform management.
💼 Career
Knowing how to bulk import resources into Terraform state is a valuable skill for cloud infrastructure automation and management roles.
Progress0 / 4 steps
1
Create Terraform configuration with multiple resources
Create a Terraform configuration file named main.tf that defines three aws_s3_bucket resources with the exact names bucket1, bucket2, and bucket3. Use the aws provider and set the region to us-east-1 in a provider block.
Terraform
Need a hint?

Start by defining the AWS provider with region us-east-1. Then add three aws_s3_bucket resource blocks with the exact names bucket1, bucket2, and bucket3.

2
Define a list variable with bucket names to import
Add a Terraform variable named bucket_names of type list(string) with the exact values ["bucket1", "bucket2", "bucket3"] in a file named variables.tf.
Terraform
Need a hint?

Define a variable block named bucket_names with type list(string) and set the default to the list of bucket names.

3
Write a shell script to bulk import buckets into Terraform state
Create a shell script named bulk_import.sh that loops over the list bucket_names and runs the Terraform import command for each bucket. Use the exact variable name bucket in the loop and import each bucket with the command terraform import aws_s3_bucket.${bucket} ${bucket}.
Terraform
Need a hint?

Write a bash for loop iterating over bucket1, bucket2, bucket3 and run terraform import for each bucket using the variable bucket.

4
Add Terraform output to confirm imported buckets
Add a Terraform output named imported_buckets that outputs the list of bucket names from the variable bucket_names.
Terraform
Need a hint?

Add an output block named imported_buckets that outputs the variable bucket_names.