Terraform Resources
3 min readJul 31, 2022
Introduction
- Terraform resource syntax introduction
- Resource behaviour
- Understanding Terraform state
- Understand Terraform Resource Behaviour Update In place
- Introduction to Resource Meta-Arguments
- Meta arguments Depends on
- Count
- Resource meta-argument for_each
- Terraform resource meta argument lifecycle
- Meta argument lifecycle
— — — — — — — — — — — — — — — —
- Terraform resource syntax introduction
- Block Type:- Resource Figure 1.
- There can be Block inside block which could be either provisioners or tags.
- Block labels:- 2 block labels.
- Inside block we have arguments.
- Resource syntax
- Resource “resource_type” “resource local_name” {}
- for example Resource “aws_instance” “demo_aws_instance” {} - Resource local name is use to refer to same resource in the same module (unique).
- Meta argument:- It can be used with any resource to change behaviour of resource.
- Resource arguments:- specific to that resource type.
2. Resource behaviour
- Create Resource:- Resource in configuration but not in real infrastructure
- Destroy resource:- Resource in state but not in configuration
- Update in place resource:- update in place resources whose arguments change.
- Destroy and recreate:- Destroy and recreate resources whose arguments have changed
- This all tied to state file
- Command:- terraform fmt to format all the files
3. Understanding Terraform state
- terraform.tfstate file store state of managed infrastructure and configuration
- It’s used to map real world resources to your configuration (.tf files) It keep track of metadata and improve performance for large infrastructure
- It can be also stored Remotely for Teams
- It main purpose is to store binding between object in remote system and which are declared in your configurations
4. Understand Terraform Resource Behaviour Update In place
- Current state:- present state of infrastructure
- Desired state:- If you add any new resources in configuration that become desired state and it’s different from current state
5. Introduction to Resource Meta-Arguments
- Can be used with any resource type to change behaviour of resources
- Types
- Depends_on:- Hidden interdependencies that terraform cannot handle on it’s own
- Count:- for creating multiple resources instances according to a count
- for_each:- To create multiple instances according to a map or set of strings
- Provider:- For selecting a non-default provider configuration
- Lifecycle:- Altering standard resource behaviour
- Provisioners and connections:- For taking extra action once resource creation is done
6. Meta arguments Depends on
- Saying explicitly that terraform can’t handle automatically
- Direct reference to the resource whose value must be known
- Its only used as last resort
7. Count
- We can create multiple instance of EC2
8. Resource meta-argument for_each
- Modify configuration of each resource with for_each
- With Map Figure 3
- With strings Figure 4
9. Terraform resource meta argument lifecycle
- For example
- Resource { lifecycle { create_before_destroy = true } }
- Types
- create_before_destroy:- by default destroy and then recreate but now first create then destroy
- prevent_destroy:- If we don’t want to destroy any resource we ever the case or we also do terraform destroy but if we remove this resource configuration then it will be removed
- Ignore_changes:- Due to some cases we might need to change tag or something from console in the case we don’t want to run our terraform again. So this is to ignore the such things
- Example
- ignore_changes = [ tags, ]
10. Meta argument lifecycle
- Create_before_destroy:- First create new resource then destroy the previous one
- prevent_destroy:- Do not destroy that resource
- Ignore_changes:- Whatever we do manually from console will be destroyed by terraform apply when it’s executed so we add ignore_changes