The why, what and how to leverage Terraform to manage Cloud resources safely.
Experience feedback from adoption by Leboncoin DataEngineering team.
In these slides you will find introduction material for beginners and advanced use cases you will quickly be facing when working within a team and with enterprise constraints.
11. Terraform 101
Overview
Terraform is a tool for building, changing and
versioning infrastructure safely and efficiently.
Terraform can manage existing and popular service
providers as well as custom in-house solutions.
terraform.io/intro
12. Terraform 101
Overview
What is Terraform
● Infrastructure as code
● Execution plan
● Resource graph
● Change automation tool
https://www.terraform.io/intro/index.html
15. Terraform 101
Concepts:
● Providers
● Resources
A TANGIBLE component of you infrastructures
● Provider specific
● What you want to manage
resource "aws_db_instance" "timeout_example" {
allocated_storage = 10
engine = "mysql"
engine_version = "5.6.17"
instance_class = "db.t1.micro"
name = "mydb"
# ...
timeouts {
create = "60m"
delete = "2h"
}
}
16. Terraform 101
Concepts:
● Providers
● Resources
● Data sources
A specific “dynamic” data you want
● External source
● Like dynamic variables
# Find the latest available AMI that is tagged with Component = web
data "aws_ami" "web" {
filter {
name = "state"
values = ["available"]
}
filter {
name = "tag:Component"
values = ["web"]
}
most_recent = true
}
17. Terraform 101
Concepts:
● Providers
● Resources
● Data sources
● Variables
Parameters of our code
● Have to be declared specifically
● Different types (String, boolean, maps, list, …)
● Can have defaults
variable "key" { type = "string" }
variable "images" {
type = "map"
default = {
us-east-1 = "image-1234"
us-west-2 = "image-4567"
}
}
variable "zones" {
default = ["us-east-1a", "us-east-1b"]
}
18. Terraform 101
Concepts:
● Providers
● Resources
● Data sources
● Variables
● Outputs
Outputs = Informations we want to get after Terraform has run
● Can be queried via CLI
● Will be shared across modules and resources
output "address" {
value = "${aws_instance.db.public_dns}"
}
26. Terraform 102
Internals
1 - Pre-Compiles
Check syntax, types …
Validate resources
5 - Applies
Makes the API call to apply the
changes described in the plan
2 - Refresh the state
Call the providers APIs to
get an updated view
4 - Plan
Computes the plan to
match the desired state
3 - Compiles 2
Runs DataSources
Instantiates the resources
-> Gets desired state
Terraform
internals
6 - Applies
Updates the final state file
27. Terraform 102
Internals
1 - Pre-Compiles
Check syntax, types …
Validate resources
5 - Applies
Makes the API call to apply the
changes described in the plan
2 - Refresh the state
Call the providers APIs to
get an updated view
4 - Plan
Computes the plan to
match the desired state
3 - Compiles 2
Runs DataSources
Instantiates the resources
-> Gets desired state
Terraform
internals
6 - Applies
Updates the final state file
44. Terraform 103
Modules
Terraform Modules
● Reusable set of “pre” defined / packaged resources
● Helps to model the architecture
Features:
● Versioned
● Various sources:
○ HTTP
○ SCM (git, svn, hg, …)
○ Local file system
https://registry.terraform.io/
https://www.terraform.io/docs/modules/index.html
68. Automated actions via “invoke”
Terraform 104
Automate you needs
$ invoke -l
Available tasks:
...
provision.apply Update the whole stack (More with '--help')
provision.destroy Destroy the aws resources (More with '--help')
provision.init Initialize Terraform (More with '--help')
provision.list-stack-envs
provision.list-stacks
provision.status Display IDs of current resources (More with '--help')
...
70. Terraform 104 State Drift Detection
● TF is imperative by usage (No daemon)
● For better readability -> Split your code in “Stacks”
● Shared with data-sources among teams
● Manual actions in the AWS Console or other projects
https://www.hashicorp.com/blog/detecting-and-managing-drift-with-terraform
https://medium.com/build-acl/state-drift-detection-using-terraform-d0383628d2ea
Monitoring
71. Terraform 104 State Drift Detection
https://github.com/gibbster/terraform-plan-drift-checker
Monitoring
83. Conclusion
Terraform:
● 1 binary, for every OS
● Wide range of providers
● Simple concepts
Answers our needs:
● Infra as Code
● Operations safety
● Share and reuse with ease