What is Terraform
Terraform is a tool for building, changing, and versioning infrastructure safely and efficiently.
It is open source and declarative
The differences between declarative and imperative:
declarative: define WHAT the result you want
imperative: define exact steps - HOW
The key features:
- Infrastructure as Code
- Execution Plan
- Resource Graph
- Change Automation
A. Previsioning infrastructure:
- private network space
- ec2 server instances
- install Docker and other tools
- security
B. Deploy applications
Difference between Ansible and Terraform
Both: Infrastructure as a Code
Ansible
Better for configuring that infrastructure
Mainly a configuration tool
More mature
Terraform
Better for infrastructure
Mainly infrastructure provisioning tool
More advanced in orchestration
How does Terraform work
create execution plan
execute plan with providers
Configuration
example:
1 | terraform { |
Terraform Block
it defines which provider to download from Terraform Registry
Provider
It is used to configure the named provider
Resource
It contains the resource type, and the resource name.
resource ID: resource type and resource name together
Commands
refresh:
query provider to get the current state
plan:
create an execution plan
apply:
execute the plan
destroy:
destroy the resources/infrastructure