Develop configuration with the console
The Terraform console is an interpreter that you can use to evaluate Terraform expressions and explore your Terraform project's state. The console helps you develop and debug your configuration, especially when working with complex state data and Terraform expressions.
The Terraform console command does not modify your state, configuration files, or resources. It provides a safe way to interactively inspect your existing project's state and evaluate Terraform expressions before incorporating them into your configuration.
In this tutorial, you will deploy an S3 bucket to AWS. Then, you will use the console to inspect your bucket's state. Finally, you will add an IAM policy to your bucket, using the console to help develop the configuration.
Prerequisites
You can complete this tutorial using the same workflow with either Terraform Community Edition or HCP Terraform. HCP Terraform is a platform that you can use to manage and execute your Terraform projects. It includes features like remote state and execution, structured plan output, workspace resource summaries, and more.
Select the HCP Terraform tab to complete this tutorial using HCP Terraform.
This tutorial assumes that you are familiar with the Terraform workflow. If you are new to Terraform, complete the Get Started tutorials first.
In order to complete this tutorial, you will need the following:
- Terraform v1.1+ installed locally.
- An AWS account with local credentials configured for use with Terraform.
- The AWS CLI (2.0+) installed, and configured for your AWS account.
Note
Some of the infrastructure in this tutorial may not qualify for the AWS free tier. Destroy the infrastructure at the end of the guide to avoid unnecessary charges. We are not responsible for any charges that you incur.
Clone example configuration
Clone the example repository for this tutorial, which contains configuration for you to use to learn how to work with the Terraform console.
Change to the repository directory.
Review configuration
Review the configuration in main.tf
. After configuring the AWS provider, it
defines the S3 bucket you will use for this tutorial.
The configuration defines the bucket prefix with the bucket_prefix
variable.
When you provision this configuration, Terraform will create a unique bucket
name starting with the prefix. The force_destroy
argument instructs Terraform
to delete the bucket contents when you destroy it. The
aws_s3_bucket_public_access_block.data
resource allows you to configure the
bucket to be publicly accessible. Finally, the configuration sets a
public-read
ACL, using the depends_on
meta-argument to ensure the ACL is
configured after the access block.
Create S3 bucket
Initialize this configuration.
Apply the configuration to create your S3 bucket. Respond to the confirmation
prompt with a yes
.
Explore Terraform state
Terraform's console loads your project's state and allows you to interactively evaluate Terraform expressions before using them in your configuration. Launch the console now.
Note
The Terraform console uses a >
prompt, which is not displayed in
the code blocks below.
Get the state of the aws_s3_bucket.data
resource by pasting its resource ID
into the console prompt.
The console will print out the state of the aws_s3_bucket.data
resource.
Add structured output
In this section, you will create an output value to describe your bucket, and convert it to JSON. Output values enable you to provide data about your Terraform projects to other parts of your infrastructure automation toolchain. To facilitate this, Terraform can print output values in JSON, which is machine-readable.
Systems you integrate with may expect a specific JSON data structure. Use the console to verify that the JSON created matches the required format before you add the output value to your configuration.
First, use the console to create a map that includes your S3 bucket's ARN, ID,
and region, and then encode it as JSON with the jsonencode()
function.
The Terraform console will print out the values of the map you created as a JSON
string. Since the console returned the JSON as a string value, it escaped the
"
characters with the \
prefix.
This JSON matches the intended structure, so add the following to outputs.tf
to define an output value using this map.
The Terraform console locks your project's state file, so you cannot plan or
apply changes while the console is running. Exit the console with <Ctrl-D>
or
exit
.
Apply the change and respond to the confirmation prompt with a yes
. Terraform
will now display your new output value.
Output the bucket details as JSON.
When you include the -json
flag in your Terraform output commands, Terraform
converts maps and lists to the equivalent JSON data structures.
Set bucket policy
Bucket policies allow you to control access to your S3 buckets and their contents.
In this section, you will apply a policy to your bucket that allows public read access to the objects in the bucket.
Add bucket policy
Add a policy to your bucket. The file bucket_policy.json
in the example
repository contains a policy based on an example from
AWS.
AWS policies are defined as JSON documents. As a result, the
aws_bucket_policy
resource expects policies as a JSON string. Using HCL to dynamically generate
the policy JSON string enables you to leverage HCL's benefits, such as syntax
checking and string interpolation.
Use the Terraform console to convert the policy document to HCL before you
incorporate it into your configuration. Use echo
to pass the command to the
console.
The file()
function loads the file's
content into a string, and
jsondecode()
converts the string
from JSON to an HCL map.
Add the following policy resource based on the previous output to main.tf
.
The aws_s3_bucket_policy.public_read
resource configures a policy for your
bucket based on the policy defined in bucket_policy.json
. It replaces the
<BUCKET_ARN>
placeholder with a reference to your bucket's ARN. Finally, it
uses the jsondecode()
function to convert the policy back into JSON for use by
AWS.
Now, apply this configuration. Respond to the confirmation prompt with a yes
to update your bucket policy.
Clean up your infrastructure
Remove the infrastructure you created during this tutorial. Respond to the
confirmation prompt with a yes
.
If you used HCP Terraform for this tutorial, after destroying your resources,
delete the learn-terraform-console
workspace from your HCP Terraform
organization.
Next steps
In this tutorial, you used the Terraform console to explore your state and evaluate Terraform expressions before using them in your configuration. The Terraform console can help you develop and troubleshoot your Terraform configurations.
Try out the following resources to learn more about creating Terraform configuration.
Follow the Terraform State tutorials to learn more about how to manage Terraform state.
Learn how to use and create Terraform modules.
Learn how to target changes to specific resources.