Terraform -Resource lookup

Use case:

You need to look up something in your statefile to confirm a resource setting and do not want to take the trouble to log into your AWS account. This assumes you have the access you need to access the statefile from your local machine.


If you run…

terraform state show

You will see this:

Multiple instances found for the given pattern!
This command requires that the pattern match exactly one instance
of a resource. To view the matched instances, use "terraform state list".
Please modify the pattern to match only a single instance.

Suppose we are looking for a particular setting in a launch configuration. We would do something like this:

terraform state list | grep launch_configuration

We might see a result that resembles something like this:


From here we would run the following and the results would look similar to this:

terraform state show module.pritunl-vpn.aws_launch_configuration.pritunl_launch_config

id                = pritunl-lc-20190001
associate_public_ip_address         = true
ebs_block_device.#           = 0
ebs_optimized                = false
enable_monitoring            = true
ephemeral_block_device.#     = 0
iam_instance_profile         = pritunl_instance_profile
image_id                     = ami-833XXX92725nck73c
instance_type                = t2.medium
key_name                     = pritunl
name               = pritunl-lc-20190001
name_prefix         = pritunl-lc-
root_block_device.#      = 1
root_block_device.0.delete_on_termination      = true
root_block_device.0.iops       = 0
root_block_device.0.volume_size        = 20
root_block_device.0.volume_type       = standard
security_groups.#                 = 1
security_groups.2564468536                = sg-3XXX92725nc9f9fd72ab
spot_price                                =
user_data                                 = dac1X92725nc9f9fdc3fabad3b717a6648
vpc_classic_link_id                       =
vpc_classic_link_security_groups.#        = 0


From here looking at these results we can see what setting we needed to look up with just a few commands rather than having to take the time to log into our AWS account and click around for what we need.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s