Upgrade to Pro — share decks privately, control downloads, hide ads and more …

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

Orlando Del Aguila

December 13, 2016
Tweet

More Decks by Orlando Del Aguila

Other Decks in Programming

Transcript

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

    View full-size slide

  2. @orlando
    @eatcodetravel
    @eatcodetravel
    eatcodetravel.com

    View full-size slide

  3. www.hashlabs.com

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  8. Snowflake servers
    IAC / Why

    View full-size slide

  9. Snowflake servers
    IAC / Why

    View full-size slide

  10. Confidence
    IAC / Why

    View full-size slide

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

    View full-size slide

  12. Documentation
    IAC / Why

    View full-size slide

  13. Version Control
    IAC / Why

    View full-size slide

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

    View full-size slide

  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

    View full-size slide

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

    View full-size slide

  17. HashiCorp
    Terraform / What

    View full-size slide

  18. Infrastructure definition
    Terraform / What

    View full-size slide

  19. Deterministic
    execution
    Terraform / What

    View full-size slide

  20. Multi provider support
    Terraform / What

    View full-size slide

  21. Uses HCL as language
    Terraform / What

    View full-size slide

  22. Golang OS Project
    Terraform / What

    View full-size slide

  23. Golang OS Project
    Terraform / What

    View full-size slide

  24. Translates HCL templates
    into API calls
    Terraform / How

    View full-size slide

  25. Saves current state
    as a file
    Terraform / How

    View full-size slide

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

    View full-size slide

  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"
    }

    View full-size slide

  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"
    }

    View full-size slide

  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"
    }

    View full-size slide

  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"
    }

    View full-size slide

  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"
    }

    View full-size slide

  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"
    }

    View full-size slide

  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}"
    }

    View full-size slide

  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}"
    }

    View full-size slide

  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}"
    }
    ...

    View full-size slide

  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}"
    }

    View full-size slide

  37. Keep one .tfstate
    per environment
    Terraform / Tips

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  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

    View full-size slide

  41. Thanks

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

    View full-size slide