Infrastructure as Code with Terraform

Infrastructure as Code with Terraform

A short intro to Infrastructure as Code using terraform. Talk for GDLJS https://www.meetup.com/Guadalajara-JS/events/236104692/.

Repo with example at https://github.com/orlando/terraform-intro

A0574e5093df6afd68f1c109c5a9bf22?s=128

Orlando Del Aguila

December 13, 2016
Tweet

Transcript

  1. Infrastructure as Code with Terraform Orlando Del Aguila GDLJS S02E08

  2. @orlando @eatcodetravel @eatcodetravel eatcodetravel.com

  3. www.hashlabs.com

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

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

  6. “Write and execute code to define, deploy and update infrastructure”

    IAC / What
  7. • Ad hoc scripts • Configuration Management • Server Templating

    • Infrastructure Definition IAC / Types
  8. Snowflake servers IAC / Why

  9. Snowflake servers IAC / Why

  10. Confidence IAC / Why

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

  12. Documentation IAC / Why

  13. Version Control IAC / Why

  14. Coding best practices IAC / Why DRY / Testing /

  15. 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
  16. Terraform source: http://no-mans-sky.com/press/sheet.php?p=no_man%27s_sky

  17. HashiCorp Terraform / What

  18. Infrastructure definition Terraform / What

  19. Deterministic execution Terraform / What

  20. Multi provider support Terraform / What

  21. Uses HCL as language Terraform / What

  22. Golang OS Project Terraform / What

  23. Golang OS Project Terraform / What

  24. Translates HCL templates into API calls Terraform / How

  25. Saves current state as a file Terraform / How

  26. Diffs from the current state to apply changes Terraform /

    How
  27. 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" }
  28. 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" }
  29. 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" }
  30. 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" }
  31. 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" }
  32. 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" }
  33. 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}" }
  34. 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}" }
  35. 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}" } ...
  36. 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}" }
  37. Keep one .tfstate per environment Terraform / Tips

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

    Tips
  39. Store your terraform.tfvars in your remote* Terraform / Tips *

    this is how we do it, and works for us
  40. terraform.io/docs github.com/orlando/terraform-intro github.com/hashicorp/terraform github.com/terraform-community-modules github.com/segmentio/terraform-docs Terraform / Resources

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