Skip to content

Fundamentals

Dom Clayton edited this page Jan 5, 2023 · 12 revisions

Terraform Commands

Init

Initialise a working directory be that new code or cloned code from version control e.g. Github. Also downloads the providers and creates a .terraform.lock.hcl, this file should be included in version control.

NOTE: You can show verbose logging by setting the TF_LOG flag

PowerShell

$env:TF_LOG="TRACE"

Bash

export TF_LOG="TRACE"
terraform init

Validate

Validate syntax of Terraform configuration files and ensures the configuration is consistent.

terraform validate

Plan

Dry run, what is Terrform code going to create, delete, or modify.

terraform -chdir='.\some path to the terraform root directory\' plan

terraform plan

Plan -out

Output a deployment plan for later use e.g.

terraform -chdir='.\some path to the terraform root directory\' plan -out planName -var-file='.\test.tfvars' -var='subscriptionId=000...'

terraform plan -out planName

Terraform Plan

Plan -state

Use a state file outside of the root directory e.g.

terraform -chdir='.\some path to the terraform root directory\' plan -out 'Env/planName' -var-file='.\test.tfvars' -var='subscriptionId=000...' -state='Env/terraform.tfstate'

Plan -destroy

Dry run of destroy

terraform plan -destroy

You would them use Terraform apply 'plan' as per this example to destroy the resources.

Apply

Applies changes to ensure the a 'desired state'.

terraform apply

Apply {Plan Name}

terraform apply deployment_infra_date

Apply -state {Plan Name}

terraform apply -state='Env/terraform.tfstate' 'Env/planName'

Apply -target={Resource Name}

Apply -var variableName={variableValue}

Destroy

Remove the resources created by Terraform.

Blocks

Terraform

The Terraform settings block for your root module...

The backend block within the Terraform settings block can be used to define where the state file should be located...

terraform {
    backend "azurerm" {
        storage_account_name = "storage_account_name"
        container_name       = "container"
        key                  = "someting.tfstate"
        access_key           = "storage_account_access_key"
        sas_token            = "storage_account_sas_token"
    }
}

The values above can be passed using backend.tfvars and environment variables. For example the storage_account_name, container_name and key could be stored witin backend.tfvars - this will be referenced during terraform init using the -backend-config flag. The access_key or sas_token should be kept secret and would most likely be initialised at runtime using some form of wrapper script for example...

Param
(
    [Parameter(Mandatory)]
    [string]$storage_account_name
)
...
$ACCESS_KEY = Get-AzStorageAccountKey -ResourceGroupName (Get-AzResource -Name $storage_account_name).ResourceGroupName -Name $storage_account_name | Where-Object {$_.KeyName -eq "key1"}
...
$env:ARM_ACCESS_KEY=($ACCESS_KEY).Value
...

To consume Terraform state in other root modules use the data source configuration, for example...

data "terraform_remote_state" "module_name" {
    backend = "azurerm"
    
    config = {
        storage_account_name = "storage_account_name"
        container_name       = "container_name"
        key                  = "module_name.tfstate"
    }
    
}

The access_key or sas_token would be accessed via an environment variable as detailed above.

Providers

Terraform relies on providers to manage resources; a provider is a logical abstraction of the upstream API e.g. Azure Resource Manager. Providers can be found here.

Resources

resource "resource_type" "resource_type_name" {
    argument_name = argument_value
}

// for example
resource "azurerm_resource_group" "my_resource_group" {
    name = "rg-1" // mandatory
    location = "northeurope" // mandatory
    tags = { // optional
        environment = "prod"
    }
}

Variables

Outputs

Locals

Meta-Arguments

Depends_On

Used to specify dependencies which Terraform cannot automatically resolve.

Count

Used to create multiple resources using an integer - like the copy element in ARM.

For_each

Used to create multiple resources using a map or set of strings.

Provider

Providers are defined within the terraform .tf file. These are downloaded by Terraform when you run terraform init. The providers are stored in a hidden .terraform directory within the working directory.

Lifecycle

Customisations for resource types...

Terraform types

Maps and Object Patterns and Usage