Difficulty: Beginner
Estimated Time: 30 minutes

Organize Terraform Configuration

In this tutorial, you will organize Terraform configuration by starting with an example monolithic project that manages static websites hosted in AWS S3, and refactoring it using four strategies:

  1. Split the configuration into multiple files
  2. Separate state with Terraform Workspaces
  3. Separate the configuration into separate project directories
  4. Refactor and simplify the configuration using a module

This environment has been pre-configured with the Terraform CLI, AWS CLI and localstack, a mocking framework that mocks core AWS APIs locally. As a result, you do not need an AWS account to complete this scenario.

Congratulations!

You've reached the end of the scenario!

If you'd like to learn more, try one of these resources on HashiCorp Learn:

Organize Terraform Configuration

Step 1 of 5

Create Terraform Configuration

In this step, you will create two S3 buckets configured for static website hosting using a monolithic configuration.

Review AWS provider

Open main.tf. Your configuration begins with the AWS provider block below.

provider "aws" {
  region = var.aws_region
  # ...
}

It also includes additional settings that allow this scenario to use localstack to simulate infrastructure, instead of requiring you to have an AWS account. Refer to the code in the Learn tutorial to build real infrastructure, which will require you to authenticate with AWS.

Refer to the AWS Get Started collection or the AWS Provider Registry page to learn different ways to authenticate the AWS provider.

Review monolithic configuration

Now that you have configured the AWS provider, review the rest of the configuration found in main.tf. This configuration will create two S3 buckets, each configured to host a static website, and each containing a single index.html. One bucket will be for your dev environment, and the other for prod.

Notice the three variables used in your configuration. Open variables.tf and notice how these variables are declared with default values.

Now open outputs.tf, which will output the website endpoints for the two buckets once Terraform creates them. Later you will use these values to visit the website and verify that your configuration was successfully deployed.

Apply configuration

Initialize your Terraform workspace.

terraform init

Terraform will install two providers - one for AWS, and one for the random_pet resource.

Now apply this configuration.

terraform apply

Respond with yes when prompted.

Verify the website endpoint URLs by copying them from the embedded terminal and pasting them into your web browser.

Note: If your browser window is wide enough the link may only span one line, in which case it will be clickable from the embedded terminal.

In the next step, you will begin to organize this configuration by separating your development and production configuration.