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 Slide

  2. @orlando
    @eatcodetravel
    @eatcodetravel
    eatcodetravel.com

    View Slide

  3. www.hashlabs.com

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  8. Snowflake servers
    IAC / Why

    View Slide

  9. Snowflake servers
    IAC / Why

    View Slide

  10. Confidence
    IAC / Why

    View Slide

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

    View Slide

  12. Documentation
    IAC / Why

    View Slide

  13. Version Control
    IAC / Why

    View Slide

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

    View 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 Slide

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

    View Slide

  17. HashiCorp
    Terraform / What

    View Slide

  18. Infrastructure definition
    Terraform / What

    View Slide

  19. Deterministic
    execution
    Terraform / What

    View Slide

  20. Multi provider support
    Terraform / What

    View Slide

  21. Uses HCL as language
    Terraform / What

    View Slide

  22. Golang OS Project
    Terraform / What

    View Slide

  23. Golang OS Project
    Terraform / What

    View Slide

  24. Translates HCL templates
    into API calls
    Terraform / How

    View Slide

  25. Saves current state
    as a file
    Terraform / How

    View Slide

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

    View 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 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 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 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 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 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 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 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 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 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 Slide

  37. Keep one .tfstate
    per environment
    Terraform / Tips

    View Slide

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

    View Slide

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

    View 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 Slide

  41. Thanks

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

    View Slide