0
0
Terraformcloud~30 mins

State replace-provider in Terraform - Mini Project: Build & Apply

Choose your learning style9 modes available
Terraform State Replace-Provider
📖 Scenario: You have an existing Terraform project that uses an old provider source. The provider source has changed, and you need to update your Terraform state to reflect the new provider source without destroying and recreating resources.
🎯 Goal: Update the Terraform state to replace the old provider source with the new provider source using the terraform state replace-provider command.
📋 What You'll Learn
Create a Terraform configuration with an AWS provider using the old source.
Initialize Terraform to download the old provider.
Add a configuration variable to specify the new provider source.
Use the terraform state replace-provider command to update the state to the new provider source.
Verify the state file reflects the new provider source.
💡 Why This Matters
🌍 Real World
Terraform providers sometimes change their source addresses. Updating the state to reflect these changes without destroying resources is important for smooth infrastructure management.
💼 Career
Knowing how to safely update Terraform provider sources in state files is a valuable skill for cloud engineers and DevOps professionals managing infrastructure as code.
Progress0 / 4 steps
1
Create Terraform configuration with old provider source
Create a file named main.tf with a terraform block that specifies the required provider hashicorp/aws with source hashicorp/aws and version ~> 3.0. Then add a provider block for aws with region set to us-east-1.
Terraform
Need a hint?

Use the terraform block to specify provider requirements and a provider block to configure AWS region.

2
Add new provider source variable
Add a variable named new_provider_source with default value registry.terraform.io/hashicorp/aws to the main.tf file.
Terraform
Need a hint?

Define a variable block with name new_provider_source and set its default value.

3
Run terraform state replace-provider command
Run the command terraform state replace-provider hashicorp/aws ${var.new_provider_source} in your terminal to update the provider source in the Terraform state.
Terraform
Need a hint?

This step is a command line action. Make sure to run the exact command to replace the provider source in the state.

4
Update provider source in terraform block
Update the source attribute in the required_providers.aws block to use the new provider source registry.terraform.io/hashicorp/aws.
Terraform
Need a hint?

Change the source value in the required_providers.aws block to the new provider source string.