Terraform is an IAC tool that enables developers or operations teams to manage automatically, monitor, and provision resources through code instead of doing it manually.

In this blog, we’ll look at:

  • What is Terraform
  • Getting started steps for Terraform
  • Terraform Architecture and Commands
  • Configuration File and Terraform State

Terraform workflow lets users define and provision their infrastructure and resources in the cloud from simple declarative code. When building an app in the cloud, you may find yourself using the graphical user interface of your favorite cloud provider. Terraform allows you to automate and manage your infrastructure and services. It uses a declarative approach, meaning you don't have to write a bash script for every step in your configuration.

Terraform makes it easy to create a Dev environment that replicates your Staging environment where you can easily test changes and features before you push it into production. For example, if you want to set up an infrastructure from scratch for your application, your application comprises a frontend container and a database container. With Terraform, you can quickly provision and prepare your packages and workloads from your app for application deployment, like creating your ECS container, VPC, EC2 nodes, etc.

Terraform Architecture

Terraform architecture comprises two main components that make up its Architecture:

  1. Terraform Core: The Terraform core uses two input sources to perform its operation. It takes and keeps the Terraform configuration you created. The second input source is the Terraform state, where Terraform stores the up-to-date state of your current infrastructure setup. The Core takes these inputs and plans what needs to be done, like comparing your current state with your desired state, resources to be created, deleted, etc.
  2. Providers: The second component in the Terraform architecture are providers like: AWS, Azure, and Google cloud for infrastructure-level tasks. Terraform also has providers for Kubernetes, Azure Active Directory, DNS, and Azure Stack. You can build and create projects on your existing stack at all levels with these providers. When these processes are defined, Terraform core will create a completion plan based on the input from the config file and state; it then uses the providers to execute the plan and deploy your changes wherever you want.

Terraform Configuration and State Files

We’ll be looking at how to version control changes from your infrastructure. In Terraform, you can store the state of your resources using state files so they can:

  • Detect and recognize intended code changes.
  • Identify infrastructure drift as a result of manual intervention.

State files are stored depending on how the backend configuration is set up in your terraform code.  The state files will be stored locally if you don’t have any backend configuration.

DEMO

  1. Create or log in to your Terraform Cloud Account

2. Create a new organization by inputting the name and email address.

3. Next, go to user settings and generate a token for your resources. Copy the generated token it creates somewhere safe and click Done.

4. Next, we’ll create two S3 buckets using Terraform, store the state on Terraform cloud and deploy the resources directly to AWS.

5. Create your main.tf file and configure the required providers. Here we’ll be using the aws provider. You can use any provider you want.

terraform {
 required_providers {
   aws = {
     source  = "hashicorp/aws"
     version = "~> 3.27"
   }
 }
 
}

6. Next, create your S3 bucket.  We are creating an S3 bucket for the production and staging environment in this configuration.

resource "aws_s3_bucket" "workflow-staging" {
 bucket = "workflow-staging"
 
 tags = {
   Name      = "workflow-staging"
   Project   = "staging"
   Terraform = "True"
 
 }
 
 
}
 
 
resource "aws_s3_bucket" "workflow-production" {
 bucket = "workflow-production"
 
 tags = {
   Name      = "workflow-production"
   Project   = "production"
   Terraform = "True"
 
 }
 
 
}

7. Create your backend.tf  configurations.  In the backend.tf configurations, we specify the hostname, the name of your organization, and the workspaces name.

terraform {
 backend "remote" {
   hostname     = "app.terraform.io"
   organization = "machala"
 
   workspaces {
     name = "backend-state"
   }
 }
}

8. Run terraform init to initialize your backend remote and provider plugins.

  • Back in your Workspaces UI, you will see your Terraform workspace created.

9. Determine your desired state for your Terraform configuration and compare it to the real infrastructure object using terraform plan

10. Deploy and create your infrastructure depending on the configuration files using terraform apply

  • You’ll see your changes have been deployed.

Back in your Terraform Workspace, you’ll see the details and logs of the changes on how your infrastructure was deployed.

11. Back in your Workspace, Select States; you’ll see your Terraform states that match your deployment.  Terraform state stores information about your infrastructure in a state file. This state file keeps track of resources created by your configuration and maps them to real-world resources.

12. Next, log in to your AWS S3 console, and see your S3 buckets created for usage.


I'd like to build something with Terraform + CTO.ai!

Heck yeah! Here's a starter application to try this out yourself:

Good luck!