Terraform Bootcamp Week 0 with Andrew Brown: Basic Definitions
Tags: terraform, aws, iac, ansible
Published: 2023-11-10 16:43:45

Hi guys. Welcome. I recently decided to learn Terraform as I upskill for my first tech role. This article is part of my Terraform journey with [Terraform Bootcamp](https://www.youtube.com/watch?v=TXjzwiHtGqc&t=5852s) by Andrew Brown and Andrew Bayko, together with Chris Williams(I am also using his resources that he published [here](https://www.freecodecamp.org/news/terraform-certified-associate-003-study-notes/#preparation-materials) and the beloved Shala Warner. I am also using some other resources from [Aaron Brooks](https://www.youtube.com/@mastermnd) so that I can better explain new terms. And a special shout out to [Gwen Leigh](https://medium.com/@gwenleigh/terraform-cloud-project-bootcamp-with-andrew-brown-complete-learning-journal-3640cebc74b5) for such a helpful outline that I used as a guide to create this series so that the documentation is easy to read!

As I learn more about Terraform, feel free to follow [Andrew Brown on Youtube](https://www.youtube.com/@ExamProChannel) and see their free (and paid) content . I Now let's jump in;

# Table of Contents
- [Basic Definitions](#basic-definitions)
   * [What is Terraform?](#what-is-terraform)
   * [What is IaC?](#what-is-iac)
   * [Types of infrastructure](#types-of-infrastructure)
   * [Why use IAC?](#why-use-iac)
   * [What is Configuration Management?](#what-is-configuration-management)
      + [Configuration Management vs Infrastructure Provisioning?](#configuration-management-vs-infrastructure-provisioning)
   * [What is Hashicorp Vault?](#what-is-hashicorp-vault)
   * [Terraform Infrastructure Lifecycle](#terraform-infrastructure-lifecycle)
      + [Idempotency.](#idempotency)
      + [Configuration Drift:](#configuration-drift)
         - [Mutable vs Immutable infrastructure](#mutable-vs-immutable-infrastructure)
   * [Terraform workflow](#terraform-workflow)
      + [1. Code ](#1-code)
      + [2. Terraform init](#2-terraform-init)
      + [3. Terraform plan](#3-terraform-plan)
      + [4. Terraform validate ](#4-terraform-validate)
      + [5. Terraform apply](#5-terraform-apply)
      + [6. Terraform destroy - unmakes the things! 😱](#6-terraform-destroy-unmakes-the-things-)
   * [Resources](#resources)

# Basic Definitions

## What is Terraform?
- Terraform is an infrastructure as code tool that lets you build, change, and version cloud and on-prem resources safely and efficiently.

- HashiCorp Terraform is an infrastructure as code tool that lets you define both cloud and on-prem resources in human-readable configuration files that you can version, reuse, and share. You can then use a consistent workflow to provision and manage all of your infrastructure throughout its lifecycle. <sup>1</sup>


## What is IaC?
- Infrastructure as Code (IaC) is the blueprint of our infrastructure that allows managing and provisioning of infrastructure through code instead of through manual processes. This is because provisioning infrastructure at scale is prone to error. It allows us to **provision the SAME INFRASTRUCTURE everytime.**

With IaC, configuration files are created that contain your infrastructure specifications, which makes it easier to edit and distribute configurations. It also ensures that you provision the same environment every time. By codifying and documenting your configuration specifications, IaC aids configuration management and helps you to avoid undocumented, ad-hoc configuration changes.

Version control is an important part of IaC, and your configuration files should be under source control just like any other software source code file. Deploying your infrastructure as code also means that you can divide your infrastructure into modular components that can then be combined in different ways through automation. <sup>2</sup>

## Types of infrastructure
We have 2 types of infrastructure:

- Declarative 
- Imperative

**1. Declarative** 
What you see is what you get. It's explicit with 0 chance of misconfiguration:

Azure only -> ARM Templates, Azure Blueprints
AWS only -> CloudFormation
GCP only -> Cloud Deployment Manager
All of the above (& many others) -> Terraform

**2. Imperative**
Uses existing programming languages like Python, JS or Ruby:

AWS only -> AWS CDK
AWS, Azure, GCP, K8s -> Pulumi
Terraform supports For loops, dynamic blocks, complex data structures – so it's declarative with some imperative benefits.

## Why use IAC?

1. Creates a consistent and reusable ways to deploy and manage infrastructure and configuration.

2. Help prevent configuration drift.

3. Provides free documentation i.e code and logs of current state.

4. Share & reuse your configurations more easily

5. Manage infra on multiple cloud platforms

6. Track resource changes

7. Use version control (Git, GitHub, etc..) to collaborate with team members

## What is Configuration Management?
- The primary goal of configuration management tools is to configure the server. Meaning, if you want to automate the installation and configuration of an application(e.g., Nginx) in a server, we use a **tool like Ansible and Chef**. It does all the configurations in an **idempotent manner**. It can be used to maintain computer systems, servers, applications, network devices, and other IT components in a desired state. 

- They allow administrators to set up an IT system, such as a server or workstation, then build and maintain other servers and workstations with the same settings. IT teams use configuration assessments and drift analyses to continuously identify systems that have strayed from the desired system state and need to be updated, reconfigured, or patched. It’s a way to help ensure that a system performs as expected, even after many changes are made over time. <sup>3</sup>

- Also, these tools help in managing the **configuration drift**. _It ensures all the servers are running in the same configuration mentioned in the ansible-playbook or a chef cookbook_ .In the case of an agent-based chef/puppet, if someone changes the server config manually, the chef agent brings it back to the desired state, as mentioned in the cookbook.

- All the configuration management tools keep an inventory of the server’s IP address and SSH credentials to connect to the servers. However, in cloud environments where servers are dynamically provisioned, it uses an API-based dynamic inventory to get the server details.

### Configuration Management vs Infrastructure Provisioning?
- Infrastructure provisioning tool Terraform is responsible for providing the network and servers
- Configuration management tool Ansible configures applications inside servers provisioned by Terraform.<sup>4</sup>


![Configuration Management vs Infrastructure Management](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5vzo6yslow2zwgmqh0yf.png)

## What is Hashicorp Vault?
- HashiCorp Vault is an identity-based secrets and encryption management system. A secret is anything that you want to tightly control access to, such as API encryption keys, passwords, and certificates. Vault provides encryption services that are gated by authentication and authorization methods. Using Vault’s UI, CLI, or HTTP API, access to secrets and other sensitive data can be securely stored and managed, tightly controlled (restricted), and auditable.

## Terraform Infrastructure Lifecycle

The Infrastructure Lifecycle is having clearly defined work phases for planning, designing, building, testing, maintaining and retiring your infrastructure.

### Idempotency.

- a property of some operations such that no matter how many times you execute them, you achieve the same result. Terraform is idempotent because, no matter how many times you run the same configuration file, you will end up with the same expected state.

### Configuration Drift:

- When your actual infrastructure deviates away from the desired infrastructure. Unexpected configuration change away from what is stated in the config file. Can be due to manual adjustment (console access in prod = BAD  😂), evil h@xx0rs, etc. that is done outside of the scripting tools i.e Hashicorp Terraform, AWS CloudFormation, Ansible

.. How do we fix it?

**Detect:**
- By using configuration monitoring tools/compliance tools like AWS Config, or built-in support e.g. AWS CF Drift Detect
- They monitor your infrastructure and notify based on default and custom rules.
>PS: Most of these tools are not free

**Correct:**

- TF refresh & plan commands
- Manually correct (try not to do this)
- Reprovision (comes with it's own risks)

**Prevent:**

- use immutable infrastructure
- always create & destroy, never reuse
- never share credentials
- run scheduled plans with tools like AWS Config  and use Terraform Import Blocks to import resources outside your Terraform State
- use drift detection tools like _driftctl_ and _Cloudquery_
- use GitOps to version control IaC i.e
_Create tf file
commit
Pull Request
peer review
commit to main
GitHub action triggers build_

#### Mutable vs Immutable infrastructure
Think of mutable infrastructure as (1) building a base image (2) Deploying that base image then (3) configuring the software after deploy.

## Terraform workflow
Most of the main Terraform files end with the .tf extension.

### 1. Code 
+ create or edit your terraform config file

### 2. Terraform init
- This command initializes a working directory containing Terraform configuration files, pull providers and modules.
- This is the first command that should be run after writing a new Terraform configuration or cloning an existing one from version control. It is safe to run this command multiple times.
```tf
Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see  
any changes that are required for your infrastructure. All Terraform commands  
should now work.

If you ever set or change modules or backend configuration for Terraform,      
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.
```

### 3. Terraform plan
- Moves items from current state to the configuration state described in the `main.tf` file.
- It creates an execution plan, which lets you preview the changes that Terraform plans to make to your infrastructure. By default, when Terraform creates a plan it:

1. Reads the current state of any already-existing remote objects to make sure that the Terraform state is up-to-date.
2. Compares the current configuration to the prior state and noting any differences.
3. Proposes a set of change actions that should, if applied, make the remote objects match the configuration.

### 4. Terraform validate 
Ensure types, values, and required attributes are valid and present

### 5. Terraform apply
After making our plan we can now execute it.

### 6. Terraform destroy - unmakes the things! 😱


## Resources
1. [What is Terraform](https://developer.hashicorp.com/terraform/intro) <sup>1</sup>
2. [What is IaC?](https://www.redhat.com/en/topics/automation/what-is-infrastructure-as-code-iac) <sup>2</sup>
3. [What is Configuration Management?](https://www.redhat.com/en/topics/automation/what-is-configuration-management)<sup>3</sup>
4. [Configuration Management vs Infrastructure Management](https://devopscube.com/infrastructure-as-code-configuration-management/)<sup>4</sup>
5. [A great read too](https://www.thoughtworks.com/insights/blog/why-configuration-management-and-provisioning-are-different)
6. [What is Hashicorp Vault?](https://developer.hashicorp.com/vault/docs/what-is-vault)
7. [What is Terraform init](https://developer.hashicorp.com/terraform/cli/commands/init)
8. [Creating Git branches](https://www.theserverside.com/blog/Coffee-Talk-Java-News-Stories-and-Opinions/git-branch-change-example-tutorial-github-gitlab-bitbucket)
9. [Terraform Certified Associate (003) by Chris Williams](https://www.freecodecamp.org/news/terraform-certified-associate-003-study-notes/#preparation-materials)
10. [What is Configuration Drift?](https://morethancertified.com/cloud-infrastructure-drift/)