Troubleshooting Terraform: Use Terraform Modules To Address Common Issues

In this short tutorial, I will show you how to use Terraform modules to troubleshoot common issues when building AWS infrastructure. Specifically, we’ll address leap and bounds issues to ensure a smoother and more efficient deployment process.

Background

Terraform is like a magic wand for managing your cloud infrastructure. Imagine writing a few lines of code, and voilà, your servers, databases, and networks are all set up automatically. That’s the power of Terraform.

Terrform module is a container for multiple resources that are used together. This can include configuration files, input variables, output values, and nested resources. Modules allow you to abstract your infrastructure into reusable components.

Prerequisite

For this project, you need:

  • AWS account
  • Terraform installed

Project Outline

When working with Terraform, you might encounter some challenges that can seem like they appear out of nowhere, growing by “leaps and bounds.” As a cloud engineer, this is one way to resolve these issues: the use of modules.

In our example, you notice that your team has built AWS infrastructure (EC2, VPC, S3, EBS, etc.) using Terraform. However, it seems that the EC2 instance is not being created when using the same Terraform template again. In the following tutorial, I will show you how to fix this issue and properly implement AWS EC2.

Let’s Have Fun

Instead of writing all the code in the main configuration file, I will use modules to separate the EC2 configuration, troubleshoot issues, and keep it ready for future implementation.

Verify if terraform and AWS are installed properly

  • Open the terminal and type terraform --version
  • Verify if AWS is configured properly. Type aws --version

Detach the EC2 configuration

  • Detach the EC2 configuration from the main Terraform template.
  • Create a new folder for your troubleshooting session.
  • Inside the new folder, create a file named main.tf.
  • Open main.tf and add the EC2 resource configuration.
  • Add the provider definitions at the top of the main.tf file.
  • Create variables to simplify the code.

If you leave the code as it is, you will encounter an error during execution. To fix this, create a new file named terraform.tfvars to define the values for ami and instance_type. Also,terraform.tfvars can be used to stored sensitive information.

Add some changes to our terraform code

  • Update our Terraform code to use these variables. Creating a terraform.tfvars file ensures that any developers in the project can easily use and update it when needed to create new AWS EC2 resources.

Create terraform variables

  • Create a new file named variables.tf to simplify and parameterize the code.
  • Go back to main.tf, copy and cut all the variable definitions.
  • Open variables.tf and paste the variable definitions into variables.tf.

Create the new EC2 resource

  • Go to the terraform project and run terraform init
  • Then terraform plan. Finally terraform apply. Type Yes.

Add more changes for a robust code

Now, let’s imagine that the code you are writing will be used by developers outside of your team. For that reason, I will add output and input.

  • Run terraform destroy
  • Create a new file named outputs.tf
  • Add the following lines to outputs file:
  • Run terraform init, terraform planand terraform apply

Create modules for EC2

To improve reusability and maintainability, we will create modules for EC2. This will be especially helpful when creating EC2 instances multiple times. By doing so, it resolves our issues.

  • Run terraform destroy.
  • Create a new folder called modules/ec2. This folder will contain all the necessary files for the EC2 module.
  • Move the following files main.tf, variables.tf, terraform.tfvars and outputs.tf in the folder.
  • After moving these files, delete terraform.tfvars. Other developers executing the module will create their own terraform.tfvars.

At this stage, any developer or team member can create a new main.tf file with the provider defined, from any location or repository, and execute the module to implement a new EC2 instance. Here’s how it will look:

  • Finally, execute terraform plan and terraform apply to deploy the resources.
  • Any developer can write a main.tf file, call the module they need, and add the necessary values to execute the code.

Voila! You’ve troubleshot the Terraform issue using a Terraform module. This approach also makes it easy to create reusable code for your Terraform automation.

 

Leave a Reply

Your email address will not be published. Required fields are marked *