Terraform

Terraform is an infrastructure-as-code software tool, IaC is the process of managing and provisioning computer data center resources through machine-readable definition files
==Basics==
Terraform configuration file: Tells Terraform how to manage the infrastructure, Configuration files that use the HCL format end with the . tf file extension
configuration language known as HashiCorp Configuration Language (HCL), 
Blocks (of Code) / Ressource Block: Represents infrastructure you want to deploy, will contain resource type and name , name and location are arguments), to ensure right property values are in place when defining the resource, one block represents configuration of a ressource
*blocks have arguements, attributes, properties
Signature:
resource <resource-type> <name of block)> {
.....
}
Provider; Allows Terraform to work with an External provider, e.g. Azure, AWS, GCP
State: Terraform stores state about current managed infrastructure and configuration, the cloud stack's current instance, stored in file with "*.tfstate" ending
*interpretation: A tfplan file updates a tfstate file
===Dependency ===
Terraform doesn't perform blocks in any particular order -> dependency: 
Signature:
depends_on = {<ressource_type>:<ressource_name>}
===Variables ===
variables, has a name (outside bracket), a type, and a description
variable <variable name> {
description = "This is a variable of type string"
type = string
default = "Default string value for this variable"
}
reference it using var.<variable_name>
===Locals===
locals {
resource_group=<name>
location = <location>
}
reference: local.resource_group / local.location
=== Locals vs. Variables ===
Variable: visible globally, changable
locals: visible in local module, unchangable
==Execution (IPAD)==
===terraform init ===
initializes a working directory containing Terraform configuration files, prepares working directory for use with Terraformç
===terraform plan ===
creates an execution plan, which lets you preview the changes that Terraform plans to make to your infrastructure
plan file has ending "*.tfplan
=== terraform apply ===
executes the actions proposed in a Terraform plan to create, update, or destroy infrastructure
===terraform destroy===
destroy all remote objects managed by a particular Terraform configuration
destroy requires approval
==Azure==
===Pre-definition===
Provider (Terraform block)
Say the Azure Resource Manager is in place
Provider Definition (Provider block)
define provider, 
contains credentials: subscription id, client id, client secret (password for client), tenent id, subscription must be given permission to Terraform
provider "azurerm" {
 features {}
}
Ressource Group
ressource type: azurerm_resource_group
contains: name (of ressource group), and location
===Template===
Ressource type: 
Attributes:
===Storage Account===
Ressource type: azurerm_storage_account
Attributes: name, resource group, location, performance level, locally redundancy storage, account tier and replication type
===Blob Storage===
container is the folder which stores files, a blob is an individual file, storage account can have an indefinite number of containers (max 4 TB)
Ressource type: azurerm_storage_container
Attributes: name, storage account, replication type
Ressource type: azurerm_storage_blob
Attributes: name, storage account, storage container, type, source
===Public IP===
Ressource type: azurerm_public_ip
Attributes: standard (name, location, resource group), allocation_method
===Virtual Network===
Ressource type: azurerm_network_security_group
Attributes: standard (name, location, resource group)
Ressource type: azurerm_virtual_network
Attributes: standard (name, location, resource group), address space, dns servers, subnets (subnet {name = address_prefix = security group =}
Ressource type: azurerm_subnet
Attributes: name, resource group, virtual network (previously established), address_prefix, (exported) ID
Ressource type: azurerm_network_interface
Attributes: standard (name, location, resource group), ip configuration (name, subnet id, private_ip_address_allocation)
===Virtual Machine===
Ressource type: azurerm_windows_virtual_machine
Attributes: standard (name, location, resource group), size, admin_username, admin_password (only windows), network_interface_ids (previously established)
seperate blocks: os_disk, source_image_reference, admin ssh key (only linux)
===Functions===
Ressource type: azurerm_app_service_plan
Attributes: standard (name, location, resource group), sku (tier & size)
Ressource type: azurerm_function_app
Attributes: standard (name, location, resource group), app service plan (previously established), storage account name (previously established), storage account access key, app_settings & site config (for programming language), ostype & version (for platform)
===Storage Queue===
Ressource type: azurerm_storage_queue
Attributes: name & storage accoutn name (previously established)
===Service Bus===
Ressource type: azurerm_servicebus_namespace
Attributes: standard (name, location, resource group), sku
===Load Balancing===
requires public IP-Address for Load Balancer, private IP-Adresss for VMs in Virtual Network/corresponding subnets
*virtual machines need port for access and network assignment
pre-processes: requisites: Health Probes -> Backend Pool -> Load Balancing Rules
backend pool: contains several VMs the load can be balanced to
health probe: Backend VMs might not be working. the health probe helps examine whether or not a vm is working and load should be assigned to it
load balancing rules: by which logic should load be assigned to VM
Ressource type: azurerm_lb
Attributes: standard (name, location, resource group), frontend_ip_configuration (name & public ip address, previously established)
Ressource type: azurerm_lb_backend_address_pool
Attributes: name & load balancer id
Ressource type: azurerm_lb_backend_address_pool_address (one required for every vm)
Attributes:name, backend address pool id, virtual_network_id, ip_address
Ressource type: azurerm_lb_probe
Attributes: name, loadbalancer id, port
Ressource type: azurerm_lb_rule
Attributes: name, loadbalancer id, protocol, frontend port, backend port, frontend_ip_configuration_name
===Monitor===
create alert rules (e.g. when performance goes past particular value). set on ressource, metric (CPU percentage), if it's greater than treshhold, trigger alert
e.g.: If the CPU was utilized more than 90 percent over the last five minutes
actions: create action groups, notification type (how to notify), define what to do when alert is triggered, severity, alertrulename, e.g. autoscale
Ressource type: azurerm_monitor_action_group
Attributes: name, resource group, <xyz>_receiver, e.g. email_receiver
Ressource type: aazurerm_monitor_metric_alert
Attributes: name, resource group, <scope, description, target resource type (e.g. vm), 
blocks: criteria (when to trigger alert), action (previously defined)
===Monitor Autoscaling===
Ressource type: azurerm_monitor_autoscale_setting
Attributes: standard (name, location, resource group), target resource id (previously established vm, 
profile: name, capacity, rules
rules: metric triggers and scale action