Slide 1

Slide 1 text

Infrastructure as Code with Terraform Orlando Del Aguila GDLJS S02E08

Slide 2

Slide 2 text

@orlando @eatcodetravel @eatcodetravel eatcodetravel.com

Slide 3

Slide 3 text

www.hashlabs.com

Slide 4

Slide 4 text

Infrastructure as Code source: http://www.itnews.com.au/news/facebook-powers-up-arctic-circle-data-centre-346509

Slide 5

Slide 5 text

source: http://www.itnews.com.au/news/facebook-powers-up-arctic-circle-data-centre-346509

Slide 6

Slide 6 text

“Write and execute code to define, deploy and update infrastructure” IAC / What

Slide 7

Slide 7 text

• Ad hoc scripts • Configuration Management • Server Templating • Infrastructure Definition IAC / Types

Slide 8

Slide 8 text

Snowflake servers IAC / Why

Slide 9

Slide 9 text

Snowflake servers IAC / Why

Slide 10

Slide 10 text

Confidence IAC / Why

Slide 11

Slide 11 text

Self-service IAC / Why source: https://www.flickr.com/photos/smdevivo/5524665573

Slide 12

Slide 12 text

Documentation IAC / Why

Slide 13

Slide 13 text

Version Control IAC / Why

Slide 14

Slide 14 text

Coding best practices IAC / Why DRY / Testing / ❤

Slide 15

Slide 15 text

2016 DevOps Report* IAC / Why • 24x faster to recover from failure • 50% less time to find issues • 200x more deploys • 2.2x engineer happiness** ** Likeliness to recommend their place of work to others * puppet.com/resources/white-paper/2016-state-of-devops-report

Slide 16

Slide 16 text

Terraform source: http://no-mans-sky.com/press/sheet.php?p=no_man%27s_sky

Slide 17

Slide 17 text

HashiCorp Terraform / What

Slide 18

Slide 18 text

Infrastructure definition Terraform / What

Slide 19

Slide 19 text

Deterministic execution Terraform / What

Slide 20

Slide 20 text

Multi provider support Terraform / What

Slide 21

Slide 21 text

Uses HCL as language Terraform / What

Slide 22

Slide 22 text

Golang OS Project Terraform / What

Slide 23

Slide 23 text

Golang OS Project Terraform / What

Slide 24

Slide 24 text

Translates HCL templates into API calls Terraform / How

Slide 25

Slide 25 text

Saves current state as a file Terraform / How

Slide 26

Slide 26 text

Diffs from the current state to apply changes Terraform / How

Slide 27

Slide 27 text

Terraform / Syntax data "aws_ami" "ubuntu" { most_recent = true filter { name = "name" values = ["ubuntu/images/hvm-ssd/ubuntu-trusty-*"] } } resource "aws_instance" "example" { ami = "${data.aws_ami.ubuntu.id}" instance_type = "t2.micro" }

Slide 28

Slide 28 text

Terraform / Syntax data "aws_ami" "ubuntu" { most_recent = true filter { name = "name" values = [“ubuntu/images/hvm-ssd/ubuntu-trusty-*"] } } resource "aws_instance" "example" { ami = "${data.aws_ami.ubuntu.id}" instance_type = "t2.micro" }

Slide 29

Slide 29 text

Terraform / Syntax data "aws_ami" "ubuntu" { most_recent = true filter { name = "name" values = [“ubuntu/images/hvm-ssd/ubuntu-trusty-*"] } } resource "aws_instance" "example" { ami = "${data.aws_ami.ubuntu.id}" instance_type = "t2.micro" }

Slide 30

Slide 30 text

Terraform / Syntax data "aws_ami" "ubuntu" { most_recent = true filter { name = "name" values = [“ubuntu/images/hvm-ssd/ubuntu-trusty-*"] } } resource "aws_instance" "example" { ami = "${data.aws_ami.ubuntu.id}" instance_type = "t2.micro" }

Slide 31

Slide 31 text

Terraform / Syntax data "aws_ami" "ubuntu" { most_recent = true filter { name = "name" values = [“ubuntu/images/hvm-ssd/ubuntu-trusty-*"] } } resource "aws_instance" "example" { ami = "${data.aws_ami.ubuntu.id}" instance_type = "t2.micro" }

Slide 32

Slide 32 text

Terraform / Syntax data "aws_ami" "ubuntu" { most_recent = true filter { name = "name" values = [“ubuntu/images/hvm-ssd/ubuntu-trusty-*"] } } resource "aws_instance" "example" { ami = "${data.aws_ami.ubuntu.id}" instance_type = "t2.micro" }

Slide 33

Slide 33 text

Terraform / Syntax data "aws_ami" "ubuntu" { most_recent = true filter { name = "name" values = [“ubuntu/images/hvm-ssd/ubuntu-trusty-*"] } } resource "aws_instance" "example" { ami = "${data.aws_ami.ubuntu.id}" instance_type = "t2.micro" } resource "aws_eip" "example_eip" { instance = "${aws_instance.example.id}" }

Slide 34

Slide 34 text

Terraform / Syntax data "aws_ami" "ubuntu" { most_recent = true filter { name = "name" values = [“ubuntu/images/hvm-ssd/ubuntu-trusty-*"] } } resource "aws_instance" "example" { ami = "${data.aws_ami.ubuntu.id}" instance_type = "t2.micro" } resource "aws_eip" "example_eip" { instance = "${aws_instance.example.id}" } // IP of the Bastion EC2 instance output "ip" { value = "${aws_eip.example_eip.public_ip}" }

Slide 35

Slide 35 text

Terraform / Syntax ... variable "instance_type" { description = "EC2 instance type" default = "t2.nano" } resource "aws_instance" "example" { ami = "${data.aws_ami.ubuntu.id}" instance_type = "${var.instance_type}" } ...

Slide 36

Slide 36 text

Terraform / Syntax / Modules # Create instances module "t2nano" { source = "./aws_instance" instance_type = "t2.nano" } module "t2micro" { source = "./aws_instance" instance_type = "t2.micro" } # Output variables output "t2microip" { value = "${module.t2micro.ip}" } output "t2small" { value = "${module.t2small.ip}" }

Slide 37

Slide 37 text

Keep one .tfstate per environment Terraform / Tips

Slide 38

Slide 38 text

Use a remote store for your .tfstate file Terraform / Tips

Slide 39

Slide 39 text

Store your terraform.tfvars in your remote* Terraform / Tips * this is how we do it, and works for us

Slide 40

Slide 40 text

terraform.io/docs github.com/orlando/terraform-intro github.com/hashicorp/terraform github.com/terraform-community-modules github.com/segmentio/terraform-docs Terraform / Resources

Slide 41

Slide 41 text

Thanks source: http://no-mans-sky.com/press/sheet.php?p=no_man%27s_sky