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

Reusable Infrastructure with Terraform

Reusable Infrastructure with Terraform

In this workshop, Mary Cutrali (Product Manager at HashiCorp) and I give an overview on how to build reusable infrastructure components using HashiCorp Terraform.

This version of the presentation was given at an AWS-internal summit in April 2021.

Kerim Satirli
PRO

April 29, 2021
Tweet

More Decks by Kerim Satirli

Other Decks in Programming

Transcript

  1. Creating Reusable Infrastructure with Terraform Modules

  2. CODE EDITOR service "http" "proxy" { listen_addr = "127.0.0.1:${var.port}" process

    "server" { command = ["./server"] } } variable "port" { type = number description = "Port for proxy service" default = 8080 } HashiCorp Configuration Language
  3. Terraform Editions Terraform Cloud Terraform OSS Terraform Enterprise ▪ freely

    available ▪ broad support ▪ common binary ▪ hosted option ▪ all OSS features ▪ team management ▪ module sharing ▪ audit logging ▪ cost estimation ▪ on-prem option ▪ all Cloud features ▪ team management ▪ module sharing ▪ audit logging
    Learn more about Terraform Editions on <a href="https://www.terraform.io/docs/cloud/paid.html">terraform.io</a>.
  4. Building Modules for HashiCorp Terraform

  5. Building Modules TERMINAL > mkdir ./terraform-aws-cf-website

  6. TERMINAL > mkdir ./terraform-aws-cf-website Building Modules

  7. TERMINAL > mkdir ./terraform-aws-cf-website Building Modules

  8. Building Modules TERMINAL > tree ./terraform-aws-cf-website . ├── main.tf ├──

    outputs.tf └── variables.tf
  9. Building Modules TERMINAL > tree ./terraform-aws-cf-website . ├── examples/ ├──

    LICENSE ├── main.tf ├── outputs.tf ├── README.md ├── tests/ └── variables.tf
  10. Writing Modules MAIN.TF resource "aws_cloudfront_distribution" "this" { comment = var.comment

    enabled = var.enabled default_cache_behavior { ... } origin { ... } }
  11. Writing Modules VARIABLES.TF variable "enabled" { type = bool description

    = "Toggle to enable CF Distribution" default = true } variable "comment" { type = string description = "Comment for CloudFront Distribution" default = "Terraform-managed" }
  12. Writing Modules OUTPUTS.TF output "arn" { value = aws_cloudfront_distribution.this.arn description

    = "ARN of the CloudFront Distribution" }
  13. Documenting Modules README.MD ### Inputs | Name | Description |

    Type | Default | |-----------|-------------------|----------|-------------------| | `comment` | Comment for [...] | `string` | Terraform-managed | | `enabled` | Toggle to [...] | `bool` | `true` | ### Outputs | Name | Description | |-------|------------------------------------| | `arn` | ARN of the CloudFront Distribution |
  14. Using Modules EXAMPLE.TF module "my_website" { source = "aws-quickstart/cf-website/aws" }

  15. Using Modules EXAMPLE.TF module "my_website" { source = "../.." }

  16. Using Modules EXAMPLE.TF module "my_website" { source = "git@github.com/aws-quickstart/website" }

  17. Using Modules EXAMPLE.TF module "my_website" { source = "aws-quickstart/cf-website/aws" version

    = "1.0.0" comment = "CF Website for example.com" enabled = true } output "my_website_arn" { value = module.my_website.arn }
  18. Publishing Modules for HashiCorp Terraform

  19. Tagging Modules TERMINAL > git tag "1.0.0"

  20. Tagging Modules github.com/aws-quickstart/terraform-aws-cf-website

  21. Publishing Modules registry.terraform.io/

  22. Publishing Modules registry.terraform.io/github/create

  23. registry.terraform.io/namespaces/aws-quickstart

  24. Publishing Modules registry.terraform.io/modules/aws-quickstart/cf-website

  25. AWS Terraform Modules Program

  26. Modules Program ▪ collab between HashiCorp and AWS IA team

    ▪ empowers SAs and engineers to build modules ▪ codifies best-practices for common workflows
  27. https://registry.terraform.io/namespaces/aws-quickstart

  28. Tips and Tricks

  29. Tips and Tricks ▪ do one thing well ▪ think

    about composability ▪ bad documentation is a bug
  30. Materials ▪ slides: speakerdeck.com/ksatirli/reusable-infrastructure-with-terraform ▪ tutorial: learn.hashicorp.com/collections/terraform/modules ▪ docs generator:

    terraform-docs.io
  31. Mary Cutrali (She/Her) Product Manager at HashiCorp

    Find me on <a href="https://twitter.com/maryelizbeth">twitter.com/maryelizbeth</a>.
  32. @maryelizbeth on GitHub Product Manager at HashiCorp

    Find me on <a href="https://twitter.com/maryelizbeth">twitter.com/maryelizbeth</a>.
  33. Kerim Satirli (He / Him) Sr. Developer Advocate at HashiCorp

    Find me on <a href="https://twitter.com/ksatirli">twitter.com/ksatirli</a>.
  34. @ksatirli on Twitter and GitHub Sr. Developer Advocate at HashiCorp

    Find me on <a href="https://twitter.com/ksatirli">twitter.com/ksatirli</a>.
  35. Thank You mary@hashicorp.com kerim@hashicorp.com