Inspecting Infrastructure

Sign Up to Build

About this Architecture

Here is some information about this architecture.

How to Build This Solution

Here are the steps you can follow to build this solution on your own.

As we’ve learned in the previous module, Terraform saves its state in a structured file. This file is saved on a local computer by default, but can be saved remotely.

There are cases where you may need to inspect state. Perhaps you need to get a certain value of a deployed resource, or you may need to see how state will look under a proposed change.

Terraform saves the state file in the JSON format. You could open this file and inspect it directly. However, that is not recommended because you’re more likely to make a mistake with it.

Instead, you should use the powerful Terraform CLI! It has a range of commands you can use to learn more about Terraform state. Specifically, the CLI comes with these commands to aid in state inspection:

  • terraform graph - can create a visual representation of current state or planned changes.

  • terraform output - gets the top-level output values of a project.

  • terraform show - creates human-readable or machine-readable versions of the state file.

  • terraform state list - creates a list of project resources.

  • terraform state show - generates a list of resources and attributes.

In this lab you will get experience using the CLI to inspect Terraform state.

Lab Time!

In this lab, you will learn how to inspect state on a system that has resources deployed already.

Get Your AWS Credentials

If you're using the Skillmix Labs feature, open the lab settings (the beaker icon) on the right side of the code editor. Then, click the Start Lab button to start hte lab environment.

Wait for the credentials to load. Then run this in the terminal.

Be sure to enter in your own access key and secret key and name your profile 'smx-lab'.

$ aws configure --profile smx-lab
AWS Access Key ID [None]: 
AWS Secret Access Key [None]: 
Default region name [None]: us-west-2
Default output format [None]: 

Note: If you're using your own AWS account you'll need to ensure that you've created and configured a named AWS CLI profile named smx-lab.

Create a Working Directory

Let’s start off by creating a working directory and the files that we’ll need to use. Open your terminal or command prompt and create a working directory.

# create the directory
$ mkdir inspect-state-lab
$ cd inspect-state-lab

Download & Install the Project

We will use an existing project for this lab. Download it from Github into your working directory, and then apply the project.

$ git clone https://github.com/tabdon/terraform_module_2_challenge.git .

$ terraform init

...output

$ terraform plan

...output

$ terrafrom apply

...output

Now that you have the project installed, let’s get experience working with the CLI.

The Graph Command

The terraform graph command will generate a representation of the project in the DOT format. This output can be used by tools like Graphviz to generate a visual representation of the project.

To create the visualization shown you need to install Graphviz (install page).

The graph command can show output for different Terraform operations. By default, it shows the output for the plan operation. You can use the type= argument to change this behavior.

# generate output for the terminal window
$ terraform graph 

...output

# generate a Graphviz .svg file (requires Graphviz to be installed)
$ terraform graph | dot -Tsvg > graph.svg

# generate output of the apply command
$ terraform graph type=apply

The Output Command

The terraform output command is used to display output values from the state file. If your project includes any output blocks, then they can be displayed using this command.

If you don’t specify an output value name, all output values will be displayed.

# show all output values
$ terraform output

# show a specific output value
$ terraform output instance

The Show Command

The terraform show command will generate human-readable output for the current state, or a plan file. You can use this to inspect current state, or see how a change will be implemented.

This command will generate output in the JSON format.

# show the current state
$ terraform show

The State List Command

The terraform state list list command will generate a list of resources in the project. This list will be organized by module depth and then alphabetical.

# generate a list of resource addresses 
$ terraform state list

# filter by module
$ terraform state list module.networking

The State Show Command

The terraform state show command will output the details of a specific resource. You must supply the resource address to this command.

# show the details of a specific resource
$ terraform state show aws_instance.web_server

# show the details of module resource
$ terraform state show module.networking.aws_vpc.my_vpc

Destroy

Great work! You’ve completed the lab. Now it’s time to destroy it.

$ terraform destroy