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

Infrastructure as Code @ phpCE 2017

Infrastructure as Code @ phpCE 2017

Terraform enables you to safely and predictably create, change, and improve production infrastructure. It is an open source tool that codifies APIs into declarative configuration files that can be shared amongst team members, treated as code, edited, reviewed, and versioned. Terraform has emerged as a key player in the DevOps world for defining, launching, and managing infrastructure as code (IAC) across a variety of cloud and virtualization platforms, including AWS, Google Cloud, and Azure. In this talk I'll show you how to get up and running with Terraform using IaaS (e.g. AWS, DigitalOcean) or SaaS providers (e.g. CloudFlare, GitHub).

Sebastian Grodzicki

November 02, 2017
Tweet

More Decks by Sebastian Grodzicki

Other Decks in Programming

Transcript

  1. $ whoami Sebastian Grodzicki
 • CTO at SHOWROOM • PHP

    developer for 15+ years • DevOps enthusiast !@sebgrodzicki
  2. Data center evolution DC VM C C C C C

    C VM C C C C C C VM C C C C C C VM C C C C C C VM C C C C C C VM C C C C C C VM C C C C C C VM C C C C C C VM C C C C C C VM C C C C C C VM C C C C C C VM C C C C C C
  3. Data center evolution ACQUIRE
 
 
 
 
 
 


    
 
 
 DESTROY
 
 
 
 
 
 
 
 
 
 PROVISION
 
 
 
 
 
 
 
 
 
 UPDATE
 
 
 
 
 
 
 
 
 

  4. Data center evolution ACQUIRE
 
 
 
 
 
 


    
 
 
 DESTROY
 
 
 
 
 
 
 
 
 
 PROVISION
 
 
 
 
 
 
 
 
 
 UPDATE
 
 
 
 
 
 
 
 
 
 VENDOR
  5. Data center evolution ACQUIRE
 
 
 
 
 
 


    
 
 
 DESTROY
 
 
 
 
 
 
 
 
 
 PROVISION
 
 
 
 
 
 
 
 
 
 UPDATE
 
 
 
 
 
 
 
 
 
 VENDOR DC OPS
  6. Data center evolution ACQUIRE
 
 
 
 
 
 


    
 
 
 DESTROY
 
 
 
 
 
 
 
 
 
 PROVISION
 
 
 
 
 
 
 
 
 
 UPDATE
 
 
 
 
 
 
 
 
 
 VENDOR SYSADMIN DC OPS
  7. Data center evolution ACQUIRE
 
 
 
 
 
 


    
 
 
 DESTROY
 
 
 
 
 
 
 
 
 
 PROVISION
 
 
 
 
 
 
 
 
 
 UPDATE
 
 
 
 
 
 
 
 
 
 VENDOR DC OPS SYSADMIN DC OPS
  8. Data center evolution ACQUIRE
 
 
 
 
 
 


    
 
 
 DESTROY
 
 
 
 
 
 
 
 
 
 PROVISION
 
 
 
 
 
 
 
 
 
 UPDATE
 
 
 
 
 
 
 
 
 
 VENDOR DC OPS SYSADMIN DC OPS WEEKS
  9. Data center evolution ACQUIRE
 
 
 
 
 
 


    
 
 
 DESTROY
 
 
 
 
 
 
 
 
 
 PROVISION
 
 
 
 
 
 
 
 
 
 UPDATE
 
 
 
 
 
 
 
 
 
 VENDOR DC OPS SYSADMIN DC OPS WEEKS DAYS
  10. Data center evolution ACQUIRE
 
 
 
 
 
 


    
 
 
 DESTROY
 
 
 
 
 
 
 
 
 
 PROVISION
 
 
 
 
 
 
 
 
 
 UPDATE
 
 
 
 
 
 
 
 
 
 VENDOR DC OPS SYSADMIN DC OPS WEEKS DAYS DAYS
  11. Data center evolution ACQUIRE
 
 
 
 
 
 


    
 
 
 DESTROY
 
 
 
 
 
 
 
 
 
 PROVISION
 
 
 
 
 
 
 
 
 
 UPDATE
 
 
 
 
 
 
 
 
 
 VENDOR DC OPS SYSADMIN DC OPS WEEKS DAYS DAYS DAYS
  12. Cloud computing ACQUIRE
 
 
 
 
 
 
 


    
 
 DESTROY
 
 
 
 
 
 
 
 
 
 PROVISION
 
 
 
 
 
 
 
 
 
 UPDATE
 
 
 
 
 
 
 
 
 
 SECONDS SECONDS SECONDS SECONDS
  13. resource "digitalocean_droplet" "demo" {
 image = "ubuntu-17-10-x64"
 name = "phpCE"


    region = "fra1"
 size = "512mb"
 }
 
 resource "cloudflare_record" "demo" {
 domain = "grodzicki.pl"
 name = "phpce"
 type = "A"
 value = "${digitalocean_droplet.demo.ipv4_address}"
 }
 main.tf
  14. $ terraform init
 
 Initializing provider plugins...
 - Downloading plugin

    for provider "digitalocean" (0.1.2)...
 - Downloading plugin for provider "cloudflare" (0.1.0)...
 
 Terraform has been successfully initialized! Terminal
  15. $ terraform plan
 
 Terraform will perform the following actions:


    
 + cloudflare_record.demo
 id: <computed>
 domain: "grodzicki.pl"
 hostname: <computed>
 name: "phpce"
 proxied: "false"
 ttl: <computed>
 type: "A"
 value: "${digitalocean_droplet.demo.ipv4_address}"
 zone_id: <computed>
 
 + digitalocean_droplet.demo
 id: <computed>
 disk: <computed>
 image: "ubuntu-17-10-x64"
 ipv4_address: <computed>
 ipv4_address_private: <computed>
 ipv6_address: <computed>
 ipv6_address_private: <computed>
 locked: <computed>
 name: "phpCE"
 price_hourly: <computed>
 price_monthly: <computed>
 region: "fra1"
 resize_disk: "true"
 size: "512mb"
 status: <computed>
 vcpus: <computed>
 
 Plan: 2 to add, 0 to change, 0 to destroy. Terminal
  16. $ terraform apply
 
 digitalocean_droplet.demo: Creating...
 disk: "" => "<computed>"


    image: "" => "ubuntu-17-10-x64"
 ipv4_address: "" => "<computed>"
 ipv4_address_private: "" => "<computed>"
 ipv6_address: "" => "<computed>"
 ipv6_address_private: "" => "<computed>"
 locked: "" => "<computed>"
 name: "" => "phpCE"
 price_hourly: "" => "<computed>"
 price_monthly: "" => "<computed>"
 region: "" => "fra1"
 resize_disk: "" => "true"
 size: "" => "512mb"
 status: "" => "<computed>"
 vcpus: "" => "<computed>"
 digitalocean_droplet.demo: Creation complete after 26s (ID: 69048017)
 cloudflare_record.demo: Creating...
 domain: "" => "grodzicki.pl"
 hostname: "" => "<computed>"
 name: "" => "phpce"
 proxied: "" => "false"
 ttl: "" => "<computed>"
 type: "" => "A"
 value: "" => "207.154.225.151"
 zone_id: "" => "<computed>"
 cloudflare_record.demo: Creation complete after 1s (ID: 4a59cffb21560ea257b2567d362fec2b)
 
 Apply complete! Resources: 2 added, 0 changed, 0 destroyed. Terminal
  17. $ terraform destroy
 
 digitalocean_droplet.demo: Refreshing state... (ID: 69048513)
 cloudflare_record.demo:

    Refreshing state... (ID: c30fe74f5279d0def)
 cloudflare_record.demo: Destroying... (ID: c30fe74f5279d0def)
 cloudflare_record.demo: Destruction complete after 2s
 digitalocean_droplet.demo: Destroying... (ID: 69048513)
 digitalocean_droplet.demo: Destruction complete after 14s
 
 Destroy complete! Resources: 2 destroyed. Terminal
  18. resource "digitalocean_droplet" "demo" {
 image = "ubuntu-17-10-x64"
 name = "phpCE"


    region = "fra1"
 - size = "512mb"
 + size = "1gb"
 }
 
 resource "cloudflare_record" "demo" {
 domain = "grodzicki.pl"
 name = "phpce"
 type = "A"
 value = "${digitalocean_droplet.demo.ipv4_address}"
 }
 main.tf
  19. $ terraform plan
 
 Resource actions are indicated with the

    following symbols:
 ~ update in-place
 
 Terraform will perform the following actions:
 
 ~ digitalocean_droplet.demo
 size: "512mb" => "1gb"
 
 Plan: 0 to add, 1 to change, 0 to destroy. Terminal
  20. $ terraform apply
 
 digitalocean_droplet.demo: Modifying... (ID: 69048968)
 size: "512mb"

    => "1gb"
 digitalocean_droplet.demo: Modifications complete after 53s (ID: 69048968)
 
 Apply complete! Resources: 0 added, 1 changed, 0 destroyed. Terminal
  21. resource "digitalocean_droplet" "demo" {
 - image = "ubuntu-17-10-x64"
 + image

    = "debian-9-x64"
 name = "phpCE"
 region = "fra1"
 size = "1gb"
 }
 
 resource "cloudflare_record" "demo" {
 domain = "grodzicki.pl"
 name = "phpce"
 type = "A"
 value = "${digitalocean_droplet.demo.ipv4_address}"
 }
 main.tf
  22. $ terraform plan
 
 Resource actions are indicated with the

    following symbols:
 ~ update in-place
 -/+ destroy and then create replacement
 
 Terraform will perform the following actions:
 ~ cloudflare_record.demo
 value: "46.101.143.229" => "${digitalocean_droplet.demo.ipv4_address}"
 
 -/+ digitalocean_droplet.demo (new resource required)
 id: "69048968" => <computed> (forces new resource)
 disk: "30" => <computed>
 image: "ubuntu-17-10-x64" => "debian-9-x64" (forces new resource)
 ipv4_address: "46.101.143.229" => <computed>
 ipv4_address_private: "" => <computed>
 ipv6_address: "" => <computed>
 ipv6_address_private: "" => <computed>
 locked: "false" => <computed>
 name: "phpCE" => "phpCE"
 price_hourly: "0.01488" => <computed>
 price_monthly: "10" => <computed>
 region: "fra1" => "fra1"
 resize_disk: "true" => "true"
 size: "1gb" => "1gb"
 status: "active" => <computed>
 vcpus: "1" => <computed>
 
 Plan: 1 to add, 1 to change, 1 to destroy. Terminal
  23. $ terraform apply
 
 digitalocean_droplet.demo: Destroying... (ID: 69056095)
 digitalocean_droplet.demo: Destruction

    complete after 12s
 digitalocean_droplet.demo: Creating...
 disk: "" => "<computed>"
 image: "" => "debian-9-x64"
 ipv4_address: "" => "<computed>"
 ipv4_address_private: "" => "<computed>"
 ipv6_address: "" => "<computed>"
 ipv6_address_private: "" => "<computed>"
 locked: "" => "<computed>"
 name: "" => "phpCE"
 price_hourly: "" => "<computed>"
 price_monthly: "" => "<computed>"
 region: "" => "fra1"
 resize_disk: "" => "true"
 size: "" => "1gb"
 status: "" => "<computed>"
 vcpus: "" => "<computed>"
 digitalocean_droplet.demo: Creation complete after 47s (ID: 69056200)
 
 cloudflare_record.demo: Modifying... (ID: 7a5ad9e5a510eb3a87606bbacf2ccbec)
 value: "207.154.230.75" => "165.227.146.145"
 cloudflare_record.demo: Modifications complete after 3s (ID: 7a5ad9e5a510eb3a87606bbacf2ccbec)
 
 Apply complete! Resources: 1 added, 1 changed, 1 destroyed. Terminal
  24. 70+ providers AWS Bitbucket Chef Cloudflare Consul Datadog DigitalOcean DNSimple

    Docker Dyn Fastly GitHub Google Cloud Grafana Heroku Kubernetes Logentries MySQL New Relic Nomad NS1 OpenStack OVH Rancher Scaleway SoftLayer VMware
  25. resource "github_team" "backend" {
 name = "backend"
 }
 
 resource

    "github_team_membership" "sebastian" {
 team_id = "${github_team.backend.id}"
 username = "sgrodzicki"
 } main.tf
  26. $ terraform plan
 
 Resource actions are indicated with the

    following symbols:
 + create
 
 Terraform will perform the following actions:
 
 + github_team.backend
 id: <computed>
 name: "backend"
 privacy: "secret"
 
 + github_team_membership.sebastian
 id: <computed>
 role: "member"
 team_id: "${github_team.backend.id}"
 username: "sgrodzicki"
 
 Plan: 2 to add, 0 to change, 0 to destroy. Terminal
  27. $ terraform apply
 
 github_team.backend: Creating...
 name: "" => "backend"


    privacy: "" => "secret"
 github_team.backend: Creation complete after 1s (ID: 2541906)
 
 github_team_membership.sebastian: Creating...
 role: "" => "member"
 team_id: "" => "2541906"
 username: "" => "sgrodzicki"
 github_team_membership.sebastian: Creation complete after 1s (ID: 2541906:sgrodzicki)
 
 Apply complete! Resources: 2 added, 0 changed, 0 destroyed. Terminal
  28. $ terraform plan
 
 Resource actions are indicated with the

    following symbols:
 + create
 
 Terraform will perform the following actions:
 
 + github_repository.phpce
 id: <computed>
 allow_merge_commit: "true"
 allow_rebase_merge: "true"
 allow_squash_merge: "true"
 default_branch: <computed>
 description: "phpCE 2017"
 full_name: <computed>
 git_clone_url: <computed>
 http_clone_url: <computed>
 name: "phpce"
 ssh_clone_url: <computed>
 svn_url: <computed>
 
 Plan: 1 to add, 0 to change, 0 to destroy. Terminal
  29. $ terraform apply
 
 github_repository.phpce: Creating...
 allow_merge_commit: "" => "true"


    allow_rebase_merge: "" => "true"
 allow_squash_merge: "" => "true"
 default_branch: "" => "<computed>"
 description: "" => "phpCE 2017"
 full_name: "" => "<computed>"
 git_clone_url: "" => "<computed>"
 http_clone_url: "" => "<computed>"
 name: "" => "phpce"
 ssh_clone_url: "" => "<computed>"
 svn_url: "" => "<computed>"
 
 github_repository.phpce: Creation complete after 1s (ID: phpce)
 
 Apply complete! Resources: 1 added, 0 changed, 0 destroyed. Terminal
  30. $ terraform graph cloudflare_record.demo digitalocean_droplet.demo provider.cloudflare provider.digitalocean [root] meta.count-boundary (count

    boundary fixup) [root] provider.cloudflare (close) [root] provider.digitalocean (close) [root] root
  31. $ terraform state list (List resources in the state) mv

    (Move an item in the state) pull (Pull current state and output to stdout) push (Update remote state from a local state file) rm (Remove an item from the state) show (Show a resource in the state)