In this hands-on lab, you’ll be working with the Terraform fmt, taint, and import commands and understand what scenarios to use them in.
Learning Objectives
Successfully complete this lab by achieving the following learning objectives:
- Clone Terraform Code and Switch to the Proper Directory
- Clone the code required for the lab from the provided GitHub repository.
- Switch to the appropriate directory where the code required for this lab resides.
- Use the ‘fmt’ Command to Format Any Unformatted Code Before Deployment
- Examine the code in the
main.tf
file. - Format the code using the
terraform fmt
command. - View the changes that were made to the code in the
main.tf
file. - Initialize the Terraform working directory and fetch any required providers with the
terraform init
command. - Deploy the code with the
terraform apply
command.
- Examine the code in the
- Use the ‘taint’ Command to Replace a Resource
- Modify the provisioner in the
main.tf
file to change the webpage output for theaws_instance.webserver
resource toVersion 2
. - Taint the
aws_instance.webserver
resource using theterraform taint
command. - Deploy the code again to recreate and replace the tainted resource.
- Verify that the changes display on the webpage, validating that the resource was replaced.
- Modify the provisioner in the
- Use the ‘import’ Command to Import a Resource
- View the contents of the
resource_ids.txt
file to obtain the EC2 instance ID of the virtual machine you need to import. - Create the associated
aws_instance.webserver2
resource in themain.tf
file. - Import the resource to be managed by your Terraform configuration using the
terraform import
command. - Verify that the resource was added and is now being tracked by the state file.
- View the contents of the
- Modify the Imported Resource
- Modify the imported
aws_instance.webserver2
resource to customize its parameters. - Deploy the code to implement the changes to the resource.
- Tear down the infrastructure using the
terraform destroy
command.
- Modify the imported