Practicing Terraform CLI Commands (fmt, taint, and import)

45 minutes
  • 5 Learning Objectives

About this Hands-on Lab

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
  1. Clone the code required for the lab from the provided GitHub repository.
  2. Switch to the appropriate directory where the code required for this lab resides.
Use the ‘fmt’ Command to Format Any Unformatted Code Before Deployment
  1. Examine the code in the file.
  2. Format the code using the terraform fmt command.
  3. View the changes that were made to the code in the file.
  4. Initialize the Terraform working directory and fetch any required providers with the terraform init command.
  5. Deploy the code with the terraform apply command.
Use the ‘taint’ Command to Replace a Resource
  1. Modify the provisioner in the file to change the webpage output for the aws_instance.webserver resource to Version 2.
  2. Taint the aws_instance.webserver resource using the terraform taint command.
  3. Deploy the code again to recreate and replace the tainted resource.
  4. Verify that the changes display on the webpage, validating that the resource was replaced.
Use the ‘import’ Command to Import a Resource
  1. View the contents of the resource_ids.txt file to obtain the EC2 instance ID of the virtual machine you need to import.
  2. Create the associated aws_instance.webserver2 resource in the file.
  3. Import the resource to be managed by your Terraform configuration using the terraform import command.
  4. Verify that the resource was added and is now being tracked by the state file.
Modify the Imported Resource
  1. Modify the imported aws_instance.webserver2 resource to customize its parameters.
  2. Deploy the code to implement the changes to the resource.
  3. Tear down the infrastructure using the terraform destroy command.

Additional Resources

For the purposes of this lab, an existing EC2 instance has already been provisioned in your AWS account, but it is not being managed by your Terraform configuration. Its EC2 instance ID has been saved to the resource_ids.txt file on the lab server.

Link to the GitHub respository containing Terraform code for this lab:

Before starting work on the lab, please give the lab an extra moment to make sure it is fully provisioned. To get started, log in to the lab server using the credentials provided:

ssh cloud_user@<Terraform-Controller>

What are Hands-on Labs

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!

Get Started
Who’s going to be learning?