Difficulty: Beginner
Estimated Time: 30 minutes

Organize Terraform Configuration

In this workshop, 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

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.

Configure AWS Provider

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

provider "aws" {
  region = var.aws_region
}

Terraform will use IAM credentials to authenticate with your AWS account. To do so, you will need to export two environment variables in the terminal window.

$ export AWS_ACCESS_KEY_ID=AKIAIOSFODNN7EXAMPLE
$ export AWS_SECRET_ACCESS_KEY=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY

You will need to replace the values for the access key ID and secret access key values with credentials configured with the correct IAM policy.

You can review assets/policy.json for an example of an appropriate IAM policy for the actions you will take while following this scenario.

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 with 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.

This tab will not be visible to users and provides only information to help authors when creating content.

Creating Katacoda Scenarios

Thanks for creating Katacoda scenarios. This tab is designed to help you as an author have quick access the information you need when creating scenarios.

Here are some useful links to get you started.

Running Katacoda Workshops

If you are planning to use Katacoda for workshops, please contact [email protected] to arrange capacity.

Debugging Scenarios

Below is the response from any background scripts run or files uploaded. This stream can aid debugging scenarios.

If you still need assistance, please contact [email protected]