2019-Infrastructure As Code With Terraform
2019-Infrastructure As Code With Terraform
greg@blacksintechnology:~$ whoami
Greg Greenlee
Agenda
● What is IaC?
○ Benefits
● What is Terraform?
● Why do we need Terraform?
● How do we use Terraform?
○ Providers
○ Resources
○ Variables (inputs)
○ Outputs
○ Data Structures
○ Modules
○ Conditionals
○ Iterations
○ Terraform State
● How do I get started?
What Is Infrastructure as Code?
Visibility
Traceability
What is Terraform?
● Infrastructure as code management tool that uses a declarative language to
build infrastructure
● Written in Go
● terraform.io
Imperative vs Declarative
Imperative (How) Declarative (What)
● Buy chocalate cake mix I need a chocolate cake big enough to feed 20
● Open cake mix box people
● Pour cake mix in bowl
● Add ingredients
● Stir
● Pour in pan
● Preheat oven to 350
● Place pan in oven
● Bake at 350
● etc
Why do we need Terraform?
Infrastructure is hard!
Idempotent
Cloud agnostic
DEV
STAGING
PRODUCTION
How do we use Terraform?
● MacOS
● Linux
● Windows
● FreeBSD
● Solaris
Usage
● Terraform init
○ initializes terraform directory
○ pulls in plugins for specified provider
○ Pulls in modules
● Terraform fmt
○ Rewrites terraform config files to canonical format and style
● Terraform validate
○ Runs checks that verify whether a configuration is syntactically valid and internally consistent
● terraform plan
○ A preview of what changes will be made
● Terraform apply
○ Applies changes
● Terraform destroy
○ Destroys all changes
● Terraform show
○ Shows resources from state file
Providers
provider "aws" {
region = "us-east-1"
}
Resources
Bread and butter that represents the infrastructure components you want to
manage
● Virtual machines
● Load balancers
● Firewall rules
● Virtual Networks
● Databases
● Message queues
● Data warehouses
● ….etc
Resources - code example
resource "aws_instance" "web" { resource "aws_elb" "bar" {
tags = {
instances = ["${aws_instance.web.id}" ]
Name = "HelloWorld"
tags = {
}
Name = "foobar-terraform-elb"
}
}
Variables
● Environment
○ Begins with TF_VAR_
■ export TF_VAR_somevariable=somevalue
● Inputs
● Ouputs
● Data Structures
○ Strings
○ Arrays
○ Maps
Variable example code
variable "image_id" { resource "aws_instance" "web" {
} }
Conditionals
If statements
If/else
Boolean operations
Conditional example
resource "aws_instance" "vpn" {
}
Iteration
resource "aws_iam_user" "example" {
count = length(var.user_names)
name = var.user_names[count.index]
variable “user_names” {
type = “list”
Modules
Reusable code
Collection of resources
name = "${var.sql_server_name}"
source = "git::https://myrepo/sql/_git/tf_azurerm_sql?ref=1.7"
resource_group_name = "${var.resource_group_name}"
resource_group_name = "my_resource_group"
location = "${var.resource_group_location}"
resource_group_location = "useast1"
}
sql_server_name = "my_sql_server_name"
resource "azurerm_sql_database" "test" {
sql_database_name = "my-sql-database"
name = "${var.sql_database_name}"
}
resource_group_name = "${var.resource_group_name}"
location = "${var.resource_group_location}"
server_name = "${var.my_sql_server_name}"
Functions
● String manipulation
● Numeric
● Collection
● Date and time
● ….more
Ex.
● uses local state to create plans and make changes to your infrastructure. Prior to any operation, Terraform does a refresh
Use it
Recommendations
Use terraform plan output
Use secret management - don’t store secrets directly in tf config files or env
variables
Plan structure
Resources
● Terraform.io
● The Terraform Book - James Turnbull
● Terraform Up and Running - Yevginy Brikman
● Me
○ greg.greenlee@insight.com
○ @BIT_greggreenle