12 - Terraform Handout
12 - Terraform Handout
BOOTCAMP Infrastructure as
Code with Terraform
Introduction to Terraform - 1
An open-source infrastructure as code (IaC) tool by HashiCorp
Let's you automate and manage:
Definition of configuration Declarative = define WHAT end result or desired state you want
files is declarative!
Imperative = define exact steps - HOW
Introduction to Terraform - 2
4. Configure Security
5. ...
DEV PROD
Difference of Ansible and Terraform
Both:
Infrastructure as Code
Mainly infrastructure
Mainly a configuration
provisioning tool
management tool
More advanced in orchestration
TF compares your desired with actual Without state, you would always have to
state to know which changes it needs to check the current state yourself and see
make to your infrastructure how to update your desired state!
How Terraform works - 2
This is the core workflow
main.tf
Terraform Architecture
TF executes the plan with Providers real infrastructure
main.tf
CORE
terraform.tfstate
Providers
the resources
(e.g. AWS)
Resources
Data Source
To create a new resource
Data Sources
Resource
Variables in Terraform - 1
Define
Variables (Input variables) let you customize
file
with variables
environment
Environment Variables in Terraform
Predefined Env Vars export TF_LOG=off
TF-Env Vars: TF has env vars, which you can use to change
logs
environment variable
This is technically the 4th way of setting a variable value, because we define a variable and
team collaboration
repository
review infrastructure changes using
When deploying virtual machines, we often need Another way are Provisioners
to pass in initial data when launching the instance Can be used to execute commands on the local
Most cloud providers have a mechanism to machine or remote machine to prepare the
available
Provisioners - 1 Different types of provisioners
Difference:
=> user_data: passing data to AWS
=> remote-exec: connect via ssh using TF
Provisioners - 2
Alternative to remote-exec:
Reuse
No simple clean up
running TF
Data backup
Can be shared
S3
bucket
Best Practices - 1
Use state locking (locks state file until writing of state file is completed)
Back up your state file and enable versioning (allows for state recovery)
Use _ (underscore) instead of - (dash) in all resource names, data source names, variable
Don’t hardcode values as much as possible - pass as variables or use data sources to get a
value